Cow Sorting
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6664   Accepted: 2602

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).
题解:要把一个数列通过交换变成一个递增的序列,交换的权值是两个元素的和;
通过这n个数组成的数列 我们可以得到若干个置换子群。
            比如 给定序列{
                                            3  1  7  10  6
                                            1  3  6   7  10
                                        }上面序列要变成下面序列。 我们可以得到2个置换子群(1 -> 3)( 6 -> 10 -> 7)。
接下来有两种方法变换序列:
 
(1)  在子群里面置换 -> 用最小的元素sonMin和其它 t - 1 元素交换 t - 1 次,就有花费1:sum + (t - 2) * sonMin;  (2)  借助于外界元素置换 -> 先让数列中最小元素Minn和群中最小元素sonMin交换,再用Minn与其它 t - 1个元素交换 t - 1 次,然后把Minn和sonMin交换回来
       有花费2: sum + sonMin + Minn * (t + 1)。 提醒: sum为当前群的数值之和。因此可以先统计所有元素之和,然后每次加上min(花费1, 花费2)即可;
 
对于每个置换群的求法,可以用结构体存储原数值以及原数值对应的位置,然后按数值升序排列就求出变换的位置。
 
序列{                                                                                   {
              数值 3  1  7  10  6         ------>                                    数值 1  3  6  7 10
              位置 1  2  3   4   5         ------>                                    位置   2  1  5  3  4
          }             
代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=;
int vis[MAXN];
struct Node{
int pos,val;
bool operator < (const Node &b)const{
if(val!=b.val)return val<b.val;
else return pos<b.val;
}
};
Node dt[MAXN];
int main(){
int N;
while(~SI(N)){
LL sum=;
int min_all=INF,min_area;
for(int i=;i<=N;i++){
SI(dt[i].val);
dt[i].pos=i;
sum+=dt[i].val;
min_all=min(min_all,dt[i].val);
}
sort(dt+,dt+N+);
mem(vis,);
int num;
for(int i=;i<=N;i++){
if(!vis[i]){
num=;
min_area=dt[i].val;
int j=i;
while(!vis[j]){
vis[j]=;
num++;
//printf("%d ",dt[j].val);
min_area=min(min_area,dt[j].val);
j=dt[j].pos;
}//puts("");
sum+=min((num-)*min_area,*(min_all+min_area)+(num-)*min_all-min_area);
}
}
printf("%lld\n",sum);
}
return ;
}

Cow Sorting(置换群)的更多相关文章

  1. TOJ 1690 Cow Sorting (置换群)

    Description Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow ...

  2. POJ 3270 Cow Sorting(置换群)

    题目链接 很早之前就看过这题,思路题把,确实挺难想的,黑书248页有讲解. #include <cstdio> #include <cstring> #include < ...

  3. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  4. BZOJ1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 215[S ...

  5. hdu 2838 Cow Sorting(树状数组)

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. Cow Sorting hdu 2838

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心

    BZOJ_1697_[Usaco2007 Feb]Cow Sorting牛排序_贪心 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行 ...

  8. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  9. 【BZOJ 1697】1697: [Usaco2007 Feb]Cow Sorting牛排序

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大 ...

随机推荐

  1. Bayesian Formulation on Cooperative Tracking

    Suppose a joint state representing a set of \(N_{n}\) nodes moving in a field\[    \textbf{X}=    \b ...

  2. 商人过河问题(二)java实现

    本文实现的java版商人过河是参考http://wenku.baidu.com/link?url=dpe2AC8mCjpGnclFv6iZy88_vqYm3bED4QDpSkAI4ssgs7Bhntu ...

  3. C#接口的使用【转】

    我们定义一个接口public interface IBark{   void Bark();}再定义一个类,继承于IBark,并且必需实现其中的Bark()方法public class Dog:IBa ...

  4. poj2392 Space Elevator(多重背包)

    http://poj.org/problem?id=2392 题意: 有一群牛要上太空.他们计划建一个太空梯-----用一些石头垒.他们有K种不同类型的石头,每一种石头的高度为h_i,数量为c_i,并 ...

  5. MAC中在eclipse luna上搭建移动平台自己主动化測试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录

    这几天由于原来在用的hp laptop的电池坏掉了,机器一不小心就断电.所以仅仅能花时间在自己的mackbook pro上又一次搭建整套环境.大家都知道搭建好开发环境是个非常琐碎须要耐心的事情,特别是 ...

  6. vs2010编译live555源码

    最近加入了公司的C++视频小组,利用中秋这个假期将研究了一些live555的源码,现在先将如何编译使用vs2010编译live555,整理出来,对以后分析代码有很大帮助. 1.下载live555源码, ...

  7. 移动端适配:font-size设置的思考

    1. 问题的引出 如果html5要适应各种分辨率的移动设备,可以使用rem这样的尺寸单位,针对各个分辨率范围在html上设置font-size的代码: html{font-size:10px} @me ...

  8. 【MFC学习笔记-作业7-小型画图软件】【】

    作业要求: 按下鼠标右键画圆. 按下鼠标左键移动曲线. 丝毫没有思路..网上教程又比这个程序复杂100倍... 好吧 总算找到一个合适的了... 转载至:http://blog.chinaunix.n ...

  9. 【最大流之sap】【HDU1532】模板题

    与上题一样 纯属测试模板 来自kuangbin的模板 #include <cstdio> #include <cstdlib> #include <cmath> # ...

  10. 子级Repeater获取 父级Repeater

    第一种方法,子级Repeater中绑定父级的某个字段: <%# DataBinder.Eval((Container.NamingContainer.NamingContainer as Rep ...