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 February 2007

 #include <stdio.h>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 110000
using namespace std; int n;
int a[MAXN];
int pos[MAXN];
int use[MAXN]; int main(int argc, char *argv[])
{
int maxValue;
int minValue;
while( scanf("%d" ,&n)!=EOF ){
maxValue=;
minValue=inf;
memset(pos , ,sizeof(pos));
memset(use , ,sizeof(use));
for(int i=; i<=n; i++){
scanf("%d",&a[i]);
pos[a[i]]++;
if(a[i]>maxValue){
maxValue=a[i];
}
if(a[i]<minValue){
minValue=a[i];
}
}
for(int i=; i<=maxValue; i++){
pos[i]=pos[i-]+pos[i];
}
int sum=;
for(int i=; i<=n; i++){
//找循环节
if(!use[i]){
int j=i;
int len=;
int t=a[j];
int ts=;
while(!use[j]){
//找到置换群里最小的数
if(a[j]<t){
t=a[j];
}
//求置换群的和
ts+=a[j];
use[j]=;
j=pos[a[j]];
len++;
}
if(<len){
sum+=ts;
}
if(<len){
int t1=(len-)*t;
int t2=t+(len+)*minValue;
if(t1<t2){
sum+=t1;
}
else{
sum+=t2;
}
}
}
}
printf("%d\n",sum);
}
return ;
}

最新文章

  1. Maven 最佳实践
  2. [UML]UML系列——类图class的依赖关系
  3. 字符串分割函数(New)
  4. vc++ 加载,卸载自己的驱动程序
  5. ArcGIS Engine 中 线加箭头
  6. 学习Linux第四天
  7. 安卓天天练练(十一)用list绑数据
  8. Ubuntu 16.04 Django安装和配置
  9. http://mozilla.debian.net/
  10. JVM之GC算法、垃圾收集算法——标记-清除算法、复制算法、标记-整理算法、分代收集算法
  11. mac/Linux/centos ssh连接服务器以及跳板机,实现类型Xshell 功能
  12. LINQ to SQL 的常见异常及解决办法
  13. Java使用Future设置方法超时
  14. 【OpenStack】network相关知识学习
  15. matlab global persistent变量
  16. day39 css
  17. git基础使用——TortoiseGit
  18. 20155304《网络对抗》Exp4 恶意代码分析
  19. Hibernate的实体规则、主键生成策略、对象状态
  20. 【Spring】SpringMVC之基于注解的实现SpringMVC+MySQL

热门文章

  1. delphi XE7 在Android编译SharedActivity时出错
  2. 在iis7.5上部署asp.net mvc5
  3. android android studio
  4. 灯塔AOI简易实现
  5. 第0篇 Python前言
  6. selenium面试题
  7. 洛谷P2754 [CTSC1999]家园(最大流)
  8. HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和
  9. requests库和urllib包对比
  10. count failed: not master{ &quot;note&quot; : &quot;from execCommand&quot;, &quot;ok&quot; : 0, &quot;errmsg&quot; : &quot;not master&quot; } at src/mongo/shell/query.js:191在SECONDARY节点无法show dbs