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[],每次只允许交换相邻两个数,最少要交换多少次才能把它变成非递降序列. 思路:题目就是要求逆序对数,我们知 ...
随机推荐
- 看程序写结果(program)
看程序写结果(program) Time Limit:1000ms Memory Limit:64MB 题目描述 LYK 最近在准备 NOIP2017 的初赛,它最不擅长的就是看程序写结果了,因此它拼 ...
- MonoDevelop line endings
文件编码问题 这个让我头疼很久的问题,每次修改文件后,都会出现这个提示框. 解决办法 之前修改 D:\Program Files (x86)\Unity\Editor\Data\Resources\S ...
- jira与readmine区别
JIRA适合多人的团队(100+),而Readmine适合中小型团队. Redmine是用ruby开发的基于web的项目管理软件,免费.JIRA收费Redmine可以创建子任务,而jira不易创建子任 ...
- java 16 -4 LinkedList的特有功能
了解 LinkedList的特有功能: A:添加功能 public void addFirst(Object e) public void addLast(Object e) B:获取功能 publi ...
- 数字转换为壹仟贰佰叁拾肆的Java方法
网银转帐时, 填写金额后下方出现的汉字金额, 这是Java下的实现. public static String toRMB(double money) { char[] s1 = {'零', '壹', ...
- 【转】【C#】C# 垃圾回收机制
摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...
- 深入了解view以及自定义控件
参考文章: http://blog.csdn.net/guolin_blog/article/details/12921889 Android LayoutInflater原理分析,带你一步步深入了解 ...
- 异步编程 In .NET(转)
转自:http://www.cnblogs.com/jesse2013/p/Asynchronous-Programming-In-DotNet.html 概述 在之前写的一篇关于async和awai ...
- windows live writer 尝试登陆时发生意外错误,导致无法发表博客解决方案
刚用windows live writer发表博客, 但是出现如下提示: 尝试登陆时发生意外错误: 网络连接错误--尝试连接到一下日志时出错: http://www.cnblogs.com//xxxx ...
- [CareerCup] 3.4 Towers of Hanoi 汉诺塔
3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes ...