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. 迷你DVD管理器项目

    package chapter5; import java.util.*; public class MiniDVD { public static void main(String[] args){ ...

  2. Linux下smokeping网络监控环境部署记录

    smokeping是一款监控网络状态和稳定性的开源软件(它是rrdtool的作者开发的),通过它可以监控到公司IDC的网络状况,如延时,丢包率,是否BGP多线等:smokeping会向目标设备和系统发 ...

  3. 22Spring_JdbcTemplatem模板工具类的使用——使用外部属性文件来配置(properties)

    前一篇文章写得是xml文件来配置数据库连接的.但是为了方便,我们实际中采用的是properties文件的方式来配置数据库的.修改properties 文件 会比 修改 xml文件 方便. 做法是: 将 ...

  4. 墙国内新建Rails应用的要点(windows 7环境, Rails 4.2.0)

    1. 使用rails new 命令创建完的应用在自动执行bundle install不会成功,根据出错提示,判断原因有可能是被墙与https的证书的安全性问题. 作为开发环境,选用绕开的办法,在目录  ...

  5. Studying-Swift :Day01

    学习地址:http://www.rm5u.com/    或    http://www.runoob.com/ 如果创建的是 OS X playground 需要引入 Cocoa;  如果我们想创建 ...

  6. react native 布局注意点

    一.react native中很多是ES6语法. 1行.表示是js的严格模式. 'use strict';严格模式中变量必须先声明,然后赋值.定义等:还有就是this的绑定. 2行到8行.导入依赖,可 ...

  7. HashMap 中的 entrySet()使用方法 2016.12.28

    package map; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import ...

  8. MVC出错案例之一:主外键映射失败

    今天在编写DomainModel和DomainMapper,最后放到OnModelCreating中运行的时候,给我抛出了如下错误: One or more validation errors wer ...

  9. Linux第13周学习笔记

    网络编程 客户端-服务器编程模型 每个网络应用都是基于客户端-服务器模型. 一个应用是由一个服务器进程和一个或者多个客户端进程组成. 服务器管理某种资源,并通过操作资源来为客户端提供某种服务. 基本操 ...

  10. UIStepper步进器 ——事件驱动型控件,(一个+和-按钮的)

    - (void)viewDidLoad {    [super viewDidLoad];        //步进器 固定的size (94*27), 事件驱动型控件    UIStepper *st ...