Cow Sorting(置换群)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cow Sorting(置换群)相关的知识,希望对你有一定的参考价值。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6664 | Accepted: 2602 |
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
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<queue> using namespace std; const int INF=0x3f3f3f3f; #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define PI(x) printf("%d",x) typedef long long LL; const int MAXN=10010; int vis[MAXN]; struct Node{ int pos,val; bool operator < (const Node &b)const{ if(val!=b.val)return val<b.val; else return pos<b.val; } }; Node dt[MAXN]; int main(){ int N; while(~SI(N)){ LL sum=0; int min_all=INF,min_area; for(int i=1;i<=N;i++){ SI(dt[i].val); dt[i].pos=i; sum+=dt[i].val; min_all=min(min_all,dt[i].val); } sort(dt+1,dt+N+1); mem(vis,0); int num; for(int i=1;i<=N;i++){ if(!vis[i]){ num=0; min_area=dt[i].val; int j=i; while(!vis[j]){ vis[j]=1; num++; //printf("%d ",dt[j].val); min_area=min(min_area,dt[j].val); j=dt[j].pos; }//puts(""); sum+=min((num-2)*min_area,2*(min_all+min_area)+(num-1)*min_all-min_area); } } printf("%lld\n",sum); } return 0; }
以上是关于Cow Sorting(置换群)的主要内容,如果未能解决你的问题,请参考以下文章
BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序
bzoj 1119 [POI2009]SLO && bzoj 1697 [Usaco2007 Feb]Cow Sorting牛排序——思路(置换)