poj-3270 Cow Sorting 置换群

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

Cow Sorting
Time Limit: 2000MS Memory Limit: 65536K
Total 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

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)   花费为: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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值