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

  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

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

  1. 1: #include <iostream>

  1. 2: #include <cstdio>

  1. 3: #include <cstring>

  1. 4: #include <algorithm>

  1. 5: using namespace std;

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

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

  1. 8: typedef long long ll;

  1. 9: const int maxn=555555;

  1. 10: int col[maxn<<2];

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

  1. 12: 

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

  1. 14: {

  1. 15: col[rt]=0;

  1. 16: if(l==r) return ;

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

  1. 18: build(lson);

  1. 19: build(rson);

  1. 20: }

  1. 21: 

  1. 22: int find(int x)

  1. 23: {

  1. 24: int l=1,r=n;

  1. 25: while(l<=r)

  1. 26: {

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

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

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

  1. 30: else r=m-1;

  1. 31: }

  1. 32: return 0;

  1. 33: }

  1. 34: 

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

  1. 36: {

  1. 37: col[rt]++;

  1. 38: if(l==r) return ;

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

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

  1. 41: else update(p,rson);

  1. 42: }

  1. 43: 

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

  1. 45: {

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

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

  1. 48: int ans=0;

  1. 49: if(p<=m)

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

  1. 51: else

  1. 52: ans=query(p,rson);

  1. 53: return ans;

  1. 54: }

  1. 55: 

  1. 56: int main()

  1. 57: {

  1. 58: int i;

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

  1. 60: {

  1. 61: build(1,n,1);

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

  1. 63: {

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

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

  1. 66: }

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

  1. 68: ll sum=0;

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

  1. 70: {

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

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

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

  1. 74: }

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

  1. 76: }

  1. 77: return 0;

  1. 78: }

POJ 2299 Ultra-QuickSort(线段树入门)的更多相关文章

  1. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  2. POJ 2808 校门外的树(线段树入门)

    题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...

  3. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  4. POJ 3264 线段树入门解题报告

    题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...

  5. poj 3841 Double Queue (AVL树入门)

    /****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...

  6. HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  7. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  8. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  9. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  10. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. IOS 之 PJSIP 笔记(二) iPJSUA 的简单使用

    上一篇在编译完之后,就很不负责的结束了,本篇就对 PJSIP 库中提供的一个示例 iPJSUA 的使用,做一个简单的介绍.也能解除很多人对官方文档的一个困扰,起码我是被困扰过了. 首先,要确保你的 P ...

  2. Winfrom中ListBox绑定List数据源更新问题

    Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...

  3. 基于吉日嘎底层架构的Web端权限管理操作演示-角色管理

    上一篇介绍了用户管理,这篇来介绍角色管理,这是权限管理的核心部分,因为我们的权限管理系统是基于角色的,有个高大上的英文名叫RBAC(Role Based Acccess Control). 下面的这段 ...

  4. jquery自定义插件——window的实现

    本例子实现弹窗的效果: 1.jquery.show.js /* * 开发者:lzugis * 开发时间:2014年6月10日 * 实现功能:点击在鼠标位置显示div * 版本序号:1.0 */ (fu ...

  5. Struts2中 Result类型配置详解

    一个result代表了一个可能的输出.当Action类的方法执行完成时,它返回一个字符串类型的结果码,框架根据这个结果码选择对应的result,向用户输出.在com.opensymphony.xwor ...

  6. 什么是CSR证书申请文件?

      CSR是Cerificate Signing Request的英文缩写,即证书请求文件,在多方之间在互联网上安全分享数据的公钥基础架构PKI系统中,CSR文件必须在申请和购买SSL证书之前创建.也 ...

  7. maven nexus deploy方式以及相关注意事项

    以前公司都是配管负责管理jar的,现在没有专职配管了,得自己部署到deploy上供使用.总的来说,jar部署到nexus上有两种方式: 1.直接登录nexus控制台进行上传,如下: 但是,某些仓库可能 ...

  8. Intellij idea开发Hadoop MapReduce程序

    1.首先下载一个Hadoop包,仅Hadoop即可. http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.6.0/hadoop-2.6.0 ...

  9. [Tool] 使用Astah绘制UML图形

    [Tool] 使用Astah绘制UML图形 前言 在软件开发的过程中,开发人员可以绘制UML图形来将分析设计内容转化为图形化文件,方便在团队之间传递分析设计结果.但在团队经费有限的情景中,可能没办法为 ...

  10. PHP调用SQL Server存储过程

    一.安装SQL Server Driver for PHP     在微软官网上发现了这个东西,他提供了一套PHP对MS2005/2008操作的全新函数库,并且支持UTF8,作为PHP的扩展运行.看来 ...