POJ 3270 Cow Sorting(置换最小费用)

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


USACO 2007 February Gold
 
 
 
大致题意:Farmer John有N头牛(1 ≤ N ≤ 10000),这N头牛都很各应,各自有一个不同的脾气脾气指数L(1 ≤ L ≤ 100000),这N头牛按脾气指数是无序排列,指数越大的越容易破坏farmer的挤奶器,所以farmer为了保护他的设施,要对这些牛按脾气指数递增的顺序排列,但交换两头牛的代价是这两头牛的脾气指数只和,现在告诉你牛的个数N和N头牛的脾气指数Li,求最小代价。


这题跟黑书上的“无聊的排序”(P247)是一个题,而且这题目竟然是ACM/ICPC 02年Final的题啊我去,憋了很久想的思路竟然仅仅是半对的......,果断YM神题了。


思路:仔细读题,看到FARMER是两两交换牛的顺序进行排序的话,应该就往置换上靠拢,而这个题果然是置换的应用(有的解题报告上说是置换群,其实这只是单个置换,不用让它构成群)。我们来将这些无序的牛抽象成一个置换,一次移动就是对一对元素置换。举个例子:


                     


                      


那么我们用置换环表示应该是(1 6 5)(2 3 4)这样两个3阶环,也就是说这六头牛是两个无序子序列,每个子序列内部按坐标排好序则整体有序,既然要使代价最小,我们就应该让置换的次数尽量少,置换的两个数和尽量小。根据上面的划分,我们知道在一个长度为m的环中至少要移动m-1次使它有序,这样我们就能保证次数最少。那怎样使置换的数尽量小呢?我们知道,所有置换可以拆成对换(2阶环)乘积的形式,不妨将上面的环拆开,(1 6 5)可以拆成(1 6)(1 5),或者(6 5)(6 1),或者(5 1)(5 6),其实这就是3种置换方案。不难发现其中最优的方案是找出环中的极小元素将他分别和环中的其他元素置换一轮,形象点就是从这一波牛中找到那个脾气最小的当媒介,让它换脾气大的牛到相应的位置。貌似这个问题就解决了,我的思路也只是走到了这里。


但是这个思路是不完全正确的。如果给出的置换中每个环中的那个极小元素就是置换中的最小元素,那上面的思路肯定正确。但如果最小元素不在这个环里,那存不存在更优的策略?答案是存在的。试试1 8 9 7 6这组数据。将它划分成(8 6 9 7)(1),(没写出下标,直接按权划分的),那么我们把1和6交换,让1加入到前面的环中置换那另外三个大数,最后再把6换出来,发现得到的话费比上面给出的策略更优。也就是说置换的次数最少不一定能得到最优。那除了上面给出的两种策略还有其他的更优策略么?从贪心的角度分析不可能有更优了,所以我们只要对上面的两种策略中选择一个较小的即可。 给出两种方案的计算:


第一种策略:sum1=(L1+Min(L))+(L2+Min(L))+...+(Lm-1+Min(L))  其中:sum1为总花费,Li为此环中的牛脾气质数,除去那个极小的一共m-1个,Min(L)为脾气最小的牛。


整理一下得到:sum1=sum(L)+(m-2)*Min(L)


第二种策略:sum2=(L1+MIN)+(L2+MIN)+...+(Lm-1+MIN)+2*(Min(L)+MIN)  其中MIN为N头牛中脾气最小的


整理一下得到:sum2=sum(L)+Min(L)+(m+1)*MIN


 


每个环都按两种策略找到较小累加就是总最小话费。至于如何求循环节,用计数排序即可,因为计数排序能求出元素在正序下的下标,计数排序的时间复杂度是O(n+k)其中n是排序元素个数,k是元素最大值,计数排序是时间复杂度非常低的非比较排序算法,但是应用性比较差,不懂原理的可以维基百科,讲的很好。题目难度不小,数学+贪心的一道题目。


//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e4+10;
const int maxx=1e4+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline LL Scan()
{
	LL Res=0,ch,Flag=0;
	if((ch=getchar())=='-')Flag=1;
	else if(ch>='0' && ch<='9')Res=ch-'0';
	while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
	return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

struct node
{
    int id,num;
}Q[maxx];
int vis[maxx],n,minc;
bool cmp(node a,node b)
{
    return a.num<b.num;
}
void solve()
{
    me(vis,0);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            int l=Q[i].id;
            int sum=Q[i].num;
            int m=Q[i].num,cnt=1;
            while(l!=i)
            {
                vis[l]=1;
                cnt++;
                sum+=Q[l].num;
                m=min(m,Q[l].num);
                l=Q[l].id;
            }
            ans=ans+min(sum+(cnt-2)*m,sum+m+(cnt+1)*minc);
        }
    }
    cout<<ans<<endl;
}
int main()
{
    //freopen( "in.txt" , "r" , stdin );
    while(scanf("%d",&n)!=EOF)
    {
        minc=INF;
        for(int i=1;i<=n;i++)
        {
            Q[i].id=i;Q[i].num=Scan();
            minc=min(minc,Q[i].num);
        }
        sort(Q+1,Q+n+1,cmp);
        solve();
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值