poj Cow Sorting 3270 (置换群)好题
2016-03-01 22:04
232 查看
Cow Sorting
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
Sample Output
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
题意:Farmer John有N头牛(1 ≤ N ≤ 10000),这N头牛都很各应,各自有一个不同的脾气脾气指数L(1 ≤ L ≤ 100000),这N头牛按脾气指数是无序排列,指数越大的越容易破坏farmer的挤奶器,所以farmer为了保护他的设施,要对这些牛按脾气指数递增的顺序排列,但交换两头牛的代价是这两头牛的脾气指数只和,现在告诉你牛的个数N和N头牛的脾气指数Li,求最小代价。
思路:http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.html
有两种策略:
第一种策略:sum1=(L1+Min(L))+(L2+Min(L))+...+(Lm-1+Min(L)) 其中:sum1为总花费,Li为此环中的牛脾气质数,除去那个极小的一共m-1个,Min(L)为脾气最小的牛。
整理一下得到:sum1=sum(L)+(m-2)*Min(L)
第二种策略:sum2=(L1+MIN)+(L2+MIN)+...+(Lm-1+MIN)+2*(Min(L)+MIN) 其中MIN为N头牛中脾气最小的
整理一下得到:sum2=sum(L)+Min(L)+(m+1)*MIN
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6661 | Accepted: 2600 |
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
题意:Farmer John有N头牛(1 ≤ N ≤ 10000),这N头牛都很各应,各自有一个不同的脾气脾气指数L(1 ≤ L ≤ 100000),这N头牛按脾气指数是无序排列,指数越大的越容易破坏farmer的挤奶器,所以farmer为了保护他的设施,要对这些牛按脾气指数递增的顺序排列,但交换两头牛的代价是这两头牛的脾气指数只和,现在告诉你牛的个数N和N头牛的脾气指数Li,求最小代价。
思路:http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.html
有两种策略:
第一种策略:sum1=(L1+Min(L))+(L2+Min(L))+...+(Lm-1+Min(L)) 其中:sum1为总花费,Li为此环中的牛脾气质数,除去那个极小的一共m-1个,Min(L)为脾气最小的牛。
整理一下得到:sum1=sum(L)+(m-2)*Min(L)
第二种策略:sum2=(L1+MIN)+(L2+MIN)+...+(Lm-1+MIN)+2*(Min(L)+MIN) 其中MIN为N头牛中脾气最小的
整理一下得到:sum2=sum(L)+Min(L)+(m+1)*MIN
#include<stdio.h> #include<string.h> #include<algorithm> #define INF 0x3f3f3f3f #define ll long long #define N 10010 using namespace std; int a ; int c ; struct zz { int id; int num; }p ; int cmp(zz a,zz b) { return a.num<b.num; } int main() { int n,i,j; while(scanf("%d",&n)!=EOF) { int mm=INF; int sum=0; for(i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; p[i].num=a[i];p[i].id=i; if(a[i]<mm) mm=a[i]; c[i]=i; } sort(p+1,p+n+1,cmp); for(i=1;i<=n;i++) { int t; if(a[i]) { int cnt=1; t=a[i]; int d=p[c[i]].id; if(a[d]<t) t=a[d]; while(d!=i) { cnt++; d=p[d].id; if(a[d]<t) t=a[d]; } int v=(cnt-2)*t; int w=(cnt+1)*mm+t; sum+=v<w?v:w; a[i]=0; } } printf("%d\n",sum); } return 0; }
相关文章推荐
- java.lang.IllegalArgumentException: Page directive: invalid value for import
- 核电站
- QT与泰剧
- Gradle之dependencies
- PAT-A1036 Boys vs Girls(25) (模拟)
- MyEclipse使用总结——在MyEclipse中设置jsp页面为默认utf-8编码
- Poj 2388 Who's in the Middle【排序】
- 兔子繁殖问题即斐波那契数列的java实现
- Java 虚拟机与 Dalvik 虚拟机的区别
- 染色大战
- PAT-A1006 Sign In and Sign Out(25)(模拟)
- sql语句中in后面的数据过多报错,或效率低
- unity-场景重置,游戏对象的初始化改变以及复制
- String类为什么被定义为final类型
- Java中@Override的作用
- 在CSDN查阅资料呢么久了,感觉还是自己开一个博客吧!
- CSS3弹性布局内容对齐(justify-content)属性使用详解
- Raft 为什么是更易理解的分布式一致性算法
- 编译mod_jk.so
- 复制文件方式新建虚拟机