POJ 3270-Cow SortingA(Polya定理-交换次数)

Cow Sorting
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7309 Accepted: 2874

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).

Source


题目意思:
计算使得N个数有序的最小交换次数。

解题思路【 (๑ŐдŐ)b以下思路 参考原出处不详

然后对于一个循环i,设它的长度是ki,那么它至少要交换ki-1次,即每次让一个元素到达目标位置。既然交换次数一定,那么要使交换的代价最小,就是每次都拿循环里最小的元素ti与其他元素交换。根据这些知识,我们得知解决原题,有两种方案:

1.对于每个循环,都那该循环里最小的元素ti与其他元素交换,那么共花费 sumi + (ki-2)*ti,(sumi是该循环个元素之和)

2.让ti先和n个元素中最小的元素m交换,让m进入该循环,并和剩下的ki-1个元素依次交换把他们送入目标位置,然后再让m和ti交换,ti退出该循环。这样共花费 sumi + ti + (ki+1)*m

 

综上:所有循环都交换完所需的最小花费cost = sum + ∑min{ (ki-2)*ti , ti + (ki+1)*m };


#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define MAXN 100010
int a[MAXN],b[MAXN],s[MAXN],t[MAXN],x[MAXN];
//原数组、置换群、排序数组、循环节、一个置换群内对应的原数组元素
bool vis[MAXN];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/cbx/read.txt","r",stdin);
    //freopen("G:/cbx/out.txt","w",stdout);
#endif
    int n;
    while(~scanf("%d",&n))
    {
        memset(vis,false,sizeof(vis));
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(s,0,sizeof(s));
        memset(x,0,sizeof(x));
        int sum=0;
        for(int i=0; i<n; ++i)
        {
            scanf("%d",&a[i]);
            s[i]=a[i];
            sum+=a[i];//计算所有元素的和
        }
        int ans=0;
        sort(s,s+n);
        int *m=min_element(a,a+n);//找出所有元素的最小值
        /* for(int i=0; i<n; ++i)
             cout<<s[i]<<" ";
         cout<<endl;*/

        int cnt=0;
        while(1)//计算置换群
        {
            if(cnt==n) break;
            for(int i=0; i<n; ++i)
                if(a[cnt]==s[i])
                {
                    b[cnt]=i;
                    ++cnt;
                    break;
                }
        }

        /* for(int i=0; i<n; ++i)
             cout<<b[i]<<" ";
         cout<<endl;*/

        while(1)//分别处理每个循环节
        {
            int cnt=0,i;
            for(i=0; i<n; ++i)
                if(!vis[i])
                {
                    vis[i]=true;
                    t[cnt++]=i;
                    break;
                }
            if(i==n) break;//处理完毕
            int temp=b[t[cnt-1]];
            while(!vis[temp])
            {
                vis[temp]=true;
                t[cnt++]=temp;
                temp=b[t[cnt-1]];
            }
            for(int i=0; i<cnt; ++i)
                x[i]=a[t[i]];//对应原数组元素

               /*for(int i=0; i<cnt; ++i)
                   cout<<x[i]<<" ";
               cout<<endl;*/

            int *p=min_element(x,x+cnt);
            ans+=min(((cnt-2)*(*p)),((*p)+(cnt+1)*(*m)));
        }
        printf("%d\n",ans+sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值