poj-3270 Cow Sorting 置换群

来源:互联网 发布:javascript教程廖雪峰 编辑:程序博客网 时间:2024/06/05 16:56

http://poj.org/problem?id=3270

Cow Sorting
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 6875 Accepted: 2699

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

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).
置换群

题意:给出一个序列,每次交换两个,使序列变成递增序列,每次交换的花费值为交换这两数值,问按升序排列的最小花费值

例如: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;}



0 0
原创粉丝点击