polya定理

H - Cow Sorting
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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).
题意:这是一个排序问题,说的是一个数列排成从小到大的顺序,但是和其权值有关。
只能进行交换。例如一个序列(1,9,4,6,2),排列结束的情况就是(1,2,4,6,9),而
最初情况2和9的交换的值就是2+9=11.

在这种算法,有两种考虑。

1. ,比如说一个数列为(3,2,5,6,9,8);他们的排列结果为(2,3,5,6,8,9);(2,3,5)
(6,8,9)为两个不相互干扰的循环。他们独立完成各自循环。设他们的总长度为K,
对于一个循环。让每一位 回复原位,至少需要K-1次。让每次的轮换代价最少,当然
是让循环中最小的参与循环。 假设ti为最小的,其他元素只参与一次交换,
总花费为sum+(K-2)*ti;
2,但是有种情况是一个循环比较小,存在最小的数,例如(1,9,8,7,6);
两个循环就是(1)(9,8,7,6)这样算出的数就比较大;我们可让最小的m,和另一个
循环的 最小数ti交换,让他参与到循环中去,进行交换。得到的值为sum+ti+(K+1)*m;


而所得花费就是比较上面两种方法中的最小值:
具体情况看代码实现:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;//头文件
int b[100010],d[100010];
int i;
int a;
int siv[100010];
int c[100010],min;


int diaoyong()
{
    int sum=0;
         memset(siv,0,sizeof(siv));//初始化,清零
        for(i=0;i<a;i++)//对于数组中的数据,一个一个接入
        {
            if(siv[i])//如果,已经标记,会跳过,接着执行下一个数据
            continue;
            int t=i;
            int s=0;
            int first=b[i];
            int min=100000000;
            while(1)//查找一个独立的循环
            {
                siv[t]=1;
                sum+=b[t];//叠加的循环中的所有值
                if(min>b[t])
                min=b[t];//挑选出循环的最小值
                s++;
                if(first==d[t])//跳出循环
                break;
                t=c[d[t]];
            }
            if(min*(s-2)>min+(s+1)*d[0])//两种方法的大小判定。
            sum=sum+min+(s+1)*d[0];
            else
            sum=sum+min*(s-2);
        }
        return sum;
}
int main()
{
    while(~scanf("%d",&a))
    {
        for(i=0;i<a;i++)
        {
            scanf("%d",&b[i]);//接受原本输入的数据。
            c[b[i]]=i;//存储数据原来的位置,便于找到一个独立循环。
            d[i]=b[i];//用新的数组接受,便于等会的排序
        }

        sort(d,d+a);//将d数组变化成我们需要的状态。
        printf("%d\n",diaoyong());//调用无输入,又返回。
    }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值