Ultra-QuickSort

Time Limit: 7000MS
Memory Limit: 65536K

Description

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

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

::本题其实就是要求出有多少逆序对。本题还要虚拟化,因为0<=a[i]<=999,999,999,开一个数组大小为1,000,000,000*4铁定超内存

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: using namespace std;

   6: #define lson l,m,rt<<1

   7: #define rson m+1,r,rt<<1|1

   8: typedef long long ll;

   9: const int maxn=555555;

  10: int col[maxn<<2];

  11: int a[maxn],b[maxn],n;

  12:  

  13: void build(int l,int r,int rt)

  14: {

  15:     col[rt]=0;

  16:     if(l==r) return ;

  17:     int m=(r+l)>>1;

  18:     build(lson);

  19:     build(rson);

  20: }

  21:  

  22: int find(int x)

  23: {

  24:     int l=1,r=n;

  25:     while(l<=r)

  26:     {

  27:         int m=(l+r)>>1;

  28:         if(x==a[m]) return m;

  29:         if(x>a[m]) l=m+1;

  30:         else r=m-1;

  31:     }

  32:     return 0;

  33: }

  34:  

  35: void update(int p,int l,int r,int rt)

  36: {

  37:     col[rt]++;

  38:     if(l==r) return ;

  39:     int m=(l+r)>>1;

  40:     if(p<=m) update(p,lson);

  41:     else update(p,rson);

  42: }

  43:  

  44: int query(int p,int l,int r,int rt)

  45: {

  46:     if(p<=l) return col[rt];

  47:     int m=(l+r)>>1;

  48:     int ans=0;

  49:     if(p<=m)

  50:         ans=col[rt<<1|1]+query(p,lson);

  51:     else

  52:         ans=query(p,rson);

  53:     return ans;

  54: }

  55:  

  56: int main()

  57: {

  58:     int i;

  59:     while(scanf("%d",&n),n)

  60:     {

  61:         build(1,n,1);

  62:         for(i=1; i<=n; i++)

  63:         {

  64:             scanf("%d",a+i);

  65:             b[i]=a[i];

  66:         }

  67:         sort(a+1,a+n+1);//让数组a升序排序,那么等下b就可以通过a来求出对应的树是第几大(这算是虚拟化吧)

  68:         ll sum=0;

  69:         for(i=1; i<=n; i++)

  70:         {

  71:             int t=find(b[i]);

  72:             sum+=(ll)query(t,1,n,1);

  73:             update(t,1,n,1);

  74:         }

  75:         printf("%lld\n",sum);

  76:     }

  77:     return 0;

  78: }

最新文章

  1. WPF中TreeView的使用
  2. C#中标准Dispose模式的实现与使用(条目17 实现标准的销毁模式)
  3. xml json protobuf
  4. Digit Counts
  5. Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别
  6. EF4.1之Code first 的几种连接数据库的方式
  7. O-C相关-10-动态类型检查
  8. C# 控制台程序 托盘图标 事件响应
  9. (转) mac 下的eclipse 配置 python 2.7
  10. iOS8.0以后的相册
  11. 一道面试题引发的对javascript类型转换的思考
  12. Angular 小试牛刀[1]:Getting Started
  13. org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manu
  14. 大学jsp实验七--JavaBean在JSP中的应用
  15. hdu 5877 Weak Pair (Treap)
  16. hdu 5274 Dylans loves tree (树链剖分 + 线段树 异或)
  17. Windows未能启动:0xc00000e9错误
  18. D:\hunting2014\小题目\字符串倒序
  19. SQL语句帮助大全
  20. String、Date、Calendar之间的转换

热门文章

  1. 树莓派配置静态ip
  2. c#对Aspose.Word替换书签内容的简单封装
  3. 使用Dhcpstarv解决DHCP服务器冲突问题
  4. 继续转 [转]php版本的cron定时任务执行器
  5. Hibernate Validator
  6. DP---Mahjong tree
  7. Webhooks PHP
  8. Html==&gt;&gt;一些经典
  9. python peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.
  10. C++ 面向对象的三个特点--多态性(二)