标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6750 | Accepted: 2633 |
Description
Farmer John‘s N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage FJ‘s milking equipment, FJ would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (not necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes FJ a total of X+Y units of time to exchange two cows whose grumpiness levels are X and Y.
Please help FJ calculate the minimal time required to reorder the cows.
Input
Output
Sample Input
3 2 3 1
Sample Output
7
Hint
Source
输入
line1行:一个整数:N
line2行:N + 1:每行包含一个整数:line+ 1描述了牛一、暴躁
输出
line 1:一个需要在暴躁的情绪增加的顺序排列的最小时间行牛。
样例解释
3 1 2:初始订单。
2 1 3:交换后奶牛暴躁3和1(时间= 1 + 3 = 4)。
1 2 3:交换后奶牛暴躁1和2(时间= 2 + 1 = 3)。
解析思路
同:给你一列数,需要将这些数按升序排列。你可以每次交换任意两个数的位置,而一次交换的代价被定义成交换的两个数的和。写一程序,用最小代价来完成这项无聊的排序工作。
先排序。。 找出每个数的位置 ,。,。,, 我们就 需要将其两两交换。。这里也就形成了一个置换群、、。。
这里有些不同就是 我们需要一个中间量去 完成这整个的置换。。。
要是用 这个群中的最小的 则 花费 sum(表示置换群中的所有数之和)+(k-2)*min k表示 置换群中数的个数
我们又想到,, 能不能用这个数列中的 最小值 参与这个置换
代价就是 sum+min+(k+1)*totmin
则 最终的式子就是 ans=sum(整个数列的和)+sigma(min((k-2)*mini,(k+1)*min+mini));
代码
代贴
标签:
原文地址:http://www.cnblogs.com/shenben/p/5458624.html