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

题目大意:

给你一个排列,然后让你交换到递增排列,交换X和Y的位置,花费就是X+Y,求变成递增序列的最小花费

题解:

我们先说说样例:2 3 1 —— ①

编号分别为:       1 2 3 —— ②

排序后的编号为:3 1 2 —— ③

这样一来我们就可以理解为②经过一系列变换就形成了③(也就是最终形态),那么既然是序列变换,我们考虑置换。那么②和③形成了一个置换。

$$\binom{1,2,3}{3,1,2}$$

那么对于一个置换,我们分别处理置换上的每一个环,有两点可以肯定:

  1.一个环可以通过自身内部的交换形成最终形态。

  2.当每一个环都形成了最终形态时,序列也就从②变到了三③。

那么接下来怎么求出代价的最小值呢?


首先我们分别考虑每一个环,如果不考虑其他环对这个环的影响(也就是说,这个环在自己内部交换)。

此时有一个贪心十分显然,用一个环的最小值去和环上的每一个数分别交换所付出的代价最小。

下面我们给出证明:

  假设我们当前处理的环长度为$k$,那么我们必定要交换$k-1$次。(不可能更多,因为环已经确定了每一个数的先后关系,我们只要两两之间分别交换就好)

  ∴我们要在环上确定一个起点$x$,这个$x$沿着环交换一圈即可。

  ∴付出的代价为$sum-v[x]+v[x]*(tot-1)$(sum为环上权值和,tot为环上的点数,v[x]为起点的权值)

  ∴权值越小越好。

  综上所述,得证。

接下来我们考虑其他环的影响。


假设某一个环上的最小值比当前处理的环的最小值还要小,那么我们只要将这两个环的最小值先交换,然后处理完当前环,在交换回去。

那么这样做的代价和单独考虑一个环的代价取个最小值,就是处理完这个环的代价。

于是乎我们只要求出所有数中的最小值min,再将它代入每一个环求一遍,然后和这个环自身处理的代价比较一下就好。

 1 //Never forget why you start
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #define inf (2147483647)
 9 using namespace std;
10 int n,m,ans;
11 struct node{
12   int x,id,pos;
13   friend bool operator < (const node a,const node b){
14     return a.x<b.x;
15   }
16 }a[10005];
17 bool cmp(const node a,const node b){
18   return a.id<b.id;
19 }
20 struct part{
21   int mmin,sum,tot;
22   part(){}
23   part(int _mmin,int _sum,int _tot){
24     mmin=_mmin;
25     sum=_sum;
26     tot=_tot;
27   }
28 }o[10005];
29 int vis[10005],cnt,mmin[10005],sum[10005],tot[10005],tmp,p,lm;
30 void dfs(int r){
31   vis[r]=1;
32   p+=a[r].x;
33   cnt++;
34   tmp=min(tmp,a[r].x);
35   if(vis[a[r].pos]==1){mmin[r]=tmp;sum[r]=cnt;tot[r]=p;return;}
36   else dfs(a[r].pos);
37   tot[r]=p;
38   mmin[r]=tmp;
39   sum[r]=cnt;
40 }
41 int main(){
42   int i,j;
43   while(scanf("%d",&n)!=EOF){
44     memset(mmin,0,sizeof(mmin));
45     memset(vis,0,sizeof(vis));
46     memset(a,0,sizeof(a));
47     memset(o,0,sizeof(o));
48     memset(sum,0,sizeof(sum));
49     memset(tot,0,sizeof(tot));
50     lm=0;ans=0;
51     for(i=1;i<=n;i++){
52       scanf("%d",&a[i].x);
53       a[i].id=i;
54     }
55     sort(a+1,a+n+1);
56     for(i=1;i<=n;i++)a[i].pos=i;
57     sort(a+1,a+n+1,cmp);
58     for(i=1;i<=n;i++)
59       if(!vis[i]){
60     cnt=0;
61     p=0;
62     tmp=inf;
63     dfs(i);
64     o[++lm]=part(mmin[i],sum[i],tot[i]);
65       }
66     tmp=inf;
67     for(i=1;i<=lm;i++)tmp=min(tmp,o[i].mmin);
68     for(i=1;i<=lm;i++){
69       ans+=min(2*(tmp+o[i].mmin)+(o[i].sum-1)*tmp+o[i].tot-o[i].mmin,o[i].tot-o[i].mmin+(o[i].sum-1)*o[i].mmin);
70     }
71     printf("%d\n",ans);
72   }
73   return 0;
74 }

