In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,

Ultra-QuickSort produces the output

0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

  1. 5
  2. 9
  3. 1
  4. 0
  5. 5
  6. 4
  7. 3
  8. 1
  9. 2
  10. 3
  11. 0

Sample Output

  1. 6
  2. 0
  3.  
  4. 题意:将一个序列从小到大排序,如果只能交换相邻的数,最少需要交换多少次
    思路:和冒泡排序一样,一个数需要交换的次数就是它的逆序对数,所以就是求总的逆序对的个数
  5.  
  6. 求逆序对可以用两种方法
    ①归并排序:
  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int n;
  6. const int maxn = 5e5+;
  7. int num[maxn];
  8. typedef long long ll;
  9.  
  10. ll Mersort(int l,int r)
  11. {
  12. int mid = (l+r)/;
  13. int i=l,j=mid+;
  14. int b[r-l+];
  15. int k=;
  16. ll ans = ;
  17. while(i <= mid && j <= r)
  18. {
  19. if(num[i] <= num[j])
  20. b[k++] = num[i++];
  21. else
  22. b[k++] = num[j++],ans+=mid-i+;
  23. }
  24. while(i <= mid)
  25. {
  26. b[k++] = num[i++];
  27. }
  28. while(j <= r)
  29. {
  30. b[k++] = num[j++];
  31. }
  32. for(int i=l; i<=r; i++)
  33. {
  34. num[i] = b[i-l+];
  35. }
  36. return ans;
  37. }
  38.  
  39. int Merge(int l,int r,ll &ans)
  40. {
  41. int mid = (l+r)/;
  42. if(l == r)
  43. return ;
  44. Merge(l,mid,ans);
  45. Merge(mid+,r,ans);
  46. ans += Mersort(l,r);
  47. }
  48. int main()
  49. {
  50. while(~scanf("%d",&n) && n)
  51. {
  52. for(int i=; i<=n; i++)
  53. scanf("%d",&num[i]);
  54. ll ans = ;
  55. Merge(,n,ans);
  56. printf("%lld\n",ans);
  57. }
  58. }
  1. ②树状数组:(要注意离散,离散可以二分,也可以map
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<string.h>
  5. using namespace std;
  6.  
  7. const int maxn = 5e5+;
  8. int n;
  9. int tree[maxn];
  10. typedef long long ll;
  11.  
  12. int lowbit(int x)
  13. {
  14. return x&(-x);
  15. }
  16.  
  17. void add(int x)
  18. {
  19. for(int i=x;i<=n;i+=lowbit(i))
  20. {
  21. tree[i]++;
  22. }
  23. }
  24.  
  25. int Query(int x)
  26. {
  27. int ans = ;
  28. for(int i=x;i>;i-=lowbit(i))
  29. {
  30. ans+=tree[i];
  31. }
  32. return ans;
  33. }
  34. int query(int x,int n,int *b)
  35. {
  36. return lower_bound(b+,b++n,x) - b;
  37. }
  38. int main()
  39. {
  40. while(~scanf("%d",&n) && n)
  41. {
  42. memset(tree,,sizeof(tree));
  43. int a[n+],b[n+];
  44. for(int i=;i<=n;i++)scanf("%d",&a[i]),b[i] = a[i];
  45. sort(b+,b++n);
  46. int len = unique(b+,b++n)-b-;
  47. ll ans = ;
  48. for(int i=;i<=n;i++)
  49. {
  50. int pos = query(a[i],len,b);
  51. add(pos);
  52. ans += pos - - Query(pos-);
  53. }
  54. printf("%lld\n",ans);
  55. }
  56. }
  1.  

Ultra-QuickSort POJ - 2299 (逆序对)的更多相关文章

  1. POJ 2299 逆序对

    Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...

  2. POJ 1804 逆序对数量 / 归并排序

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12175   Accepted: 6147 Descrip ...

  3. poj 2299 逆序数

    http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...

  4. poj2299——逆序对

    题目:http://poj.org/problem?id=2299 逆序对,注意树状数组维护后缀和. 代码如下: #include<iostream> #include<cstdio ...

  5. 【POJ】2299 Ultra-QuickSort(逆序对)

    http://poj.org/problem?id=2299 在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后 ...

  6. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  7. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  8. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  9. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  10. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

随机推荐

  1. HTML阻止冒泡事件的发生

    阻止事件冒泡函数(低级标签的点击事件触发后,上级标签的点击事件再触发,此函数就是防止冒泡事件发生) function stopEventBubble(event){ var e=event || wi ...

  2. bat脚本(转)

    偶尔用到,搜到不错的资料,所以转一下: windows bat脚本for循环中对变量循环赋值 http://blog.csdn.net/u010161379/article/details/50956 ...

  3. Confluence 6 找到在创建 XML 备份的时候出现的错误

    错误可能是因为数据库突然不可访问而产生.如果你在你的日志中看到了错误  'Couldn't backup database data' ,这个指南将会帮助你更正这个错误.我们强烈推荐你备份 Confl ...

  4. vuex action 与mutations 的区别

    面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...

  5. Netty简单聊天室

    1.创建maven项目,在pom.xml中引入netty的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...

  6. ZenMap扫描笔记

    1.软件界面如下,ZenMap 扫描工具是kali linu中对WEB渗透扫描的一款工具

  7. Eclipse搭建C++\C开发环境

    1.最近使用visualStudio IDE开发Unity 3D使用的编程语言是C#但是发现visualStudio12 版本在自己主机上运行速度比够快,怀疑是不是处理器或者是版本问题,所以该卸载了, ...

  8. poj1155 依赖背包

    /* 依赖背包 dp[i][j]表示i结点为根的树选择j个用户时的最大剩余费用 即背包容量是j,价值是最大费用 */ #include<iostream> #include<cstr ...

  9. python+selenium十四:xpath和contains模糊匹配

    xpath可以以标签定位,也可以@任意属性: 如:以input标签定位:driver.find_element_by_xpath("//input[@id='kw']") 如:@t ...

  10. vue和stylus在subline中显示高亮

    首先: 安装这两个插件   Vue Syntax Highlight    和    stylus 1.按住 ctrl + shift + p 2.输入:install Package 3.输入: V ...