POJ-2299 Ultra_QuickSort 线段树+逆序对数
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 线段树+逆序对数的更多相关文章
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- POJ 2828 线段树 逆序插入
思路: 1.线段树 逆着插入就OK了 2.块状链表 (可是我并不会写) //By SiriusRen #include <cstdio> #include <cstring> ...
- POJ 2299 Ultra-QuickSort(线段树+离散化)
题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...
- POJ 2299 Ultra-QuickSort 线段树
题目链接 题意:求冒泡排序的交换次数,即求逆序数,即求对于每个数前面有多少个数比他大,n < 500,000,0 ≤ a[i] ≤ 999,999,999. 题解:因为值较大,个数较少,所以我们 ...
- POJ 2299 Ultra-QuickSort(线段树入门)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Description In this problem, you have to ana ...
- POJ 2299 离散化线段树
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 40827 Accepted ...
- POJ 2299 Ultra-QuickSort (求序列的逆序对数)
题意:废话了一大堆就是要你去求一个序列冒泡排序所需的交换的次数. 思路:实际上是要你去求一个序列的逆序队数 看案例: 9 1 0 5 4 9后面比它小的的数有4个 1后面有1个 0后面没有 5后面1个 ...
- Ultra-QuickSort(树状数组求逆序对数)
Ultra-QuickSort 题目链接:http://poj.org/problem?id=2299 Time Limit: 7000MS Memory Limit: 65536K Total ...
- POJ 1840 Brainman(逆序对数)
题目链接:http://poj.org/problem?id=1804 题意:给定一个序列a[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...
随机推荐
- JavaWeb学习----JSP脚本元素、指令元素、动作元素
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- linux启动jmeter,执行./jmeter.sh报错解决方法
1.l-bash: ./jmeter.sh: Permission denied解决办法:jmeter.sh的执行权限改改,是权限不够chmod 777 jmeter.sh 2.An error oc ...
- SVN版本回退
[SVN版本回退] 在Windows里,先打开Log面板,根据想要回退的内容,然后选择revert to this revision或者revert changes from this revisio ...
- 一个简单的python爬虫,以豆瓣妹子“http://www.dbmeizi.com/category/2?p= ”为例
本想抓取网易摄影上的图,但发现查看html源代码时找不到图片的url,但firebug却能定位得到.(不知道为什么???) 目标是抓取前50页的爆乳图,代码如下: import urllib2,url ...
- Spring小练习之宝宝淘项目
数据库准备 # 表结构 CREATE TABLE `t01_user` ( `) NOT NULL AUTO_INCREMENT COMMENT '自增主键', `) DEFAULT NULL COM ...
- HAXM cannot be installed nutil TV-x is enabled
提示错误:如图 HAXM cannot be installed nutil TV-x is enabled 问题原因: 电脑没有启动Intel的虚拟化技术 解决方法: 重启电脑,进BIOS中启动VT ...
- IIS增加并发数
网站出现这样的错误信息:Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@appConcurrentRequ ...
- 2015年新版C#从入门到精通(第2版)视频教学录像【无水印版】
<c#从入门到精通(第2版)>以零基础讲解为宗旨,用实例引导读者学习,深入浅出地介绍了c#的相关知识和实战技能.<c#从入门到精通(第2版)>第1篇[c#语言基础]主要讲解c# ...
- linux查看系统信息命令
本文转载自江一<linux查看系统信息命令> # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /p ...
- [CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字
7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟 ...