码迷,mamicode.com
首页 > 其他好文 > 详细

poj3270

时间:2016-05-04 15:47:07      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

Cow Sorting
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

Line 1: A single integer: N.
Lines 2..N+1: Each line contains a single integer: line i+1 describes the grumpiness of cow i.

Output

Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.

Sample Input

3
2
3
1

Sample Output

7

Hint

2 3 1 : Initial order.
2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4).
1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).

Source

翻译:
        牛排序
描述
  农民约翰的N(1≤N≤10000)奶牛排队挤奶是在晚上。每头奶牛都有一个独特的“暴躁”的水平范围在1…100000。因为脾气暴躁的奶牛更容易损伤FJ的挤奶设备、FJ想订货的牛线所以他们列队在暴躁的情绪增加的顺序。在这个过程中,任何两个牛的地方(不一定是相邻的)可以互换。因为脾气暴躁的牛很难移动,以福建共计x + y单位时间交换两头牛的暴躁水平X和Y请帮助FJ计算所需的最小时间排序的奶牛。

输入

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));

代码 

代贴

poj3270

标签:

原文地址:http://www.cnblogs.com/shenben/p/5458624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!