Cow Sorting
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7587   Accepted: 2982

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).
/*
* @Author: LyuC
* @Date: 2017-10-12 15:55:54
* @Last Modified by: LyuC
* @Last Modified time: 2017-10-12 16:52:18
*/
/*
题意:给你一个无序的序列,让你只能两两进行交换,使得序列有序,每次操作的代价是两个数的和
问你最少的代价是多少 思路:置换+贪心
例子 :
原始序列:1 8 9 7 6
排序下标:1 4 5 3 2
那么我们发现,位置不对的8 9 7 6实际上是一个置换:4->3->5->2->4
长度为4最少需要交换三次才能使得序列有序,保证了次数最少了,然后就是
考虑怎么交换才能代价最小,置换(4,3,5,2)可以拆成(2,4),(2,3),(2,5),
这样就保证了部分交换代价是最小的,但是这个例子就是个特例,如果首先
将1 6 进行交换使得(4,3,5,1)在一个置换里,交换完再用6把1交换出去,这
样的代价更小,所以这个有两种最优操作,处理的时候取两者更右者;
*/
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm> #define MAXN 10005
#define INF 0x3f3f3f3f using namespace std; struct Node{
int val;
int index;
int sortindex;
}a[MAXN];
int n;
bool vis[MAXN];
vector<int>v[MAXN];
int pos;
int minnum; bool cmp1(Node a,Node b){
return a.val<b.val;
} bool cmp2(Node a,Node b){
return a.index<b.index;
} void init(){
for(int i=;i<MAXN;i++)
v[i].clear();
pos=;
minnum=INF;
memset(vis,false,sizeof vis);
} int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
scanf("%d",&a[i].val);
a[i].index=i;
minnum=min(minnum,a[i].val);
}
sort(a+,a+n+,cmp1);
for(int i=;i<=n;i++){
a[i].sortindex=i;
}
sort(a+,a+n+,cmp2);
for(int i=;i<=n;i++){
if(vis[i]==true)
continue;
int x=i;
vis[i]=true;
v[pos].push_back(a[i].sortindex);
x=a[x].sortindex;
while(x!=i){
if(vis[x]==true){
break;
}
v[pos].push_back(a[x].sortindex);
vis[x]=true;
x=a[x].sortindex;
}
if(a[x].sortindex==a[i].sortindex){
pos++;
}else{
v[pos].clear();
v[pos].push_back(i);
int End=x;
x=i;
vis[x]=false;
while(x!=End){
x=a[x].sortindex;
vis[x]=false;
}
vis[i]=true;
pos++;
}
}
sort(a+,a+n+,cmp1);
int res=,ra,rb;
for(int i=;i<pos;i++){
sort(v[i].begin(),v[i].end());
ra=;
for(int j=;j<(int)v[i].size();j++){
ra+=(a[v[i][]].val+a[v[i][j]].val);
}
if(minnum!=a[v[i][]].val){
rb=;
for(int j=;j<(int)v[i].size();j++){
rb+=(minnum+a[v[i][j]].val);
}
rb+=*(minnum+a[v[i][]].val);
}else{
rb=INF;
}
res+=min(ra,rb);
}
printf("%d\n",res);
}
return ;
}

poj3270Cow Sorting(置换+贪心)的更多相关文章

  1. poj3270Cow Sorting(置换)

    链接 对于组合数学是一点也不了解 讲解 重要一点 要知道一个循环里最少的交换次数是m-1次 . #include <iostream> #include<cstdio> #in ...

  2. BZOJ_1119_[POI2009]SLO_置换+贪心

    BZOJ_1119_[POI2009]SLO_置换+贪心 Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若 ...

  3. BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)

    题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...

  4. poj 3270 Cow Sorting (置换入门)

    题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...

  5. bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...

  6. 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)

    传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...

  7. Cow Sorting(置换)

    http://poj.org/problem?id=3270 // File Name: poj3270.cpp // Author: bo_jwolf // Created Time: 2013年1 ...

  8. [BZOJ1697][USACO2007 FEB]Cow Sorting牛排序:贪心+置换

    分析 一个月前做的一道题补一下题解,就简单写一写吧. 单独考虑每一个循环节,如果只进行内部的调整,最优方案显然是把最小的绕这个循环交换一圈. 但是借助全局最小值可能使答案更优,两种情况取个\(\max ...

  9. UVA 1016 - Silly Sort 置换分解 贪心

                                           Silly Sort Your younger brother has an assignment and needs s ...

随机推荐

  1. Spring定时器的使用详解

    写个最简单的demo吧,反正睡前没什么事儿,来祸害一下园子~~虽然我菜,但是我不会承认啊,哈哈哈 明天详细补充点儿吧,很晚了,不睡觉的程序员不是好程序员,我总能给自己找借口~~~ //spring开启 ...

  2. 高德地图JSApi

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  3. 第一个ExtJS练习(添加用户面板)

    1.[准备] 我是在visual studio里面建立了一个asp.net MVC项目,然后导入ExtJS必要的包,然后写的. ExtJS5.1版本下载:https://pan.baidu.com/s ...

  4. 第4章 同步控制 Synchronization ----互斥器(Mutexes)

    Win32 的 Mutex 用途和 critical section 非常类似,但是它牺牲速度以增加弹性.或许你已经猜到了,mutex 是 MUTual EXclusion 的缩写.一个时间内只能够有 ...

  5. GitHub Desktop客户端打开文件乱码问题解决方案

    今天在使用GitHub Desktop客户端的时候,发添加本地仓库后文件内容显示为乱码. 1.现象  如下图所示: 2.原因分析 后来分析得知原来是由于编码不统一造成 的. 具体来说,我在window ...

  6. 小程序解释HTML富文本的两种办法

    今天写着着代码,读取数据库的内容时突然跳出"<span>.<p>. "这些HTML标签.字符,吓一跳:本来如果是写HTML.JS倒也没什么,但是我在写小程序 ...

  7. 一步使你的asp.net网站在手机浏览器上全屏显示

    首先要加入下面的代码: <meta name="viewport" content="width=device-width, initial-scale=1.0, ...

  8. Windows系统下使用Jenkins 自动发布 .NET core到Linux平台下Docker

    准备工作(安装过程可以百度,已安装的可以跳过) a)     安装Jenkins,安装包下载地址:http://mirrors.tuna.tsinghua.edu.cn/jenkins/windows ...

  9. php中各种定义变量的方法

      1.定义常量define("CONSTANT", "Hello world."); 常量只能包含标量数据(boolean,integer,float 和 s ...

  10. web自动化测试从入门到持续集成(selenium webdriver)

    在很多刚学习自动化的可能会认为我只需要会运用selenium,我只需要在一个编辑器中实用selenium +java编写了一些脚本那么就会自动化了,是真的吗?答案肯定是假的.自动化肯定是需要做到真的完 ...