A - Cow Sorting
来源:互联网 发布:windows手机应用 编辑:程序博客网 时间:2024/06/10 17:14
A - Cow Sorting
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uSubmit
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.
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
3231
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).
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).
提示:本题目的意思为将一组无序数据转化成有序从小到大数列。并且,
每一步只能交换两个数据,每一步付出的代价就是两个数据之和。
题目看似杂乱无章,其实倒有规律可循。
本题目讨论可分成两种情况
例1: 3 5 1 6 7 8 最终要获得是1 3 5 6 7 8
这数列可以分成两个循环链( 3 5 1)(6 7 8)
而交换的最小情况就是每个数都与最小的交换
所以一次循环得到的值为:(t-2)*min+sum;
例2:1 8 6 7,我们很清楚的看到,如果再用例1的方法解决问题是就比较大了
所以我们也可以选择其他循环群的最小值和这里面的数进行交换,最后在换回来
所以一次循环得到的值就是min1+min*(t+1)+sum;
代码实现就如下面:
<span style="font-size:24px;">#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a,i; int asoc[100010],soc[100010]; int sd[100010],vis[100001];int main(){ int min=1<<25; scanf("%d",&a); for(i=0;i<a;i++) { scanf("%d",&soc[i]); asoc[i]=soc[i]; if(soc[i]<min) min=soc[i]; sd[soc[i]]=i; } sort(asoc,asoc+a);//调整成正确的队列便于比较 int length=a; memset(vis,0,sizeof(vis)); int ans=0; while(length) { int sum=0; int t=0; int i=0; int mmm=1<<25;//定义最小值 while(vis[i])i++; int beg=i; while(1!=0) { t++; vis[i]=1; if(soc[i]<mmm) mmm=soc[i]; sum+=soc[i]; i=sd[asoc[i]]; if(i==beg) break; } length=length-t; if(t==1) continue; int v1=(t-2)*mmm; int v2=mmm+(t+1)*min; ans+=(v1<v2)?v1:v2; ans+=sum; } printf("%d\n",ans); return 0;}</span>
0 0
- A - Cow Sorting
- cow sorting
- Cow Sorting
- pku3270 Cow Sorting
- poj 3270 Cow Sorting
- POJ Cow Sorting 置换
- hdu 2838 Cow Sorting
- Cow Sorting----置换
- pku3270 Cow Sorting
- POJ 3270(Cow Sorting)
- Cow Sorting (树状数组)
- hdu2838 Cow Sorting
- [POJ3270]Cow Sorting
- poj 3270 Cow Sorting
- hdu 2838 Cow Sorting
- POJ 3270 Cow Sorting
- Cow Sorting(置换)
- POJ 3270 Cow Sorting
- JAVA实现TCP通讯
- 寒假之作—HDU—最受欢迎的颜色问题
- iOS开发-缓存图片到沙盒
- Ubuntu平台下OpenCV官方教程之how_to_scan_images.cpp例程的编译
- printk()
- A - Cow Sorting
- 【JavaScript】对数的判断与对数的处理
- Android UI开发详解之ActionBar
- 六角填数
- 将一个文件拷到沙盒
- Android菜单详解(五)——使用XML生成菜单
- MYSQL : "THE USER SPECIFIED AS A DEFINER ('ROOT'@'%') DOES NOT EXIST"
- 财务组织与会计核算
- C99 中的37个关键字