POJ-3270 Cow Sorting
来源:互联网 发布:数据库设计与开发规范 编辑:程序博客网 时间:2024/05/18 02:58
Cow Sorting
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6847 Accepted: 2684
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).
给出一列数,求将这列数排成升序的最小花费,这里花费定义为交换两个数的和。
例如给出一排数8 4 5 3 2 7,那么我们最终的状态为2 3 4 5 7 8,这里的轮换有(8 2 7)(4 5 3),这里8应该在第六个位置,
而第6个位置是7,7应该在5这个位置,而第5个位置为2,2应该在1这个位置,这样就到了8所在的位置,我们称这是一个轮换。
首先需要明确的一点是对于每个群,假设有k个数,那么我们需要交换k-1次得到升序。
对于每一个群,我们有两种换发:
1.群里换,拿群里最小的数t与其他每个数交换,共k-1次,花费为:sum+(k-2)*t.
2.将这个数列最小的数m,拉入这个群,与该群最小的数t交换,然后用这个最小的数与其他数交换k-1次,然后再将m与t换回来,这样
花费为:sum+t+(k+1)m
那么最小花费我们取两者中最小的,即sum+min{(k-2)*t,t+(k+1)*m}.
对于这个题关键就是怎么确定每个群,我们利用计数排序,得到每个数应该在的位置,然后循环判断这个位置是否已经被访问了。时间
复杂度为O(n).
代码如下:
#include <iostream>#include <algorithm>#include <cstdio>#include <math.h>#include <string.h>using namespace std;const int N=100001;const int INF=100000000;int m[N],cnt[N+1];bool used[N+1];int main(){ int n; while(~scanf("%d",&n)) { int i,j,tmp1,tmp2,len,t,maxValue,minValue,sum=0,curSum; memset(cnt,0,sizeof(cnt)); memset(used,0,sizeof(used)); minValue=INF; maxValue=0; for(i=1;i<=n;++i) //计数,得到每个数的位置,我们按照上面的那排数 { scanf("%d",m+i); cnt[m[i]]++; if(m[i]<minValue) minValue=m[i]; if(m[i]>maxValue) maxValue=m[i]; } for(i=1;i<=maxValue;++i) cnt[i]=cnt[i]+cnt[i-1]; for(i=1;i<=n;++i) { if(!used[i]) { j=i; t=m[i]; len=0; curSum=0; while(!used[j]) //每一组的循环,也就是群置换的位置 { ++len; if(m[j]<t) t=m[j]; curSum+=m[j]; used[j]=true; j=cnt[m[j]]; } if(1<len) sum+=curSum; if(2<len) { tmp1=(len-2)*t; tmp2=(len+1)*minValue+t; if(tmp1>tmp2) tmp1=tmp2; sum+=tmp1; } } } printf("%d\n",sum); } return 0;}
- 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 3270 Cow Sorting
- POJ-3270 Cow Sorting
- POJ 3270 Cow Sorting (置换)
- POJ 3270 / HDU 2838 - Cow Sorting
- POJ 3270 Cow Sorting(置换循环节)
- poj 3270 Cow Sorting(置换群)
- poj-3270-Cow Sorting-置换群
- POJ 3270 Cow Sorting 置换应用
- Cow Sorting && Permutations(poj 3270 && 2369)
- poj 3270 Cow Sorting (置换群)
- Linux makefile 教程 非常详细,且易懂
- php生成艺术签名 下
- 状压DP
- POJ 3468 A Simple Problem with Integers -
- hdu 1426 Sudoku Killer(数独 dfs)
- POJ-3270 Cow Sorting
- 杭电ACM 2087
- C++延时循环的创建
- 开发日记——手机安全卫士 Day02 2016-7-21 完成导航页面和切换功能
- Java程序员们最常犯的10个错误(转载)
- LeetCode-94:Binary Tree Inorder Traversal
- HDU 1045 Fire Net dfs
- Python.Calling a JSON API
- opencv初体验