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个数组总的长度之和.现在另外一个屌丝要 ...
随机推荐
- Topcomponent --sighoff
Topcomponent --sighoff 实现一个置于右边框的Topcomponent: 1.可远程同步更新(根据远程的xml文件),修改(增删)该xml文件,查看.刷新等 2.打包工程,记录该打 ...
- JavaEE系列之(二)commons-fileupload实现文件上传、下载
一.文件上传概述 实现Web开发中的文件上传功能,需要两步操作: 1.在Web页面中添加上传输入项 <form action="#" method=" ...
- 使用openssl工具生成证书
第一步. 生成rsa私钥文件 :\> openssl genrsa -out bexio.pem 1024 : 若要加密生成的rsa私钥文件(des3加密) :\> openssl gen ...
- javascript实现单例模式
1.简单实现单例模式: var singleTon = function(){ var _pria = 'private value'; var show_pria = function(){ con ...
- ORCLE INNODB 博客与 innodb_lru_scan_depth
https://blogs.oracle.com/mysqlinnodb/ http://mysqllover.com/?p=485 •MySQL. MySQL 5.6.10 http://www.m ...
- 代码片段 - Golang 实现集合操作
------------------------------------------------------------ 如果用于多例程,可以使用下面的版本: -------------------- ...
- javascript函数中的实例对象、类对象、局部变量(局部函数)
定义 function Person(national,age) { this.age = age; //实例对象,每个示例不同 Person.national = national; //类对象,所 ...
- label
label的使用 以前只知道使用并没太注意一些细节 话说<label><input type="checkbox"/>5星</label>就可以 ...
- 沈逸老师PHP魔鬼特训笔记(6)--巫术与骨架
PHP最牛逼的特性之一除了懒人函数,还有一些魔法函数. 首先我们来认识下__tostring,通过一个巫术方法,我们吧实例转化过后的类,直接当字符串输出.结合我们前面所做的功能,我们在类里面加上这个函 ...
- Android 珍藏(二)
一.如何控制Android LED等?(设置NotificationManager的一些参数) 代码如下: final int ID_LED=19871103; NotificationManage ...