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 ...
随机推荐
- Django JSON 时间
在views.py中导入: from django.core.serializers.json import DjangoJSONEncoder 在返回JSON数据时调用: return HttpRe ...
- C++中的空类,编译器默认可以产生哪些成员函数
C++中的空类,编译器默认可以产生哪些成员函数 C++中创建一个空类:class Empty {};默认会生成4个函数,其函数的原型如下: public: Empty() { ... } Empty( ...
- R语言计算moran‘I
R语言计算moran‘I install.packages("maptools")#画地图的包 install.packages("spdep")#空间统计,m ...
- Python【操作Redis数据库】
Redis非关系型数据库,数据存放在计算机内存中,无SQL语句.Redis中有多种数据类型,比较常用的数据类型是string类型和hash类型.平时我们使用RedisDesktopManager来对R ...
- python---await/async关键字
推文:玩转 Python 3.5 的 await/async 首先看正常的两个函数之间的执行 def func1(): print("func1 start") print(&qu ...
- classpath 及读取 properties 文件
java代码中获取项目的静态文件,如获取 properties 文件内容是必不可少的. Spring 下只需要通过 @Value 获取配置文件值 <!-- 资源文件--> <util ...
- bzoj千题计划152:bzoj3405: [Usaco2009 Open]Grazing2 移动牛棚
http://www.lydsy.com/JudgeOnline/problem.php?id=3405 n个牛棚,n-1段 因为要求距离尽量大,而且尽可能多的为d 所以: 第1个牛棚一定在位置1 最 ...
- 微信 js-sdk
使用方法 http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html Demo http://203.195.235.7 ...
- WCF: Retry when service is in fault state.
Service Host: using System; using System.Configuration; using System.ServiceModel; using System.Serv ...
- 如何创建一个https的站点(超简单) 以及 IIS7.5绑定Https域名
1.申请免费1年的ssl证书(传送门:https://common-buy.aliyun.com/?spm=5176.2020520163.cas.29.N0xOPM&commodityCod ...