Ultra-QuickSort

Time Limit: 7000MS Memory Limit: 65536K

Total Submissions: 50737 Accepted: 18595

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

Source

Waterloo local 2005.02.05

题目大意:给定一个数列,求冒泡排序交换次数(逆序对数)

线段树求逆序对,在数据范围过大的时候,需要进行离散化,然后进行建树,基本题

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 500002
int sum[maxn<<2]={0};
struct data{
int num,loc;
};
int f2[maxn]={0};
data f1[maxn]={0}; int cmp(data x,data y)
{
return x.num<y.num;
} void updata(int now)
{
sum[now]=sum[now<<1]+sum[now<<1|1];
} void point_change(int now,int l,int r,int loc)
{
if (l==r)
{
sum[now]++;
return;
}
int mid=(l+r)>>1;
if (loc<=mid)
point_change(now<<1,l,mid,loc);
else
point_change(now<<1|1,mid+1,r,loc);
updata(now);
} int query(int L,int R,int l,int r,int now)
{
if (L<=l && R>=r)
return sum[now];
int mid=(l+r)>>1;
int ans=0;
if (L<=mid)
ans+=query(L,R,l,mid,now<<1);
if (R>mid)
ans+=query(L,R,mid+1,r,now<<1|1);
return ans;
} int main()
{
int n;
while (true)
{
scanf("%d",&n);
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
memset(sum,0,sizeof(sum));
if (n==0) break;
long long number=0;
for (int i=1; i<=n; i++)
{
scanf("%d",&f1[i].num);
f1[i].loc=i;
}
sort(f1+1,f1+n+1,cmp);
for (int i=1; i<=n; i++)
f2[f1[i].loc]=i;//离散化部分(感觉这个离散化写的巨不专业)
//for (int i=1; i<=n; i++)
//cout<<f2[i]<<' ';
//cout<<endl;
for (int i=1; i<=n; i++)
{
int x=f2[i];
number+=query(x,n,1,n,1);
point_change(1,1,n,x);
}//逆序对的求法,每次从这个数到n求和,找出比他大的且出现在它之前的
printf("%d",number);
}
return 0;
}

POJ-2299 Ultra_QuickSort 线段树+逆序对数的更多相关文章

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

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

  2. POJ 2828 线段树 逆序插入

    思路: 1.线段树 逆着插入就OK了 2.块状链表 (可是我并不会写) //By SiriusRen #include <cstdio> #include <cstring> ...

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

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

  4. POJ 2299 Ultra-QuickSort 线段树

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

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

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

  6. POJ 2299 离散化线段树

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted ...

  7. POJ 2299 Ultra-QuickSort (求序列的逆序对数)

    题意:废话了一大堆就是要你去求一个序列冒泡排序所需的交换的次数. 思路:实际上是要你去求一个序列的逆序队数 看案例: 9 1 0 5 4 9后面比它小的的数有4个 1后面有1个 0后面没有 5后面1个 ...

  8. Ultra-QuickSort(树状数组求逆序对数)

    Ultra-QuickSort 题目链接:http://poj.org/problem?id=2299 Time Limit: 7000MS   Memory Limit: 65536K Total ...

  9. POJ 1840 Brainman(逆序对数)

    题目链接:http://poj.org/problem?id=1804 题意:给定一个序列a[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...

随机推荐

  1. Adobe Scout 入门

    http://www.adobe.com/cn/devnet/scout/articles/adobe-scout-getting-started.html Adobe Scout 是新一代 Flas ...

  2. mantis安装

    curl -O http://jaist.dl.sourceforge.net/project/mantisbt/mantis-stable/1.2.19/mantisbt-1.2.19.tar.gz ...

  3. find命令错误提示路径必须在表达式之前

    在某些版本的linux下,通过find查找当前目录下所有后缀名jpg的文件,命令为find ./ -iname *.jpg 会出现“find: 路径必须在表达式之前”的错误提示.解决的方法有两种 a. ...

  4. HTTP请求与响应方式

    HTTP请求格式 当浏览器向Web服务器发出请求时,它向服务器传递了一个数据块,也就是请求信息,HTTP请求信息由3部分组成: l   请求方法URI协议/版本 l   请求头(Request Hea ...

  5. oracle wm_concat(column)函数的使用

    oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oraclewm_concat(column)函数实现字段合并,如果您对oracle wm_concat(c ...

  6. java 11-7String类里的方法的一些案例

    1. 把int数组拼接成字符串的案例 需求:把数组中的数据按照指定个格式拼接成一个字符串 举例: int[] arr = {1,2,3}; 分析: A:首先定义一个空的字符串 B:其次先给这个字符串拼 ...

  7. throw跟throws关键字

    throws关键字 定义一个方法的时候可以使用throws关键字声明.使用throws关键字声明的方法表示此方法不处理异常,而交给方法调用处进行处理. throws关键字格式: public 返回值类 ...

  8. 查询各个商品分类中各有多少商品的SQL语句

    SELECT goods_category_id ,count(*) FROM `sw_goods` group by goods_category_id

  9. docker下部署gitlab

    docker用来隔离应用还是很方便的,一来本身的操作较为简单,二来资源占用也比虚拟机要小得多,三来也较为安全,因为像数据库这样的应用不会再全局暴露端口,同时应用间的通信通过加密和端口转发,更加安全. ...

  10. ES6新增const常量、let变量

    JavaScript 严格模式(use strict) 严格模式下你不能使用未声明的变量. const c1 = 1; const c2 = {}; const c3 = []; 不能对c1的值进行再 ...