poj 3270(置换群+贪心)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6993 | Accepted: 2754 |
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).
Source

设当前群的循环节为 n,这里最小的那个数总共需要交换n-1 次,其余的数各需要一次.
这里所需要的代价是 val = sum(该群内元素之和) - 该群最小的元素 + (loop-1)*该群的最小元素;
另外一种当时是利用整个集合内最小的元素将该群所有的元素归位,先需要交换整个集合最小元素(m1)与该群
最小元素(m2),然后再利用 m1 与各元素交换一次,m1总共交换了 loop 次,最后要将 m2换回来,所以 m1 交换了
loop+1次,m2交换了两次,其余元素各交换了一次.
/**
置换:
这里每一个置换群里面的数归位有两种方法:
一种是利用该群里面最小的那个元素将所有的元素归位:
设当前群的循环节为 n,这里最小的那个数总共需要交换n-1 次,其余的数各需要一次.
这里所需要的代价是 val = sum(该群内元素之和) - 该群最小的元素 + (loop-1)*该群的最小元素; 另外一种当时是利用整个集合内最小的元素将该群所有的元素归位,先需要交换整个集合最小元素(m1)与该群
最小元素(m2),然后再利用 m1 与各元素交换一次,m1总共交换了 loop 次,最后要将 m2换回来,所以 m1 交换了
loop+1次,m2交换了两次,其余元素各交换了一次.
**/
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long LL;
const int N = ;
const int INF = 1e18;
struct Node{
int val;
int id;
}node[N];
int m1,m2,loop,n;
bool vis[N];
LL solve(){
LL res = ,sum;
for(int i=;i<=n;i++){
m2 = INF;
sum = loop = ;
int t = i;
while(!vis[t]){
vis[t] = true;
loop++;
m2 = min(m2,node[t].val);
sum+=node[t].val;
t = node[t].id;
}
if(loop){
res = res+min(sum-m2+(loop-)*m2,sum+m2+(loop+)*m1);
}
}
return res;
}
int cmp(Node a,Node b){
return a.val < b.val;
}
int main()
{
while(scanf("%d",&n)!=EOF){
m1 = INF;
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
scanf("%d",&node[i].val);
node[i].id = i;
m1 = min(m1,node[i].val);
}
sort(node+,node++n,cmp);
printf("%lld\n",solve());
}
return ;
}
poj 3270(置换群+贪心)的更多相关文章
- POJ 3270 置换群问题
题目大意是: 每头牛都有一个对应的值a[i],现在给定一个初始的牛的序列,希望通过两两交换,能够使这些牛按值升序排列,每次交换都会耗费一个 a[i]+a[j] 希望耗费最小,求出这个最小耗费 个人觉得 ...
- poj 3270(置换群)
题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...
- POJ 3270 Cow Sorting(置换群)
题目链接 题意 : N头牛,每个牛的坏脾气都有一个值,每个值都不相同,把这个值按照从小到大排序,如果两个值交换,那么会花掉这两个值之和的时间,让你花最少的时间将每个值从小到大排好序,求最小的总时间. ...
- POJ 3270 Cow Sorting(置换群)
题目链接 很早之前就看过这题,思路题把,确实挺难想的,黑书248页有讲解. #include <cstdio> #include <cstring> #include < ...
- POJ 1456(贪心)
#include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...
- poj -3614 Sunscreen(贪心 + 优先队列)
http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...
- POJ 3614 Sunscreen 贪心
题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...
- poj 3270 Cow Sorting
思路:仔细读题,看到FARMER是两两交换牛的顺序进行排序的话,应该就往置换上靠拢,而这个题果然是置换的应用(有的解题报告上说是置换群,其实这只是单个置换,不用让它构成群).我们来将这些无序的牛抽象成 ...
- POJ 1456 - Supermarket - [贪心+小顶堆]
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...
随机推荐
- freemark的常用方法
1,截取字符串 有的时候我们在页面中不需要显示那么长的字符串,比如新闻标题,这样用下面的例子就可以自定义显示的长度 < lt. <= lte. > gt. >= gte < ...
- O(1)时间复杂度求栈中最小元素
import java.util.Stack; /** * 功能:O(1)时间复杂度求栈中最小元素 * 思路:空间换取时间,使用两个栈,stack1栈存储数据,stack2栈存储最小值: * stac ...
- 01 C++ 多线程入门实例
1.可复用的完整实例 #include <iostream> #include <thread> #include <mutex> using namespace ...
- npm安装socket.io时报错的解决方法(npm WARN enoent ENOENT: no such file or directory, open '/usr/local/nodejs/bin/package.json')
执行 npm install socket.io安装时报错: [root@WEB node_modules]# npm install socket.ionpm WARN enoent ENOENT: ...
- Consul 服务发现与配置
Consule 是什么 Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键特性: 服务发现 Consul 的客户端可用提供一个服务,比如 api 或 ...
- UITableViewCell的separatorInset属性
separatorInset这个属性是IOS7后才有的属性,所以需要判断一下,才能修改 if (IOS7_OR_LATER) { cell.separatorInset = UIEdgeInsetsZ ...
- bzoj千题计划150:bzoj2738: 矩阵乘法
http://www.lydsy.com/JudgeOnline/problem.php?id=2738 整体二分 二维树状数组累积 #include<cstdio> #include&l ...
- android 自定义TODO
1.找到setting -> editor -> TODO 2. 正则的写法参考以前的就可以 这样我们就可以自己写一个todo了 3.TODO过滤: 4. 然后选择要展示的TODO 这里就 ...
- select 的字段为空,给他显示默认值
select 的字段为空,给他显示默认值: 解决办法一: select id,name,(case when level is null then 0 else level end) as a fro ...
- Eclipse改变相同代码高亮颜色
一.点击某一代码时,让相同代码高亮显示(Eclipse默认是这样的) Window ->preferences ->Java ->Editor ->Mark Occurrenc ...