【POJ 3270】Cow Sorting(置换群排序)
来源:互联网 发布:罗塞塔石碑软件下载 编辑:程序博客网 时间:2024/06/06 05:50
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
Lines 2..N+1: Each line contains a single integer: line i+1 describes the grumpiness of cow i.
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
题目大意:
John的牧场里有n头牛,每头牛有一个等级level,保证没有相同等级的牛。
已知交换等级为a,b的两头牛的花费为a+b
问最少需要多少花费,能把n头牛排为从左往右等级递增的序列?
原来置换还可以这么玩!涨姿势
首先考虑把原始序列变成几个置换。如
3 4 1 5 2 -> (31)(425)
已知置换间是不相影响的,那么对于每个置换,最少需要(len-1)次即可全部归位(len为置换长度)。那么拿这个置换中最小值min将其余牛归位花费一定是最小,为sum+min*(len-1)
除此之外还有一种情况可能更优:
取出所有牛中等级最低的small,与当前置换中的最小值min交换位置,花费small+min,其实也就是互换所属置换,然后将除其外的牛排序,花费为small*(len-1)+(sum-min)
然后与min互换,花费small+min
总花费small*(len+1)+sum+min
两种情况取最小即可。
至于处理序列,我排序后标记每个牛应在的位置,然后每个置换都单独搞一下就好。
代码如下:
#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define Pr pair<int,int>#define fread() freopen("in.in","r",stdin)#define fwrite() freopen("out.out","w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int msz = 10000;const int mod = 1e9+7;const double eps = 1e-8;struct Group{ int len,mn,sum;};struct Moon{ int level,id; bool operator <(const struct Moon a)const { return level < a.level; }};Moon mn[10010];Group gp[10010];int tp;int num[10010];int val[10010];bool vis[10010];void solve(int pos,int id){ gp[tp].mn = INF; gp[tp].len = gp[tp].sum = 0; while(!vis[id]) { vis[id] = 1; gp[tp].len++; gp[tp].mn = min(gp[tp].mn,val[id]); gp[tp].sum += val[id]; id = num[id]; }}int main(){ //fread(); //fwrite(); int n; scanf("%d",&n); for(int i = 0; i < n; ++i) { scanf("%d",&mn[i].level); mn[i].id = i; } sort(mn,mn+n); for(int i = 0; i < n; ++i) { num[mn[i].id] = i; val[i] = mn[i].level; } tp = 0; int mn = INF; memset(vis,0,sizeof(vis)); for(int i = 0; i < n; ++i) { if(vis[i]) continue; solve(tp,i); mn = min(mn,gp[tp++].mn); } int ans = 0; for(int i = 0; i < tp; ++i) { ans += min( gp[i].mn*(gp[i].len-1)+gp[i].sum-gp[i].mn, mn*(gp[i].len+1)+gp[i].mn+gp[i].sum ); } printf("%d\n",ans); 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 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(置换群问题)
- 记一次Marathon被黑过程
- TOJ 1407. Paths on a Grid(组合数)
- leetcode 121. Best Time to Buy and Sell Stock
- Git查看工程的分支
- AndroidStudio用微信官方方法接入微信分享功能
- 【POJ 3270】Cow Sorting(置换群排序)
- 用数组做一个数三退一的游戏
- Solr6.1 + Tomcat8 + mysql5 安装部署
- weblogic linux 下安装 Unrecognized option: -jrockit 错误
- MFC向导页面的补充介绍
- Regular Triangles
- Android群英传 第一章-体系与系统架构
- 路由器如何架设VPN:两大解决方案
- Android内存泄漏检测利器:LeakCanary