您的位置:首页 > 产品设计 > UI/UE

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

2015-08-04 21:13 435 查看
Ultra-QuickSort

Time Limit: 7000MSMemory Limit: 65536K
Total Submissions: 48257Accepted: 17610
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
这道题可以总结的地方不少。
1:对于一组乱序数列,每次只能交换相邻元素,达到有序交换的次数就是原数列中你逆序对的个数。
  cf上好像总喜欢出这个题。。。我印象中就出现三次了。。。。。
[b]2:原始数组a[i]和树状数组的t[i]的对应问题(???存在疑问。。。应该只是这道题,而不是一般规律!)[/b]

  这道题n是500000,如果直接开数组是可以开得下的,不需要离散化。但是树状数组的下标对应的是原始数组的值!也就是t[i]的下表最大可能为999,999,999 ! 显然存不下,需要离散化。
[b]3:学习了离散化的又一种写法。
[/b]

/*************************************************************************
> File Name: code/poj/2299.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created Time: 2015年08月04日 星期二 12时27分32秒
************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=5E5+7;
int n;
int t
;
int ref
;

struct Q
{
int val,id;
}q
;

bool cmp(Q a,Q b)
{
if (a.val<b.val) return true;
return false;
}
int lowbit( int x)
{
return x&(-x);
}
void update ( int x,int c)
{
for ( int i = x ; i < N ; i = i + lowbit(i))
{
t[i] = t[i] + c;
}
}
LL Sum( int x)
{
LL res = 0;
for ( int i = x; i >= 1 ; i = i - lowbit(i))
{
res = res + t[i];
}
return res;
}
int main()
{
while (~scanf("%d",&n)&&n)
{
memset(t,0,sizeof(t));
for ( int i = 1 ; i  <= n ; i++ )
{
scanf("%d",&q[i].val);  //离散化的时候相对大小不能打乱
q[i].id = i;
}
sort(q+1,q+n+1,cmp);
for ( int i = 1 ; i <= n ; i++ )
{
ref[q[i].id] = i;
}
LL ans = 0;
for ( int i = 1 ; i <= n ; i++ )
{
update(ref[i],1);
ans = ans + i-Sum(ref[i]);
//  cout<<"ans:"<<ans<<endl;

}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: