poj3270Cow 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
Lines 2..N+1: Each line contains a single integer: line i+1 describes the grumpiness of cow i.
Output
Sample Input
3
2
3
1
Sample Output
7
Hint
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(置换+贪心)的更多相关文章
- poj3270Cow Sorting(置换)
链接 对于组合数学是一点也不了解 讲解 重要一点 要知道一个循环里最少的交换次数是m-1次 . #include <iostream> #include<cstdio> #in ...
- BZOJ_1119_[POI2009]SLO_置换+贪心
BZOJ_1119_[POI2009]SLO_置换+贪心 Description 对于一个1-N的排列(ai),每次你可以交换两个数ax与ay(x<>y),代价为W(ax)+W(ay) 若 ...
- BZOJ 1697: [Usaco2007 Feb]Cow Sorting牛排序(置换+贪心)
题面 Description 农夫JOHN准备把他的 N(1 <= N <= 10,000)头牛排队以便于行动.因为脾气大的牛有可能会捣乱,JOHN想把牛按脾气的大小排序.每一头牛的脾气都 ...
- poj 3270 Cow Sorting (置换入门)
题意:给你一个无序数列,让你两两交换将其排成一个非递减的序列,每次交换的花费交换的两个数之和,问你最小的花费 思路:首先了解一下什么是置换,置换即定义S = {1,...,n}到其自身的一个双射函数f ...
- bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...
- 2019.01.22 hdu5195 DZY Loves Topological Sorting(贪心+线段树)
传送门 题意简述:给出一张DAGDAGDAG,要求删去不超过kkk条边问最后拓扑序的最大字典序是多少. 思路:贪心帮当前不超过删边上限且权值最大的点删边,用线段树维护一下每个点的入度来支持查询即可. ...
- Cow Sorting(置换)
http://poj.org/problem?id=3270 // File Name: poj3270.cpp // Author: bo_jwolf // Created Time: 2013年1 ...
- [BZOJ1697][USACO2007 FEB]Cow Sorting牛排序:贪心+置换
分析 一个月前做的一道题补一下题解,就简单写一写吧. 单独考虑每一个循环节,如果只进行内部的调整,最优方案显然是把最小的绕这个循环交换一圈. 但是借助全局最小值可能使答案更优,两种情况取个\(\max ...
- UVA 1016 - Silly Sort 置换分解 贪心
Silly Sort Your younger brother has an assignment and needs s ...
随机推荐
- New Features and changes of Ninject 3.3
Ninject 3.3 beta1 has gone live. This release mainly focus on bug fix and platform update. Support . ...
- 【设计模式】module(模块)模式
写在前面 最近刚接触到设计模式, <head first设计模式>里有一篇文章,是说使用模式的心智, 1.初学者"心智" :"我要为HELLO WORLD找个 ...
- python进阶之Socket 网络编程
一:网络编程介绍 自从互联网诞生以来,现在基本上所有的程序都是网络程序,很少有单机版的程序了. 计算机网络就是把各个计算机连接到一起,让网络中的计算机可以互相通信.网络编程就是如何在程序中实现两 ...
- Spring-boot:5分钟整合Dubbo构建分布式服务
概述: Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合).从服务模型的角度来看,Dubbo采用的是一种非常 ...
- bzoj1036 [ZJOI2008]树的统计
一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从 ...
- 在Pycharm中使用jupyter笔记本
在Pycharm中使用jupyter笔记本 我在Pycharm中使用jupyter笔记本,发现新的Jupyter更新中,增加了令牌. 随着创建的虚拟环境启动的所有设置,并将URL设置为127.0.0. ...
- SQL Server Compact/SQLite Toolbox 使用
最近一个嵌入式的数据库用的SqlCe 需要导入到Sqlite, 网上查到了这个工具--SQL Server Compact/SQLite Toolbox.但是在使用的时候遇到了一点小曲折,记录下来给需 ...
- #云栖大会# 移动安全专场——APP加固新方向(演讲速记)
主持人导语: 近些年来,移动APP数量呈现爆炸式的增长,黑产也从原来的PC端转移到了移动端,伴随而来的逆向攻击手段也越来越高明.在解决加固产品容易被脱壳的方案中,代码混淆技术是对抗逆向攻击最有效的方式 ...
- java web 项目 图书管理系统的设计与实现
java web 项目 图书管理系统的设计与实现
- C#解析XML详解(XPath以及带命名空间NameSpace)
<?xml version="1.0" encoding="utf-8" ?> <bookstore> <book> < ...