Cow Sorting
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7309   Accepted: 2874

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

USACO 2007 February Gold
题目意思:
计算使得N个数有序的最小交换次数。
解题思路【 (๑ŐдŐ)b以下思路 参考原出处不详】

然后对于一个循环i,设它的长度是ki,那么它至少要交换ki-1次,即每次让一个元素到达目标位置。既然交换次数一定,那么要使交换的代价最小,就是每次都拿循环里最小的元素ti与其他元素交换。根据这些知识,我们得知解决原题,有两种方案:

1.对于每个循环,都那该循环里最小的元素ti与其他元素交换,那么共花费 sumi + (ki-2)*ti,(sumi是该循环个元素之和)

2.让ti先和n个元素中最小的元素m交换,让m进入该循环,并和剩下的ki-1个元素依次交换把他们送入目标位置,然后再让m和ti交换,ti退出该循环。这样共花费 sumi + ti + (ki+1)*m

综上:所有循环都交换完所需的最小花费cost = sum + ∑min{ (ki-2)*ti , ti + (ki+1)*m };

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define MAXN 100010
int a[MAXN],b[MAXN],s[MAXN],t[MAXN],x[MAXN];
//原数组、置换群、排序数组、循环节、一个置换群内对应的原数组元素
bool vis[MAXN];
int main()
{
#ifdef ONLINE_JUDGE
#elsefreopen("G:/cbx/read.txt","r",stdin);//freopen("G:/cbx/out.txt","w",stdout);
#endifint n;while(~scanf("%d",&n)){memset(vis,false,sizeof(vis));memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(s,0,sizeof(s));memset(x,0,sizeof(x));int sum=0;for(int i=0; i<n; ++i){scanf("%d",&a[i]);s[i]=a[i];sum+=a[i];//计算所有元素的和}int ans=0;sort(s,s+n);int *m=min_element(a,a+n);//找出所有元素的最小值/* for(int i=0; i<n; ++i)cout<<s[i]<<" ";cout<<endl;*/int cnt=0;while(1)//计算置换群{if(cnt==n) break;for(int i=0; i<n; ++i)if(a[cnt]==s[i]){b[cnt]=i;++cnt;break;}}/* for(int i=0; i<n; ++i)cout<<b[i]<<" ";cout<<endl;*/while(1)//分别处理每个循环节{int cnt=0,i;for(i=0; i<n; ++i)if(!vis[i]){vis[i]=true;t[cnt++]=i;break;}if(i==n) break;//处理完毕int temp=b[t[cnt-1]];while(!vis[temp]){vis[temp]=true;t[cnt++]=temp;temp=b[t[cnt-1]];}for(int i=0; i<cnt; ++i)x[i]=a[t[i]];//对应原数组元素/*for(int i=0; i<cnt; ++i)cout<<x[i]<<" ";cout<<endl;*/int *p=min_element(x,x+cnt);ans+=min(((cnt-2)*(*p)),((*p)+(cnt+1)*(*m)));}printf("%d\n",ans+sum);}return 0;
}

POJ 3270-Cow SortingA(Polya定理-交换次数)相关推荐

  1. [POJ 3270]Cow Sorting

    Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...

  2. 【POJ 1286】Necklace of Beads(polya定理)

    [POJ 1286]Necklace of Beads(polya定理) Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  3. POJ 2409 Let it Bead (Polya定理)

    题意 用k种颜色对n个珠子构成的环上色,旋转翻转后相同的只算一种,求不等价的着色方案数. 思路 Polya定理 X是对象集合{1, 2, --, n}, 设G是X上的置换群,用M种颜色染N种对象,则不 ...

  4. poj 2409 polya定理

    polya定理的入门题 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using ...

  5. 置换群Polya定理(poj 2409: Let it Bead)

    可以先看 置换群burnside引理(bzoj 1004: [HNOI2008]Cards) Polya定理公式(必须在没有限制下才能使用此公式): 其中|G|为总置换数,m表示可用的颜色数,c(gi ...

  6. pku 3270 Cow Sorting 置换群

    http://poj.org/problem?id=3270 题意: 给定N头牛的身高,要求你通过每次交换两头牛的位置使其按身高从小到大排序,身高各不相同.假设交换ai,aj两头牛的位置则花费的时间为 ...

  7. ACM:nbsp;polya定理+hashnbsp;数论题nbsp;pojnbsp;3…

    Cow Sorting Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the eveni ...

  8. 置换群和Burnside引理,Polya定理

    定义简化版: 置换,就是一个1~n的排列,是一个1~n排列对1~n的映射 置换群,所有的置换的集合. 经常会遇到求本质不同的构造,如旋转不同构,翻转交换不同构等. 不动点:一个置换中,置换后和置换前没 ...

  9. poj2154 Color ——Polya定理

    题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...

最新文章

  1. 插上翅膀,让Excel飞起来——xlwings(一)
  2. python post请求参数为list_浅谈python3发送post请求参数为空的情况
  3. 在 wxWidgets 中播放声音的示例
  4. Python 文件读写操作-Python零基础入门教程
  5. c# datetime._C#| 带示例的DateTime.DayOfWeek属性
  6. bootstrap实现表格
  7. Shell脚本中的并发(转)
  8. Flutter之跨组件状态共享Provider框架剖析(2)
  9. java api在jdk哪里,jdk api文档在哪里
  10. bootcamp opencore_macbook pro用启动转换助理(bootcamp)安装win10踩的坑 | ZPY博客
  11. 认识VBA------------------VBA基础
  12. WinRAR下载官方免费版
  13. 《Python助力交通》公众号说明
  14. iOS 开发 申请299美金企业开发账号
  15. 计算机网络共享打不开,电脑只要打开共享提示“无法启用共享访问”如何解决...
  16. 项目笔记 【调查问卷】
  17. 熟悉又陌生的 k8s 字段:finalizers
  18. yun2win即时通讯云-最安全的即时通讯云服务
  19. 【信号处理】基于优化算法的 SAR 信号处理(Matlab代码实现)
  20. 毕业1年半,我从月薪3000变成百万粉丝博主 | 附野生运营技巧

热门文章

  1. IOS中Socket详解
  2. 全网最全的Apollo教程从入门到精通
  3. 安卓Fragment使用详解
  4. 【前端之路】前端技术简介,大前端的发展史
  5. java中在方法体内抛出异常_Java异常处理中___用于方法体内,抛出一个异常对象。...
  6. 保研资料 | 行为面试题回答思路50道来啦!
  7. oracle这么导入,Oracle怎么导入数据
  8. Audio系列之静音控制
  9. arm64的ioremap_ioremap函数解析
  10. 程序没问题,运行报错:Directory not empty