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 置换分解 贪心的更多相关文章

  1. 【uva 11134】Fabled Rooks(算法效率--问题分解+贪心)

    题意:要求在一个N*N的棋盘上放N个车,使得它们所在的行和列均不同,而且分别处于第 i 个矩形中. 解法:问题分解+贪心. 由于行.列不相关,所以可以先把行和列均不同的问题分解为2个"在区间 ...

  2. Uva 11729 Commando War (简单贪心)

    Uva 11729  Commando War (简单贪心) There is a war and it doesn't look very promising for your country. N ...

  3. uva 1153 顾客是上帝(贪心)

    uva 1153 顾客是上帝(贪心) 有n个工作,已知每个工作需要的时间q[i]和截止时间d[i](必须在此前完成),最多能完成多少个工作?工作只能串行完成,第一项任务开始的时间不早于时刻0. 这道题 ...

  4. 紫书 例题8-4 UVa 11134(问题分解 + 贪心)

     这道题目可以把问题分解, 因为x坐标和y坐标的答案之间没有联系, 所以可以单独求两个坐标的答案 我一开始想的是按照左区间从小到大, 相同的时候从右区间从小到大排序, 然后WA 去uDebug找了数据 ...

  5. UVA - 11134 Fabled Rooks问题分解,贪心

    题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...

  6. UVA 11292 Dragon of Loowater(简单贪心)

    Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...

  7. UVA 11100 The Trip, 2007 (贪心)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. UVA 538 - Balancing Bank Accounts(贪心)

    UVA 538 - Balancing Bank Accounts 题目链接 题意:给定一些人的欠钱关系,要求在n-1次内还清钱,问方案 思路:贪心,处理出每一个人最后钱的状态,然后直接每一个人都和最 ...

  9. UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

随机推荐

  1. hdu(2846)Repository

    Problem Description When you go shopping, you can search in repository for avalible merchandises by ...

  2. 含神经网络的离线AI翻译 APP

    功能特性 下载 https://www.microsoft.com/en-us/store/p/translator/9wzdncrfj3pg

  3. DirectUI界面编程(一)创建第一个应用

    1.获取Duilib库文件 通过上一节大家对DirectUI界面设计有了初步的了解,本节开始我们一起学习Duilib界面库的使用. 首先我们需要获取Duilib库,目前最新版本为2.0,最新版本源码托 ...

  4. <错误>

    1. package com.multak.cookaraclient.adapter; import android.content.Context; import android.support. ...

  5. python学习——正则表达式

    正则表达式                        正则表达式的主要作用就是对字符串进行匹配,通过匹配,进行筛选,选择出符合规则的字符串.其实正则表达式的作用就像是一个过滤器一样.你输入好自己的 ...

  6. 转:Hibernate使用SQLQuery

    原文:http://hi.baidu.com/luo_qing_long/blog/item/783a15eceb75abdd2f2e21b0.html 对原生SQL查询执行的控制是通过SQLQuer ...

  7. firefox工具

    1.XPath 查看元素的xpath https://addons.mozilla.org/zh-CN/firefox/addon/xpath-checker/ 2. Tamper Data 查看页面 ...

  8. 第十一章 Python之异常处理

    异常 异常时程序运行时发生错误的信号(在程序错误时,则会产生一个异常,若程序没有处理,则会抛出该异常,程序的运行也随之终止) 常见的异常类型AttributeError 试图访问一个对象没有的树形,比 ...

  9. MySQL---Day2

    -- 转载:http://www.cnblogs.com/yuanchenqi/articles/6357507.html CREATE TABLE employee1( id TINYINT PRI ...

  10. 说说Shell在代码重构中的应用

    说说Shell在代码重构中的应用    出处信息 出处:http://blogread.cn/it/article/3426?f=wb 代码重构(Code refactoring)有时是很枯燥的,字符 ...