Sort it

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4679    Accepted Submission(s): 3250

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
 
 

题意很好理解,直接就想到冒泡排序了。

然而智障,在写的时候wa了一次。。。

代码:

#include<stdio.h>
int main()
{
int a[];
int i,j,t,n,ans;
while(~scanf("%d\n",&n)){
for(i=;i<n;i++)
scanf("%d",&a[i]);
ans=;
for(j=;j<n-;j++){ //是n-1,不是n。。。
for(i=;i<n-j-;i++) //是n-j-1,不是n-j,智障
if(a[i]>a[i+]){
t=a[i];
a[i]=a[i+];
a[i+]=t;
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

HDU 2689.Sort it-冒泡排序的更多相关文章

  1. HDU 2689 Sort it (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...

  2. hdu 2689 Sort it

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 题目分析:求至少交换多少次可排好序,可转换为逆序对问题. 用冒泡排序较为简单,复杂度较大~~ 也 ...

  3. HDU 2689 Sort it【树状数组】

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. HDU - 2689 Sort it与2016蓝桥杯B 交换瓶子 排序(相邻交换与任意交换)

    Sort it You want to processe a sequence of n distinct integers by swapping two adjacent sequence ele ...

  5. HDU 2689 sort it - from lanshui_Yang

    Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent ...

  6. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. hdu 2689

    hdu 2689 超级大水题....两种代码都过了,开始以为n^2会tle,后来竟然过了...汗 注意下cin写在while里面,就可以了 #include <iostream> usin ...

  8. hdu 1425 sort 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...

  9. HDU 5884 Sort (二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5884 nn个有序序列的归并排序.每次可以选择不超过kk个序列进行合并,合并代价为这些序列的长度和.总的 ...

随机推荐

  1. ActiveMQ+Zookeeper集群配置文档

    Zookeeper + ActiveMQ 集群整合配置文档 一:使用ZooKeeper实现的MasterSlave实现方式 是对ActiveMQ进行高可用的一种有效的解决方案, 高可用的原理:使用Zo ...

  2. Codeforces Round #392(Div 2) 758F(数论)

    题目大意 求从l到r的整数中长度为n的等比数列个数,公比可以为分数 首先n=1的时候,直接输出r-l+1即可 n=2的时候,就是C(n, 2)*2 考虑n>2的情况 不妨设公比为p/q(p和q互 ...

  3. 【题解】Bzoj4316小C的独立集

    决定要开始学习圆方树 & 仙人掌相关姿势.加油~~ 其实感觉仙人掌本质上还是一棵树,长得也还挺优美的.很多的想法都可以往树的方面上靠,再针对仙人掌的特性做出改进.这题首先如果是在树上的话那么实 ...

  4. [luogu P1442] 铁球落地

    题目链接 线段树\(+dp\). 先用线段树预处理出每个线段从左边和右边掉落到哪里,记为\(f[i][0/1]\). 然后记\(g[i][0/1]\)为到达第\(i\)个线段的左边或右边所要的最小时间 ...

  5. [洛谷P1801]黑匣子_NOI导刊2010提高(06)

    题目大意:两个操作:向一个可重集中加入一个元素:询问第$k$大的数($k$为之前询问的个数加一) 题解:离散化,权值线段树直接查询 卡点:无 C++ Code: #include <cstdio ...

  6. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. BZOJ4569 [SCOI2016]萌萌哒 【并查集 + 倍增】

    题目链接 BZOJ4569 题解 倍增的思想很棒 题目实际上就是每次让我们合并两个区间对应位置的数,最后的答案\(ans = 9 \times 10^{tot - 1}\),\(tot\)是联通块数, ...

  8. The 13th Zhejiang Provincial Collegiate Programming Contest - I

    People Counting Time Limit: 2 Seconds      Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...

  9. ShareSDK入门指南:Android 10分钟快速集成

    ShareSDK 官方已提供Android 快速集成教程,以官方教程为参考,本文重点指导大家在集成中遇到的问题. Android 快速集成官方教程:http://wiki.mob.com/Androi ...

  10. 使用HTML实现对汉字拼音的支持

    <!DOCTYPE HTML><html> <head> <meta charset="utf-8"> <title>无 ...