POJ 2299 Ultra-QuickSort(线段树入门)
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: }
POJ 2299 Ultra-QuickSort(线段树入门)的更多相关文章
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- POJ 2808 校门外的树(线段树入门)
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- POJ 3264 线段树入门解题报告
题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...
- poj 3841 Double Queue (AVL树入门)
/****************************************************************** 题目: Double Queue(poj 3481) 链接: h ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- [新手学Java]使用内省(Introspector)操作JavaBean属性
获取类bean中的所有属性: @Test //获取类bean中的所有属性 public void test1() throws Exception{ BeanInfo info = Introspec ...
- Wijmo金融图表系列之平均K线图&砖形图
2015年7月16日将会发布有史以来最令人兴奋的控件-Wijmo 金融图表,它的一体化设计为单个自定义集合提供了所有主要的金融图表,这是市场上的其他控件都不具备的独一无二的好处.它像Wijmo其他任意 ...
- Java、Hibernate(JPA)注解大全
1.@Entity(name=”EntityName”) 必须,name为可选,对应数据库中一的个表 2.@Table(name=””,catalog=””,schema=””) 可选,通常和@Ent ...
- php获取textarea的值并处理回车换行的方法
//注:\n是用双引号包的的,双引号!!双引号!!! explode("\n",$row[0]['value']
- 探索HashMap实现原理及其在jdk8数据结构的改进
因为网上已经太多的关于HashMap的相关文章了,为了避免大量重复,又由于网上关于java8的HashMap的相关文章比较少,至少我没有找到比较详细的.所以才有了本文. 本文主要的内容: 1.Hash ...
- 滴滴DSRC抢楼大赛,十一快车券飞起来
十一马上就要到了 不管你想要去哪里 总少不了滴滴打车 DSRC为你准备了30份快车券 现在开始参与抢楼,就有机会得到 活动时间: 本帖发布之时起-9月30日12:00 参与方式: 关注微信公众号“滴滴 ...
- 代码导出Reporting Services报表文件
背景部分 使用Reporting Services很容易制作和发布我们需要的报表,报表效果也还不错 不过如果报表数据过大或报表数量过多,打开及查看报表感觉可能就是另外一回事了 好在Reporting ...
- git学习笔记2
工作区和暂存区 Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的github文件 ...
- STL--迭代器(iterator)
指针与数组 指针与其它数据结构呢?比如说链表? 存储空间是非连续的.不能通过对指向这种数据结构的指针做累加来遍历. 能不能提供一个行为类似指针的类,来对非数组的数据结构进行遍历呢?这样我们就能够以同样 ...
- 【读书笔记】iOS-GCD-多线程编程
一,现在一个物理的CPU芯片实际上有64个(64核)CPU,如果1个CPU核虚拟为两个CPU核工作,那么一台计算机上使用多个CPU核就是理所当然的事了.尽管如此,“1个CPU核执行的CPU命令为一条无 ...