nlogn求逆序数 POJ 2299解题报告
2011-03-29 11:45
302 查看
前几天自己想出了利用归并排序求逆序数的方法,找了一个求逆序数的题2299 交了300++MS水过...
Ultra-QuickSort
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
Sample Output
Ultra-QuickSort
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 19686 | Accepted: 6959 |
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<iostream> using namespace std; __int64 sum=0; int *guibing(int *data,int n) { int i,j,k,L,R,s; int *now; for(i=2;i<n*2;i=i<<1) { now=new int ; for(j=0;j<=n/i;j++) { k=L=i*j; R=L+i/2; s=0; while(L<n&&R<n&&L<i*j+i/2&&R<i*(j+1)) { if(data[L]<=data[R]) { now[k++]=data[L++]; sum+=s; } else { now[k++]=data[R++]; s++; } } while(L<n&&L<i*j+i/2) { now[k++]=data[L++]; sum+=s; } while(R<n&&R<(j+1)*i) now[k++]=data[R++]; } delete data; data=now; } return data; } int main() { int n,i; int *data; while(cin>>n&&n) { sum=0; data=new int ; for(i=0;i<n;i++) scanf("%d",data+i); data=guibing(data,n); delete data; printf("%I64d/n",sum); } }
相关文章推荐
- poj2299解题报告(归并排序求逆序数)
- 树状数组 离散化 求逆序数POJ 2299Ultra-QuickSort解题报告
- poj解题报告——2299
- 解题报告——POJ 2299
- poj 2299 归并排序求逆序对 解题报告
- POJ - 2299 Ultra-QuickSort解题报告
- POJ 2299 解题报告
- 【原】 POJ 2299 Ultra-QuickSort 逆序数 解题报告
- 【解题报告】poj1042 Gone Fishing
- POJ 1163 解题报告
- 【解题报告】POJ-1467 Symbolic Derivation
- POJ 2411.Mondriaan's Dream 解题报告
- poj 2299 离散化+树状数组求逆序数
- poj 2299 Ultra-QuickSort(线段树,离散化求逆序数)
- poj 2376解题报告(详细) 带几组测试数据
- POJ 3239解题报告
- poj解题报告——3087
- POJ 2243解题报告
- POJ1190 生日蛋糕 ACM解题报告(DFS回溯+剪枝)
- acm-poj1051解题报告