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. MonoDeveloper 快捷键

    注:环境是Unity3D 5.0.2f1自带的MonoDevelop Ctrl+X 剪切功能.另外,光标放在一行的任意位置(不选中任何内容),使用快捷键,将把这一行剪切并删除此行,这个特性非常好用 C ...

  2. 强大的桌面用 PDF 重排工具:K2pdfopt 简明教程

    用 Kindle 阅读 PDF 一直以来都遭到小伙伴们的无限吐槽,在那 Kindle 还能越狱的时代,我们有 Koreader 之类优秀的 Kindle 第三方插件实现 PDF 文档的实时重排,但是随 ...

  3. 每周分享一 之 webSocket

    一:什么是webSocket ? webSocket是HTML5出的新协议,WebSocket协议支持,在受控环境中运行不受信任代码的客户端与选择了该代码通信的远程主机之间进行双向通信. 简单翻译一下 ...

  4. clipboard.js 介绍

    这是著名开源项目 clipboard.js 的 README.md,我把它翻译成中文.发出来,方便自己和他人阅读. 项目地址:https://github.com/zenorocha/clipboar ...

  5. 2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. 斐波那契数列第N项f(N)[矩阵快速幂]

    矩阵快速幂 定义矩阵A(m*n),B(p*q),A*B有意义当且仅当n=p.即A的列数等于B的行数. 且C=A*B,C(m*q). 例如: 进入正题,由于现在全国卷高考不考矩阵,也没多大了解.因为遇到 ...

  7. NOIP2014_day2:无线网络发射器选址

    #include<stdio.h>//NOIP2014 day2 :无线网络发射器选址 ,max=; ][]; void wifi(int a,int b,int c) { int i,j ...

  8. django获取ajax的post复杂对象

    一.django的request中post对象为空(即获取不到前台ajax传送的post对象) 1.将django的setting中的django.middleware.csrf.CsrfViewMi ...

  9. XML编程

     XML编程 XML及其语法 XML约束之DTD XML编程(CRUD---Create Read Update Delete) XML约束之Schema XML语法: XML文档的组成: 一个X ...

  10. Pycharm小技巧--使用正则进行查找和批量替换

    分享一个Pycharm中使用正则的分组匹配来进行批量替换的小技巧 例如,我现在需要把HTML文件中的静态文件得到路径全部替换为django模板引用路径的格式 修改为类似这样的格式: {% static ...