编程介的小学生 2019-01-07 19:34 采纳率: 20.5%
浏览 299
已采纳

计算数组合并的最小组合方式,采用C语言技术计算的,怎么做

Problem Description
There are N number,they are respectively from 1 to N.

for a set of numbers, we set a problem as flow:

we assume that the initial position of each number is the corresponding positon on number axis.

Then what we have to do is to make the minimum and maximum number of meet in minimum time.

We can operate these numbers as flow:

Each operation, we can select two adjacent numbers on the number axis and exchange them.multiple operations can be performed at the same time.

the time it spend is the distance between two position.

When the minimum and maximum number went to the adjacent positions,the time of they meet is distance/2.

Here is an example:

1..4......11....16..19

We want 1 and 19 meet.

Let 1,4 and 16,19 exchange, they can be performed at the same time and end at the same time.It spend 3 units time,and becomes the following:

4..1......11....19..16

Then there are two Plan:

A Plan:

let 1 and 11 exchange with 7 units time first.And it becomes the following:

4..11......1....19..16

Then exchange 1 and 19 with 2.5 units time.

the total times is:3+7+2.5=12.5

Plan B:

Let 11 and 19 exchange with 5 units time first.it becomes the following:

4..1......19....11..16

let 1 and 19 exchanged with a time of 3.5 units.

total:3+5+3.5=11.5

Clearly, B Plan work better.

Now everyone seems to understand the meaning of the problem.

So the question now is like the following

at the beginning, there are N sets, the i-th set contains and contains only i.

Then N-1 operations flow:

Merge two set.

And you should calculate the minimum time of making the biggest and smallest number meet after merging in this set.

Input
The first line contains a number T(T<=5), indicating T cases of data.

for each case of data:

The first line contains a number N.

Then N-1 lines flow,

each line contains two different integers u, v.

they Represents we merge two set where u and v are.

we promise that u and v are not at the same set.

  • 1≤N≤300000

Output
for each case of data:
for each line, output the minimum time after merging, accurated to 0.1.

Sample Input
1
3
1 2
1 3

Sample Output
0.5
1.5

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。