hdu 2689 Sort it
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689
题目分析:求至少交换多少次可排好序,可转换为逆序对问题。 用冒泡排序较为简单,复杂度较大~~ 也可用归并排序,复杂度O(lognn), 统计个数后复杂都不变。
/*
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2660 Accepted Submission(s): 1910 Problem Description
You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.
For example, 1 2 3 5 4, we only need one operation : swap 5 and 4. Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n. Output
For each case, output the minimum times need to sort it in ascending order on a single line. Sample Input
3
1 2 3
4
4 3 2 1 Sample Output
0
6 Author
WhereIsHeroFrom Source
ZJFC 2009-3 Programming Contest */
//冒泡排序
#include <cstdio>
const int maxn = + ;
int a[maxn];
void swap(int i, int j)
{
int t;
t = a[i];
a[i] = a[j];
a[j] = t;
} int main()
{
int n;
while(~scanf("%d", &n)){
int cnt = ;
for(int i = ; i < n; i++) scanf("%d", &a[i]);
for(int i = ; i < n-; i++)
for(int j = n-; j >= i+; j--){
if(a[j] < a[j-]){
swap(j, j-);
cnt++;
}
}
printf("%d\n", cnt);
}
return ;
} //归并排序
#include <cstdio>
#include <cstring>
const int maxn = + ;
int a[maxn], t[maxn], cnt;
void merge_sort(int x, int y)
{
if(y-x > ){
int m = x + (y-x)/;
int p = x, q = m, i = x;
merge_sort(x, m);
merge_sort(m, y);
while(p < m || q < y){
if(q >= y || (p < m && a[p] <= a[q])) t[i++] = a[p++];
else {
t[i++] = a[q++];
cnt += m-p;
}
}
for(i = x; i < y; i++) a[i] = t[i];
}
} int main()
{
int n;
while(~scanf("%d", &n)){
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
}
cnt = ;
merge_sort(, n);
printf("%d\n", cnt);
}
return ;
}
hdu 2689 Sort it的更多相关文章
- HDU 2689 Sort it (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...
- HDU 2689 Sort it【树状数组】
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 2689 sort it - from lanshui_Yang
Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent ...
- HDU 2689.Sort it-冒泡排序
Sort it Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)
Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...
- hdu 2689
hdu 2689 超级大水题....两种代码都过了,开始以为n^2会tle,后来竟然过了...汗 注意下cin写在while里面,就可以了 #include <iostream> usin ...
- hdu 1425 sort 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...
- HDU 5884 Sort (二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...
- HDU 5884 Sort(二分+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=5884 题意:有个屌丝设计了一个程序,每次可以将k个数组进行合并,代价为这k个数组总的长度之和.现在另外一个屌丝要 ...
随机推荐
- Android 开发中使用Intent传递数据的方法
Activity之间通过Intent传递值,支持基本数据类型和String对象及 它们的数组对象byte.byte[].char.char[].boolean.boolean[].short.shor ...
- 现有一些开源ESB总线的比較
现有的开源ESB总线中,自从2003年第一个开源总线Mule出现后,如今已经是百花争鸣的景象了.如今我就对现有的各种开源ESB总线根据性能.可扩展性.资料文档完整程度以及整合难易程度等方面展开. 一. ...
- 20 Free Open Source Web Media Player Apps
free Media Players (Free MP3, Video, and Music Player ...) are cool because they let web developers ...
- Linux 必掌握的 SQL 命令
数据库和 SQL 在本系列教程中,目前我们使用平面文本文件来存储数据.平面文本文件可能适合相对较少的数据,但它们对存储大量数据或查询该数据没有多大帮助.多年来,为该目的开发了多种数据库,包括分层和网络 ...
- linux用户权限
Linux下passwd和shadow文件内容详解 一./etc/passwd/etc/passwd 文件是一个纯文本文件,每行采用了相同的格式: name:password:uid:gid:comm ...
- PHP抓取网络数据的6种常见方法
http://www.nowamagic.net/academy/detail/12220245 http://www.nowamagic.net/academy/detail/12220245
- seleniu IDE 点点滴滴
在使用selenium webdriver +python 的过程中遇见了许多的问题,这些问题在网上都没有找到很好的答案,后来在看selenium IDE的时候发现这里面有很好的解决方法,写写.记记. ...
- 1.4.6 其他Schema元素
这里描述了schema.xml中的其他几个重要的元素. 唯一主键-Unique Key uniqueKey元素一篇文档的唯一标记,它几乎总是保证您的应用程序设计,例如,如果你更新索引中的文档,需要用到 ...
- 免费公测:RDS只读实例
免费公测:RDS只读实例 简要介绍 在对数据库有少量写请求,但有大量的读请求的应用场景下,单个实例可能无法抵抗读取压力, 甚至对主流程业务产生影响.为了实现读取能力的弹性扩展,分担数据库压力,阿里 ...
- javascript,jquery(闭包概念)
(function($){ $("div p").click(...);})(jQuery); 就是等于function tempFunction($){ //创建 ...