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个数组总的长度之和.现在另外一个屌丝要 ...
随机推荐
- 好记心不如烂笔头之JQuery学习,第三章
第三章中主要讲了几个对DOM进行操作的方法. 归纳如下: 属性的获取和设置: //属性的获取 $("li").attr("title"); //属性的设置 $( ...
- 汉化Eclipse+配色方法(官方语言包)
一. 汉化方法: 1.Eclipse版本查询:安装目录readme,查版本号;参照查代号如下表: 代号 平台版本 项目 主要版本发行日期 SR1发行日期 SR2发行日期 N/A 3.0 [1] N/A ...
- 0c-35-自动释放池使用注意
.autorelease使用注意 )并不是放到自动释放池中,都会自动加入到自动释放池 1.1) 因为没有调用autorelease方法,所以对象没有加入到自动释放池. int main(){ @aut ...
- IO 延迟与Queue Depth
IO 延迟:存储设备的IO延迟 Queue Depth:磁盘控制器所发出的批量指令的最大条数 IOPS:磁盘设备每秒的IO 三者之间的关系:IOPS=(Queue Depth)/(IO latenc ...
- Tcsh脚本编程
Tcsh主要用于Free BSD等UNIX系统中. 一.输出字符串Hello的示例脚本 Tcsh脚本的基本格式.编写方法及脚本中使用的命令等,与Bash脚本完全相同,只需要直接套用即可. [root@ ...
- mysql数据类型区别
create table t1(c1 float(10,2), c3 decimal(10,2)); insert into t1 values(1234567.23, 1234567.23,1234 ...
- Disable right click on the website
Many developers/website owners like to keep their website images personal and don't want anyone to c ...
- Java去除掉HTML里面所有标签的两种方法——开源jar包和自己写正则表达式
Java去除掉HTML里面所有标签,主要就两种,要么用开源的jar处理,要么就自己写正则表达式.自己写的话,可能处理不全一些自定义的标签.企业应用基本都是能找开源就找开源,实在不行才自己写…… 1,开 ...
- LeetCode45 Jump Game II
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- IOS 免受xib自动布局影响
1.适配苹果手机 iPhone 4 ,4s 一个xib iPhone 5 5s xib 以后iPhone6一个xib 2.再根据ios6 和 ios7 进行 微调 就是状态栏的那个问题 3.# ...