poj3270

Cow Sorting
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6750   Accepted: 2633

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(1≤N≤10000)奶牛排队挤奶是在晚上。每头奶牛都有一个独特的“暴躁”的水平范围在1…100000。因为脾气暴躁的奶牛更容易损伤FJ的挤奶设备、FJ想订货的牛线所以他们列队在暴躁的情绪增加的顺序。在这个过程中,任何两个牛的地方(不一定是相邻的)可以互换。因为脾气暴躁的牛很难移动,以福建共计x + y单位时间交换两头牛的暴躁水平X和Y请帮助FJ计算所需的最小时间排序的奶牛。

输入

line1行:一个整数:N
line2行:N + 1:每行包含一个整数:line+ 1描述了牛一、暴躁

输出

line 1:一个需要在暴躁的情绪增加的顺序排列的最小时间行牛。

样例解释

3 1 2:初始订单。
2 1 3:交换后奶牛暴躁3和1(时间= 1 + 3 = 4)。
1 2 3:交换后奶牛暴躁1和2(时间= 2 + 1 = 3)。

 

解析思路

同:给你一列数,需要将这些数按升序排列。你可以每次交换任意两个数的位置,而一次交换的代价被定义成交换的两个数的和。写一程序,用最小代价来完成这项无聊的排序工作。

提示:本题目的意思为将一组无序数据转化成有序从小到大数列。并且,
每一步只能交换两个数据,每一步付出的代价就是两个数据之和。
题目看似杂乱无章,其实倒有规律可循。
本题目讨论可分成两种情况
例1: 3 5 1 6 7 8 最终要获得是1 3 5 6 7 8
这数列可以分成两个循环链( 3 5 1)(6 7 8)
而交换的最小情况就是每个数都与最小的交换
所以一次循环得到的值为:(t-2)*min+sum;
例2:1 8 6 7,我们很清楚的看到,如果再用例1的方法解决问题是就比较大了
所以我们也可以选择其他循环群的最小值和这里面的数进行交换,最后在换回来
所以一次循环得到的值就是min1+min*(t+1)+sum;

则 最终的式子就是 ans=sum(整个数列的和)+sigma(min((k-2)*mini,(k+1)*min+mini));

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 101000
 7 int a[N],b[N],c[N];
 8 bool d[N];
 9 int n,ans=0,mi;
10 int main(){
11     //freopen("sh.txt","r",stdin); 
12     mi=0x7f;
13     scanf("%d",&n);
14     for(int i=0;i<n;i++){
15        scanf("%d",a+i);
16        b[i]=a[i];
17        mi=min(mi,a[i]);
18        c[a[i]]=i;//记录原来数字的下标 
19     }
20     sort(b,b+n);//调整成正确的队列便于比较
21     memset(d,0,sizeof(d));
22     while(n){
23         int sum=0,t=0,i=0,mmm=0x7f;
24         while(d[i]) i++;
25         int beg=i;
26         do{
27             t++;
28             d[i]=1;
29             mmm=min(mmm,a[i]);
30             sum+=a[i];
31             i=c[b[i]];
32         }while(i!=beg);
33         n-=t;
34         if(t==1)continue;
35         int v1=(t-2)*mmm;
36         int v2=mmm+(t+1)*mi;
37         ans+=min(v1,v2);//累加最小次数 
38         ans+=sum;
39     }    
40     printf("%d\n",ans);
41     return 0;
42 }

 

 

 

 

posted @ 2016-05-04 15:35  神犇(shenben)  阅读(670)  评论(0编辑  收藏  举报