UVA 1016 - Silly Sort 置换分解 贪心
Silly Sort
Your younger brother has an assignment and needs some help. His teacher gave him a sequence of
numbers to be sorted in ascending order. During the sorting process, the places of two numbers can be
interchanged. Each interchange has a cost, which is the sum of the two numbers involved.
You must write a program that determines the minimal cost to sort the sequence of numbers.
Input
The input file contains several test cases. Each test case consists of two lines. The first line contains
a single integer n (n > 1), representing the number of items to be sorted. The second line contains n
different integers (each positive and less than 1000), which are the numbers to be sorted.
The input is terminated by a zero on a line by itself.
Output
For each test case, the output is a single line containing the test case number and the minimal cost of
sorting the numbers in the test case.
Place a blank line after the output of each test case
Sample Input
3
3 2 1
4
8 1 2 4
5
1 8 9 7 6
6
8 4 5 3 2 7
0
Sample Output
Case 1: 4
Case 2: 17
Case 3: 41
Case 4: 34
题意:
给定一个序列,数字都不同,每次可以交换两个数字,交换的代价为两数之和,要求出把这个序列变成递增最小代价
题解:
利用置换的分解原理,可以把序列的每条循环单独考虑,对于每条循环而言,不断交换肯定每个数字至少会换到一次,再利用贪心的思想,如果每次拿循环中的最小值去置换,那么就是这个最小值会用长度-1次,而剩下的数字各一次,注意这里还有一种可能优的方法,就是先把整个序列中的最小值换到该循环中,等置换完再换出去,两种都考虑进来即可
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll;
const int N=;
const int inf = ;
int main() {
int a[N],b[N],id[N],n,mi, cas = ;
while(~scanf("%d",&n)) {
if(!n) break;
mi = inf;
for(int i = ; i <= n; i++) {
scanf("%d",&a[i]);
mi = min(mi,a[i]);
b[i] = a[i];
}
sort(b + , b + n +);
for(int i = ; i <= n; i++) id[b[i]] = i;
int ans = ;
for(int i = ; i <= n; i++) {
if(a[i]) {
int cnt = ;
int sum = a[i];
int now = id[a[i]];
int tmp = a[i];
a[i] = ;
while(a[now]) {
cnt++;
sum += a[now];
tmp = min (tmp,a[now]);
int last = now;
now = id[a[now]];
a[last] = ;
}
ans += min(tmp * (cnt - ) + sum, mi * cnt + sum + * (tmp + mi) - tmp);
}
}
printf("Case %d: %d\n\n", ++cas, ans);
}
return ;
}
UVA 1016 - Silly Sort 置换分解 贪心的更多相关文章
- 【uva 11134】Fabled Rooks(算法效率--问题分解+贪心)
题意:要求在一个N*N的棋盘上放N个车,使得它们所在的行和列均不同,而且分别处于第 i 个矩形中. 解法:问题分解+贪心. 由于行.列不相关,所以可以先把行和列均不同的问题分解为2个"在区间 ...
- Uva 11729 Commando War (简单贪心)
Uva 11729 Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...
- uva 1153 顾客是上帝(贪心)
uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...
- 紫书 例题8-4 UVa 11134(问题分解 + 贪心)
这道题目可以把问题分解, 因为x坐标和y坐标的答案之间没有联系, 所以可以单独求两个坐标的答案 我一开始想的是按照左区间从小到大, 相同的时候从右区间从小到大排序, 然后WA 去uDebug找了数据 ...
- UVA - 11134 Fabled Rooks问题分解,贪心
题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...
- UVA 11292 Dragon of Loowater(简单贪心)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVA 11100 The Trip, 2007 (贪心)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 538 - Balancing Bank Accounts(贪心)
UVA 538 - Balancing Bank Accounts 题目链接 题意:给定一些人的欠钱关系,要求在n-1次内还清钱,问方案 思路:贪心,处理出每一个人最后钱的状态,然后直接每一个人都和最 ...
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
随机推荐
- ”危险“的RESTRICT与GCC的编译优化(编程者对编译器所做的一个“承诺”:使用restrict修饰过的指针,它所指向的内容只能经由该指针修改)
restrict是C99标准中新添加的关键字,对于从C89标准开始起步学习C语言的同学来说(包括我),第一次看到restrict还是相当陌生的.Wikipedia给出的解释如下: In the C p ...
- EMC存储划分lun过程
下图是EMC存储系统示意图: 若将lun打散重建,需按以下步骤进行: 1. 在Storage Groups上点右键选择Select Luns,在打开的窗口中,将右边Selected Lun项下的lun ...
- redis模拟消息订阅
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 客户端例子: redis 127.0.0.1:6379> subscribe news Read ...
- Oracle数据库基础(一)
当今主流数据库有瑞典MySQL公司的MySQL数据库,微软的SqlServer数据库,IBM公司的DB2,Oracle公司的Oracle数据库以及美国Sybase的Sybaseshujuku .数据库 ...
- 查看md文件
使用命令将md文件转为html,在浏览器中演示 通过npm安装i5ting_toc 安装好node之后,可以直接使用npm.Windows+R打开运行框,输入cmd,打开命令窗口.连网的情况下,输入如 ...
- localStorage、sessionStorage、cookie、session
localStorage 和 sessionStorage HTML5 提供了两种在客户端存储数据的新方法:localStorage 和 sessionStorage: 两者都是仅在客户端(即浏览器) ...
- [oracle] 组织架构退格显示 connect by
1. 按组织架构关系退格显示 create or replace view v_vieworg asselect --v.OBJID,v.OBJNAMElevel as levelid, lpad(' ...
- 织梦Fatal error: Call to a member function GetInnerText()
问题:织梦修改或者添加了自定义表单后在后台修改文章的时候出现如下错误:Fatal error: Call to a member function GetInnerText() on a non-ob ...
- hdu 3572 Task Schedule【 最大流 】
求出最大流,再判断是否满流 先不理解为什么要这样建图 后来看了这一篇题解 http://blog.csdn.net/u012350533/article/details/12361003 把0看做源点 ...
- 如何巧妙使用ZBrush中的Image Plane插件
ZBrush®插件Image Plane提供了一种简单的方法加载图像到ZBrush中,以在添加纹理过程中进行使用,比如使用ZProject笔刷多边形着色,以及利用参考图建模等. ZBrush 中文版下 ...