POJ 3270 Cow Sorting(置换群)
2013-07-22 16:31
357 查看
题目链接
很早之前就看过这题,思路题把,确实挺难想的,黑书248页有讲解。
很早之前就看过这题,思路题把,确实挺难想的,黑书248页有讲解。
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> using namespace std; int p[10001]; struct node { int id; int x; int pos; }num[10001]; int flag[10001],minz; int cmp1(node a,node b) { return a.x < b.x; } int cmp2(node a,node b) { return a.id < b.id; } int judge(int x) { int res = 0,m,temp = 0; if(x == num[x].pos) { flag[x] = 1; return 0; } m = num[x].x; while(!flag[x]) { flag[x] = 1; temp += num[x].x; x = num[x].pos; m = min(m,num[x].x); res ++; } return temp + min((res-2)*m,m+(res+1)*minz); } int main() { int n,i,sum; scanf("%d",&n); minz = 100000000; sum = 0; for(i = 1;i <= n;i ++) { scanf("%d",&num[i].x); minz = min(num[i].x,minz); num[i].id = i; } sort(num+1,num+n+1,cmp1); for(i = 1;i <= n;i ++) { num[i].pos = i; } sort(num+1,num+n+1,cmp2); for(i = 1;i <= n;i ++) { if(!flag[i]) { sum += judge(i); } } 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 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 (置换)
- POJ3270 置换群
- 【解题报告】POJ 3270 Cow 置换群基础 -- 轮换