POJ-2299 Ultra-QuickSort(离散化+树状数组)

原创 2018年04月17日 18:04:16
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 67527 Accepted: 25288

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

代码:

#include <algorithm>  
#include <string.h>  
#include <iostream>  
#include <stdio.h>  
#include <string>  
#include <vector>  
#include <queue>  
#include <map>  
#include <set>  
#include<cmath>  
#include<cstdlib>  
using namespace std;
const int N = 1000005;
//离散化用的结构
struct node {
	int val;
	int pos;
}d[N];

int num[N],bit[N], n;//存放离散化后的数,树状数组 
int cmp(node x, node y) {
	return x.val < y.val;
}
int lowbit(int x) {
	return x&(-x);
}

void add(int x) {
	while (x <= n) {
		bit[x] += 1;
		x += lowbit(x);
	}
}

int  sum(int x) {
	int res = 0;
	while (x>0) {
		res += bit[x];
		x -= lowbit(x);
	}
	return res;
}

void init() {
	for (int i = 1; i <= n; i++) bit[i] = 0;
}

int main() {
	while (cin >> n, n) {
		//输入数组
		for (int i = 1; i <= n; i++) {
			d[i].pos = i;
			cin >> d[i].val;
		}
		//排序
		sort(d + 1, d + 1 + n, cmp);
		//离散化
		for (int i = 1; i <= n; i++) num[d[i].pos] = i;
		long long ans = 0;
		//初始化树状数组
		init();
		//进行计算
		for (int i = 1; i <= n; i++) {
			add(num[i]);
			ans += (i - sum(num[i]));
		}
		cout << ans << endl;
	}
	return 0;
}

POJ-2299 Ultra-QuickSort(树状数组)(离散化)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 56014   Accepted:...
  • huatian5
  • huatian5
  • 2016-09-03 11:19:23
  • 343

POJ 2299 Ultra-QuickSort (树状数组)

前段时间用归并排序写了这题,发现树状数组也能解这题,就去学习了一下 首先先来看一个序列   6 1 2 7 3 4 8 5,此序列的逆序数为5+3+1=9。冒泡法可以直接枚举出逆序数,但是时间复杂度...
  • wdkirchhoff
  • wdkirchhoff
  • 2014-12-09 22:16:54
  • 1670

Ultra-QuickSort POJ - 2299 (树状数组)

Ultra-QuickSort  POJ - 2299  树状数组模板题。 思路:看此篇博文之前,你要对树状数组有所了解,这里树状数组求的也是和,并且因为更新多次,所以不用树状数组减少复杂度的话 ...
  • duan_1998
  • duan_1998
  • 2017-02-22 21:29:48
  • 404

离散化+树状数组求逆序数

题目:POJ2299   离散化是一种常用的技巧,有时数据范围太大,可以用来放缩到我们能处理的范围 因为其中需排序的数的范围0---999 999 999;显然数组不肯能这么 大;而N的最大范围是50...
  • ACdreamers
  • ACdreamers
  • 2013-01-19 14:52:12
  • 4430

poj2299Ultra-QuickSort【线段树求逆序数】离散化

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52097 ...
  • zhou_yujia
  • zhou_yujia
  • 2016-03-15 16:48:59
  • 576

poj 2299 Ultra-QuickSort 求逆序数,树状数组解法,详细解析

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque...
  • Lionel_D
  • Lionel_D
  • 2015-02-05 20:55:37
  • 1256

树状数组求逆序对及离散化

树状数组求逆序对及离散化 逆序对指的是一个序列中有两个数ai和aj,iaj,即它们下标与数值的增减不一致,那么对于这个问题:求一个序列中逆序对的个数,该如何解决呢? 我最初接触到的方法是归并排序,...
  • qq_37666409
  • qq_37666409
  • 2017-08-26 22:59:15
  • 209

[离散化+树状数组]逆序对

看到这道题,我只能想到的思路是大暴力。。。我是蒟蒻。。去看了一下树状数组的介绍,还是半懂不懂,看了题解好长时间,才有点明白。树状数组传送门http://blog.csdn.net/int64ago/a...
  • jjqjinqiu
  • jjqjinqiu
  • 2016-05-01 12:27:31
  • 281

树状数组--离散化

- 二分 Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status ...
  • idealistic
  • idealistic
  • 2016-08-17 09:35:01
  • 366

树状数组求逆序数的模板(离散化处理)

最近刚接触线段树,今天刚弄懂树状数组;听说用树状数组解决逆序数很高效;所以也就整理了一下,随带整理了一下离散化; 把这个模板贴上,以后碰到逆序数的题目,就可以直接用了; 好吧,但是还是有一个很大的BU...
  • wlxsq
  • wlxsq
  • 2015-08-05 15:00:24
  • 409
收藏助手
不良信息举报
您举报文章:POJ-2299 Ultra-QuickSort(离散化+树状数组)
举报原因:
原因补充:

(最多只允许输入30个字)