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[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...
随机推荐
- maya获取邻接顶点的一个问题
maya网格数据结构允许"非流形"的存在,于是,这种数据结构无法按顺序给出一个点的邻接顶点. 于是,MItMeshVertex::getConnectedVertices函数返回的 ...
- Mac必装app-持续更新
所有软件都是免费,或者有破解版 都可以在google上***for mac 搜索到 Google Chrome Microsoft Office Evernote Skype Alfred AppCl ...
- js中的垃圾回收机制
代码回收规则如下: 1.全局变量不会被回收. 2.局部变量会被回收,也就是函数一旦运行完以后,函数内部的东西都会被销毁. 3.只要被另外一个作用域所引用就不会被回收 (闭包)
- 17Mybatis_动态sql-sql片段
这篇文章讲一下sql片段. 讲一下sql片段的的需求: 将上边实现的动态sql判断代码块抽取出来,组成一个sql片段.其它的statement中就可以引用sql片段. 方便程序员进行开发. 第一步我们 ...
- Unity架构有点乱
1,没有合理的将公共的东西归入到基类中,而是分散到子类中,有许多重复. 比如 enbled的变量本应该是所有component所共有的一个属性,应该写在component.然而却发现并非这样,enbl ...
- php基础11:运算符
<?php $a = 5; $b = ++$a; echo '$a'.$a; echo "<br>"; echo '$b'.$b; echo "< ...
- PHP基础01:环境搭建
1.只会前端的只是有时候让我感到很苦恼,所以决定从今天开始学习后端,看了一些关于后端语言的比较帖子,决定选择php作为我的第一门后端语言.这个是我自己的学习笔记.方便自己复习,不写下来会太无聊了. 第 ...
- SignalR 实现web浏览器客户端与服务端的推送功能
SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话. 换句话说,该对话可不受限制地进行单个无状态请求/响应数据交换:它将继 ...
- Virtualbox下Ubuntu与主机Win7共享文件夹
记下来,免得老google. 1. 在虚拟机设置里设置好win7的共享文件夹位置:如c:\share 2.确定ubuntu下需要共享的文件夹,如~/linuxshare.注意,此文件夹名字必须与win ...
- 2016动作短片《全境封锁:特工起源》HD720P.英语中字
导演: 德文·格雷厄姆主演: Matt Lynch / Sasha Andreev / Amanda Day类型: 动作 / 短片制片国家/地区: 美国语言: 英语上映日期: 2016-01-19片长 ...