poj3270cow sorting 置换群裸题

3 篇文章 0 订阅

Cow Sorting
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6727 Accepted: 2620

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



题意:给定一组无序数列,让你两两交换其中的数,使其成单调增数列,每交换一次数都要消耗两数之和的体力,问你最少需要做少体力才能使其变成递增数列。

思路:考虑一个数列 4 6 2 3 5,其排完序后为2 3 4 5 6.可以发现其中(2,4)为一组,(3,5,6)为一组,各个数字都在组内进行交换。

所以费用最小的交换策略一定是这两种之一:

1.用组内最小的数依次交换其他数,其费用为sum(该群内的数总和)+(cnt-1)*min(该组内最小数)

2.用整个数列内的最小数与组内最小数交换,再用该最小数交换组内其他数,其费用为sum+2*min+2*minest+(cnt-1)*minest

对于每一个组,其费用为以上两种方案的较小值



关于寻找环:用一个数组value【x】来记录x在原序列中的位置,借此来跳转到排序后value【x】位置上的数y,可以知道y一定是与x一组的数,接着跳转到排序后数组中value【y】的位置上的数,以此类推可以找到所有与x在一组中的数。


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <functional>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int main(int argc, char const *argv[])
{
	int i,j,k,m,n;
	int val[100005],a[10004],b[10004],vis[100005];
    while(~scanf("%d",&n))
    { memset(val,0,sizeof(val));
      memset(vis,0,sizeof(vis));	
      long long sum=0;
      int min=10000006;
      for(i=1;i<=n;i++){scanf("%d",a+i);b[i]=a[i];if(min>a[i])min=a[i];val[a[i]]=i;}
      sort(b+1,b+n+1);
      
      for(i=1;i<=n;i++)
      if(vis[a[i]]==0)
      { int beg=a[i];
      	vis[beg]=1;
      	int x=a[i];
      	int sumx=x;
      	int smin=x;
      	int cnt=1;
        while(b[val[x]]!=beg)
        {cnt++;
         vis[b[val[x]]]=1;	
         sumx+=b[val[x]];
         if(smin>b[val[x]])smin=b[val[x]];
         x=b[val[x]];
        }
        if(sumx+(cnt-2)*smin<sumx+(cnt+1)*min+smin)sum+=sumx+(cnt-2)*smin;
          else sum+=sumx+(cnt+1)*min+smin;
      }
      printf("%lld\n",sum);
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值