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 ...
随机推荐
- 使用rsync同步数据(by quqi99)
作者:张华 发表于:2015-12-28版权声明:能够随意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) 急需使 ...
- js中的三种函数写法
js中的三种函数写法 <script type="text/javascript"> //普通的声明方式 function myFun(m,n){ alert(m+n) ...
- oracle init.ora常用配置详解
参考网上整理了重要的配置文件 db_name = "51cto" 一个数据库标识符,应与CREATE DATABASE 语句中指定的名称相对应. instance_name = ...
- [Tomcat]Tomcat6和Tomcat7的区别
Tomcat 7最大的改进是对Servlet 3.0和Java EE 6的支持.◆Tomcat 7完全支持Servlet 3.0规范◆Tomcat 7新增了对Java注释的支持◆Tomcat 7通过w ...
- 剑指offer——05用两个栈实现队列(Python3)
思路:(转) 代码: # -*- coding:utf-8 -*-class Solution: stack1 = [] stack2 = [] def push(self, node): self. ...
- redis模拟消息订阅
使用办法: 订阅端: Subscribe 频道名称 发布端: publish 频道名称 发布内容 客户端例子: redis 127.0.0.1:6379> subscribe news Read ...
- .NET中的异步操作及线程同步
执行异步操作 CLR使用了WIN的线程处理能力,但保留了与其分离的权利.某些时候CLR的线程与Win的线程不是完全的匹配. 线程的系统开销较大,应限制其数量. 创建:分配并初始化一线程内核对象,保留1 ...
- 编码介绍(ANSI、GBK、GB2312、UTF-8、GB18030和 UNICODE)
转载:http://blog.jobbole.com/30526/(前面内容)和http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf ...
- Mysql-slowlog
MySQL慢查询日志是MySQL提供的一种日志记录,用来记录执行时长超过指定时长的查询语句,具体指运行时间超过 long_query_time 值的 SQL 语句,则会被记录到慢查询日志中. long ...
- 关于read函数的一些分析
ssize_t readn(int fd, std::string &inBuffer, bool &zero) { ssize_t nread = ; ssize_t readSum ...