Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 40827   Accepted: 14752

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

求冒泡排序交换的次数。
因为这些数可能太大。且差距非常大,所以离散化一下,然后求一下逆序数。边查询边插入边就可以。


//32684K	1579MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 500007
#define ll __int64
using namespace std;
int s[M],n;
struct Tree
{
int l,r,mid;
ll val;
}tree[M<<1];
struct sa
{
int id;
ll val;
}p[M*2];
int cmp(sa a,sa b)
{
return a.val>b.val;
}
void build(int left,int right,int i)
{
tree[i].l=left;tree[i].r=right;tree[i].mid=(left+right)>>1;tree[i].val=0;
if(left==right){return;}
build(left,tree[i].mid,i*2);
build(tree[i].mid+1,right,i*2+1);
}
int query(int x,int i)
{
if(tree[i].l==tree[i].r)return tree[i].val;
if(x<=tree[i].mid)return query(x,i*2)+tree[i].val;
else return query(x,i*2+1)+tree[i].val;
}
void insert(int left,int right,int i)
{
if(tree[i].l==left&&tree[i].r==right){tree[i].val++;return;}
if(right<=tree[i].mid)insert(left,right,2*i);
else if(left>tree[i].mid)insert(left,right,2*i+1);
else {insert(left,tree[i].mid,i*2);insert(tree[i].mid+1,right,i*2+1);}
}
void discretization()
{
int tmp=p[1].val,pos=1;
for(int i=1;i<=n;i++)
if(p[i].val!=tmp)p[i].val=++pos,tmp=p[i].val;
else p[i].val=pos;
for(int i=1;i<=n;i++)
s[p[i].id]=p[i].val;
}
int main()
{
while(scanf("%d",&n)&&n)
{
ll ans=0;
build(0,M,1);
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++)
{
scanf("%I64d",&p[i].val);
p[i].id=i;
}
sort(p+1,p+n+1,cmp);
discretization();
for(int i=1;i<=n;i++)
printf("%d ",s[i]);
printf("\n");
for(int i=1;i<=n;i++)
{
ans+=query(s[i],1);
insert(s[i],M,1);
}
printf("%I64d\n",ans);
}
return 0;
}

POJ 2299 离散化线段树的更多相关文章

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

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

  2. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  3. POJ 2299 Ultra-QuickSort 线段树

    题目链接 题意:求冒泡排序的交换次数,即求逆序数,即求对于每个数前面有多少个数比他大,n < 500,000,0 ≤ a[i] ≤ 999,999,999. 题解:因为值较大,个数较少,所以我们 ...

  4. POJ 2299 Ultra-QuickSort(线段树入门)

    Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Description In this problem, you have to ana ...

  5. D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询

    题意 贴海报 最后可以看到多少海报 思路 :离散化大区间  其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时  把y+1点也加入 ...

  6. poj/OpenJ_Bailian - 2528 离散化+线段树

    传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...

  7. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  8. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

  9. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

随机推荐

  1. [Luogu1273] 有线电视网

    [Luogu1273] 有线电视网 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树 ...

  2. Hadoop MapReduce编程 API入门系列之wordcount版本4(八)

    这篇博客,给大家,体会不一样的版本编程. 是将map.combiner.shuffle.reduce等分开放一个.java里.则需要实现Tool. 代码 package zhouls.bigdata. ...

  3. 从Oracle同步数据到SQLServer——大小写敏感设置

    Oracle默认是大小写敏感,而SQLServer默认大小写不敏感, 尤其是涉及主键字段时,注意请提前设置SQLServer对应的数据库表为大小写敏感,不然会报主键冲突的错误. 设置表内大小写敏感 A ...

  4. SQLServer inner join,left join,right join,outer join 备忘备忘

    LEFT JOIN LEFT JOIN 关键字会从左表那里返回所有的行,即使在右表中没有匹配的行. 即LEFT JOIN 的 ON 条件不会对数据行造成影响 RIGHT JOIN RIGHT JOIN ...

  5. Python 之 基础知识(四)

    一.公共方法(列表.元组.字典以及字符串) 1.内置函数 cmp函数取消可以用比较运算符来代替,但是字典是无序的,故而不可以用比较运算符比较. 2.切片(列表.元组.字符串适用) 3.运算符 列表中直 ...

  6. 将mysql默认编码改为UTF8

    windows: a. WIN+R  net stop mysql  关闭mysql服务 b. 复制my-dafault.ini,重命名为my.ini,进入里面 UBUNTU: ** sudo vim ...

  7. 【Oracle】闪回drop后的表

    本文介绍的闪回方式只适用于:删除表的表空间非system,drop语句中没有purge关键字(以上两种情况的误删除操作只能通过日志找回): 1.删除表后直接从回收站闪回 SCOTT@LGR> d ...

  8. 如何像Uber一样给工程师派单 解放外包落后的生产力

    2014年,陈柯好的第一个创业项目失败,半年之内,陈柯好以技术合伙人的方式游走于旅游.电商.团购.票务等各种领域.正当他对职业方向感到迷茫时,“大众创业.万众创新”的口号被提了出来 一时间,技术需求被 ...

  9. vc++如何创建程序01

    1 .选择文件+新建(ctrl+N),然后选择一个空的工程,完成 2 然后在选择file新建,在files文件下面选择一个C++Source File,并取个文件名(比如为point可以不带.c) 我 ...

  10. FastFDS常用命令

    1.启停fastdfs相关服务 #start fastdfs  启动服务 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart /usr/loca ...