poj-3270 Cow Sorting 置换群
来源:互联网 发布:javascript教程廖雪峰 编辑:程序博客网 时间:2024/06/05 16:56
http://poj.org/problem?id=3270
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).
题意:给出一个序列,每次交换两个,使序列变成递增序列,每次交换的花费值为交换这两数值,问按升序排列的最小花费值
例如:8 4 5 3 2 7 要变成2 3 4 5 7 8 有两个循环(8 2 7)(4 5 3) 花费为:2+8+2+7+3+4+3+5 = 34
每次交换取最小值,有两种方式:1、用循环里的最小值依次与循环里的其它元素交换记录花费值,此时花费为:元素总和+(循环的阶数即循环里元素的个数-2)*最小值。2、将整个序列的最小值与循环里的最小值交换,再用这个最小值依次与循环里的其它值交换,最后在把这个序列的最小值与循环里的最小值换回来,此时花费为:元素总和+原循环最小值+(循环阶数+1)*整个序列最小值
例:1 8 9 7 6要变成1 6 7 8 9 循环为(8 9 7 6)方式1花费为:6+8+6+9+6+7 = 42,方式2花费为:(1+6)*2+1+8+1+7+1+9 = 41
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int n, a[100005], mub[100005], c[100005], vis[100005];int main(){ int i, j, k, mmin, com, s, temp, ans, zhihuan, l; while(cin>>n){ memset(a, 0, sizeof(a)); memset(mub, 0, sizeof(mub)); memset(c, 0, sizeof(c)); memset(vis, 0, sizeof(vis)); for(i=1; i<=n; i++){ scanf("%d",&a[i]); mub[i] = a[i]; } sort(mub+1, mub+n+1); for(i=1; i<=n; i++) c[mub[i]] = i; mmin = mub[1]; //数列中最小的 ans = 0; for(i=1; i<=n; i++){ if(mub[i]==a[i]){ vis[i] = 1; continue; } if(vis[i]) continue; vis[i] = 1; com = mub[i]; s = mub[i]; temp = a[i]; zhihuan = mub[i]; //置换群里最小的 l = 1; while(temp!=com){ vis[c[temp]] = 1; s += temp; l++; if(temp<zhihuan) zhihuan = temp; temp = a[c[temp]]; } //cout<<" "<<l<<endl; int sum = s+zhihuan*(l-2); sum = min(sum, s+zhihuan+mmin*(l+1)); ans += 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 Cow Sorting 3270【哈希表+置换群】
- POJ 3270 - Cow Sorting【置换群】
- POJ 3270 Cow Sorting(置换群)
- poj-3270 Cow Sorting 置换群
- POJ 3270 Cow Sorting (置换群)
- POJ 3270 Cow Sorting (置换)
- POJ 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 (置换群利用) 位置交换问题
- broadcastreceiver中显示dialog以及重启当前activity
- fristChild
- Android 数据库ORM框架——GreenDao
- ubuntu16安装jdk1.8
- 关于zookeeper,可以看看这个作者写的zookeeper系列。
- poj-3270 Cow Sorting 置换群
- C#控制台基础 函数的参数是借口的实例
- 【linux】玩转awk 二
- [leetcode] 378. Kth Smallest Element in a Sorted Matrix
- CSDN博客日记一则
- QSqlQuery::value: not positioned on a valid record
- rmdir (删除“空”的目录)
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- C++中的friend详细解析