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. OC——多态

    书接上文,上文提到继承一个很大用途的是为了更好的实现多态,现在我们就来看看OC的多态. 多态:顾名思义就是好多种状态,以前学C#时候印象最深刻的例子是好多个类共同实现同一个接口,然后把这些类的对象都装 ...

  2. day09<面向对象+>

    面向对象(多态的概述及其代码体现) 面向对象(多态中的成员访问特点之成员变量) 面向对象(多态中的成员访问特点之成员方法) 面向对象(多态中的成员访问特点之静态成员方法) 面向对象(超人的故事) 面向 ...

  3. StringBuffer的替换和反转和截取功能

    A:StringBuffer的替换功能 * public StringBuffer replace(int start,int end,String str): * 从start开始到end用str替 ...

  4. nmap扫描某段网络连通性

    nmap -v -sP 10.0.10.0/24 进行ping扫描,打印出对扫描做出响应的主机,不做进一步测试(如端口扫描或者操作系统探测): nmap -sP 192.168.1.0/24 仅列出指 ...

  5. 如何解决conda install:command not found问题

    每次运行conda相关代码之前先做一遍source ~/.bashrc.即可

  6. java从命令行接收多个数字,求和程序分析

    问题:编写一个程序,此程序从命令行接收多个数字,求和之后输出结果. 1.设计思想 (1)声明两个变量接收输入的字符串 (2)将字符串转换成int类型 (3)输出求和 2.程序流程图 3.源程序代码 i ...

  7. AES加解密算法Qt实现

    [声明] (1) 本文源码 在一位未署名网友源码基础上,利用Qt编程,实现了AES加解密算法,并添加了文件加解密功能.在此表示感谢!该源码仅供学习交流,请勿用于商业目的. (2) 图片及描述 除图1外 ...

  8. Spring Boot Document Part I

    最近准备学习Spring Boot 随便翻一下官方的文档 Part I. Spring Boot Documentation Spirng Boot简短介绍,作为接下来内容的导航,可快速预览本章内容. ...

  9. 《深入理解Java虚拟机》读书笔记-垃圾收集器与内存分配策略

    在堆里存放着java世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前需要知道哪些对象还存活,哪些对象已经死去.那怎么样去判断对象是否存活呢? 一.判断对象是否存活算法 1.引用计数法 实现思路:给 ...

  10. 使用javaAPI操作hdfs

    欢迎到https://github.com/huabingood/everyDayLanguagePractise查看源码. 一.构建环境 在hadoop的安装包中的share目录中有hadoop所有 ...