转载于:https://www.cnblogs.com/huangdalaofighting/p/8343431.html

[POJ 3270]Cow Sorting相关推荐

  1. pku 3270 Cow Sorting 置换群

    http://poj.org/problem?id=3270 题意: 给定N头牛的身高,要求你通过每次交换两头牛的位置使其按身高从小到大排序,身高各不相同.假设交换ai,aj两头牛的位置则花费的时间为 ...

  2. POJ 3660 Cow Contest [Floyd]

    POJ - 3660 Cow Contest http://poj.org/problem?id=3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1. ...

  3. poj 1985 Cow Marathon 【树的直径】

    求树的直径 /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostre ...

  4. bzoj 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换)

    1697: [Usaco2007 Feb]Cow Sorting牛排序 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 706  Solved: 413 ...

  5. P5200 [USACO19JAN]Sleepy Cow Sorting

    P5200 [USACO19JAN]Sleepy Cow Sorting 题目描述 Farmer John正在尝试将他的N头奶牛(1≤N≤10^5),方便起见编号为1-N,在她们前往牧草地吃早餐之前排 ...

  6. LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组

    \(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...

  7. poj3270 Cow Sorting 置换环+贪心

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7455   Accepted: 2926 Descr ...

  8. POJ 3660 Cow Contest 传递闭包+Floyd

    原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. poj 3045 Cow Acrobats (贪心!!不是二分,)

    农夫的N只牛(1<=n<=50,000)决定练习特技表演. 特技表演如下:站在对方的头顶上,形成一个垂直的高度. 每头牛都有重量(1 <= W_i <= 10,000)和力量( ...

最新文章

  1. 微信小程序 view中的image水平垂直居中
  2. WebStorm中SVN配置
  3. 论坛报名 | 智能信息检索与挖掘的最新进展和挑战
  4. rsynv+inotify触发试实时同步
  5. linux下批量修改文件名的方法
  6. JSON(3)--- 数组
  7. 为什么程序员愿意加入管理糟糕的创业公司?
  8. Android水平仪实训报告,测量实训报告范文3篇
  9. 学习ARM64页表转换流程
  10. 成品app直播源码,fragment切换 常用写法
  11. python怎么定义向量类_python的用户定义向量类
  12. Linux用到的大数据相关命令
  13. Java Json格式化工具
  14. python_day6_面向对象的介绍/构造函数/类变量和实例变量/析构函数/私有属性和私有方法/继承、多继承和继承实例/多态
  15. 关于自己写的第一份简历
  16. qq播放器免费的方法
  17. Android 应用层组件安全测试基础实战技巧
  18. android 解决图片替换之后没有更新图片
  19. 扩增子16S分析专题研讨论会——背景介绍
  20. 骨传导耳机是怎么发声的,骨传导耳机值得入手嘛

热门文章

  1. Apache Kafka源码分析 – Log Management
  2. start_stop_time
  3. 国外程序员收集整理的PHP资源大全
  4. 利用工具及api接口写博文
  5. Matlab怎么计算信号的能量,用Matlab求离散讯号的能量与功率怎么编程
  6. php 消息中间件,消息中间件NMQ
  7. mysql outfile 路径_MySQL load_file()/into outfile路径问题总结
  8. 弹出页(指定高度,自由拖动,点击空白包括状态栏触发)
  9. python ctime源码_Python3基础 getatime getctime getmtime 文件的最近访问 + 属性修改 + 内容修改时间...
  10. linux内核更新/修补程序,Ubuntu 18.04.3 LTS无需重启即可轻松修补Linux内核