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. 小知识点-ios跳过app store更新版本

    版本更新实现的思路 获取自身的版本号 获取AppStore的版本号 自身的版本号和AppStore的比较 弹窗提示所需数据的获取的方式 1.获取自身的版本号 2.AppStore的版本号 Wechat ...

  2. jquery模板下载网站

    jquery模板下载网站 http://www.jqshare.com/

  3. vue学习心得

    前言 使用vue框架有一段时间了,这里总结一下心得,主要为新人提供学习vue一些经验方法和项目中一些解决思路. 文中谨代表个人观点,如有错误,欢迎指正. 环境搭建 假设你已经通读vue官方文档(文档都 ...

  4. 【NOIP2016提高组day2】蚯蚓

    那么我们开三个不上升队列, 第一个记录原来的蚯蚓, 第二个记录乘以p的蚯蚓 第三个记录乘以(1-p)的蚯蚓, 在记录每条就要入队列的时间,就可以求出增加的长度 每次比较三个队列的队首,取最大的值x的切 ...

  5. css常见布局方式

    CSS常见布局方式 以下总结一下CSS中常见的布局方式.本人才疏学浅,如有错误,请留言指出. 如需转载,请注明出处:CSS常见布局方式 目录: 使用BFC隐藏属性 float + margin abs ...

  6. 简单又炫酷的two.js 二维动画教程

      前  言 S     N 今天呢给大家介绍一个小js框架,Two.JS.其实在自己学习的过程中并没有找到合适的教程,所以我这种学习延迟的同学是有一定难度的,然后准备给大家整理一份,简单易懂的小教程 ...

  7. Angular JS中的路由

      前  言            本章节将为大家介绍 AngularJS 路由.AngularJS 路由允许我们通过不同的 URL 访问不同的内容.通过 AngularJS 可以实现多视图的单页We ...

  8. Are We There Yet? (zoj1745)

    Are We There Yet?     (ZOJ Problem Set - 1745) Are We There Yet? Time Limit: 2 Seconds      Memory L ...

  9. HDU1166 数状数组

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  10. ZOJ1171

    错误代码先放这 #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring& ...