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个数组总的长度之和.现在另外一个屌丝要 ...
随机推荐
- TOJ3649欧拉回路
欧拉回路 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte Total Submit: 35 ...
- SQL Server 2008数据库重命名方法
假设SQL Server 2008中有个数据库test,现在要将其改名为zhy步骤:(1) 分离数据库:打开management studio,找到test数据库-->右键-->任务--& ...
- iOS开发——实用技术OC篇&单例模式的实实现(ACR&MRC)
单例模式的实实现(ACR&MRC) 在iOS开发中单例模式是一种非常常见的模式,虽然我们自己实现的比较少,但是,系统却提供了不少的到来模式给我们用,比如最常见的UIApplication,No ...
- lambda形式(转)
lambda语句被用来创建新的函数对象,并且在运行时返回它们. 使用lambda形式 #!/usr/bin/python# Filename: lambda.py def make_repeater( ...
- FlatUI博皮制作
Bootstrap3出来了,抛开内部框架结构和css命名的变化,bs3最大的改变莫过于扁平化. 扁平化UI中的典范,除了Metro,就是Flat了.目前本人FlatUI中毒中,于是开始慢慢的开始将博皮 ...
- Linux下校验下载文件的完整性(MD5,SHA1,PGP)
查看: Linux下校验下载文件的完整性(MD5,SHA1,PGP) http://blog.useasp.net/archive/2014/03/29/use-md5-sha1-or-pgp-to- ...
- iOS “请在微信客户端打开链接” UIWebview加载H5页面携带session、cookie、User-Agent信息 设置cookie、清除cookie、设置User-Agent
公司新开的一个项目..内容基本上是加载H5页面显示..当时觉得挺简单的..后来发现自己掉坑里了..一些心理历程就不说了..说这个项目主要用到的知识点吧..也是自己踩得坑. 首先说说..这个项目上的内容 ...
- iOS runtime 运行时( 三 )
上一篇 聊的是: 在不知道一个对象有什么属性的情况下, 获取对象的所有属性,然后可以改变属性的值, 今天我们谈谈 怎么通过Category(类别)+runtime 给一个类 添加一个新的属性,一般情况 ...
- javascript,jquery(闭包概念)
(function($){ $("div p").click(...);})(jQuery); 就是等于function tempFunction($){ //创建 ...
- linux_cpu信息查询
查看cpu信息: [root@css-management ~]# cat /proc/cpuinfo processor : 0vendor_id : GenuineIntelcpu family ...