poj Cow Sorting 3270 (置换群)好题
来源:互联网 发布:通州淘宝城地址 编辑:程序博客网 时间:2024/06/06 14:21
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 ofX+Y units of time to exchange two cows whose grumpiness levels areX and Y.
Please help FJ calculate the minimal time required to reorder the cows.
Input
Lines 2..N+1: Each line contains a single integer: line i+1 describes the grumpiness of cowi.
Output
Sample Input
3231
Sample Output
7
Hint
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 10010using namespace std;int a[N];int c[N];struct zz{int id;int num;}p[N];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;}
- poj Cow Sorting 3270 (置换群)好题
- POJ 3270 Cow Sorting (置换群)
- poj 3270 Cow Sorting(置换群)
- poj-3270-Cow Sorting-置换群
- poj 3270 Cow Sorting (置换群)
- poj 3270 Cow Sorting (置换群)
- POJ 3270-Cow Sorting(置换群)
- POJ Cow Sorting 3270【哈希表+置换群】
- POJ 3270 - Cow Sorting【置换群】
- POJ 3270 Cow Sorting(置换群)
- poj-3270 Cow Sorting 置换群
- POJ 3270 Cow Sorting(置换环)
- poj 3270 Cow Sorting(置换)
- poj 3270 Cow Sorting(置换)
- POJ 3270 Cow Sorting(置换)
- POJ 3270 Cow Sorting(置换)
- 【POJ 2370】Cow Sorting(置换群)
- POJ 3270 Cow Sorting(置换群问题)
- C++智能指针简单剖析
- Android中SheardPreference的使用
- ubuntu 快捷键
- NSRunLoop
- nodejs Util
- poj Cow Sorting 3270 (置换群)好题
- 《C语言程序设计(第二版新版)》第二章习题解答(部分)
- Java中@Override的作用
- unity-场景重置,游戏对象的初始化改变以及复制
- PAT-A1006 Sign In and Sign Out(25)(模拟)
- sql语句中in后面的数据过多报错,或效率低
- java asm库的原理与使用方法(一)
- 兔子繁殖问题即斐波那契数列的java实现
- [GDKOI2016]染色大战