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

POJ 2299 Ultra-QuickSort (求逆序数)

2013-04-19 15:51 387 查看
Ultra-QuickSort

Time Limit: 7000MS

Memory Limit: 65536K
Total Submissions: 31927Accepted: 11375
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

水题,求逆序数。

做法一:归并排序  3772k 360ms

#include <iostream>
#include <cstdio>

using namespace std;

int first[1000005],temp[1000005];
__int64 ans;

void merge(int low,int mid,int high)
{
int i=low,j=mid+1,k=low;
while(i<=mid && j<=high)
{
if(first[i]<=first[j])
{
temp[k++]=first[i++];
}
else
{
temp[k++]=first[j++];
ans += (mid-i+1);
}
}
while(i<=mid)
{
temp[k++]=first[i++];
}
while(j<=high)
{
temp[k++]=first[j++];
}
for(i=low;i<=high;i++)
first[i]=temp[i];
}

void mergeSort(int a,int b)
{
if(a<b)
{
int mid=(a+b)/2;
mergeSort(a,mid);
mergeSort(mid+1,b);
merge(a,mid,b);
}
}

int main()
{
int N;
while(cin>>N && N)
{
ans=0;
for(int i=0;i<N;i++)
scanf("%d",&first[i]);
mergeSort(0,N-1);
printf("%I64d\n",ans);
}
return 0;
}
 
 
 
 
做法二:线段树+离散化 40560K 3985MS 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>
#define SIZE 500005
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1

using namespace std;

typedef __int64 Int;
int N,idx;
Int sum[SIZE<<2];
int a[SIZE];
int temp[SIZE];
set <int> st;
set <int>::iterator it;

void update(int l,int r,int rt,int v)
{
sum[rt] ++;
if(l == r)
return;
int mid = (l + r) >> 1;
if(v <= mid)update(ls,v);
else update(rs,v);
}

Int query(int l,int r,int rt,int L,int R)
{
if(L <= l && r <= R)
return sum[rt];
int mid = (l + r) >> 1;
Int ret = 0;
if(L <= mid)ret += query(ls,L,R);
if(R > mid)ret += query(rs,L,R);
return ret;
}

int binarySearch(int tar)
{
int low = 1, high = idx;
while(low <= high)
{
int mid = (low + high) >> 1;
if(temp[mid] > tar)
high = mid - 1;
else if(temp[mid] < tar)
low = mid + 1;
else
return mid;
}
return -1;
}

int main()
{
while(~scanf("%d",&N) && N)
{
idx = 0;
st.clear();
for(int i=1; i<=N; i++)
{
scanf("%d",&a[i]);
a[i] ++;
st.insert(a[i]);
}
for(it = st.begin(); it != st.end(); it ++)
temp[++idx] = (*it);
sort(temp+1,temp+1+idx);
memset(sum,0,sizeof(sum));
int L;
Int ans = 0;
for(int i=1; i<=N; i++)
{
L = binarySearch(a[i]);
ans += query(1,idx,1,L,idx);
update(1,idx,1,L);
}
printf("%I64d\n",ans);
}
return 0;
}




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