POJ 2299 Ultra-QuickSort 题解
来源:互联网 发布:淘宝客服信息发布出去 编辑:程序博客网 时间:2024/06/07 07:14
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 53630 Accepted: 19693
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.
Ultra-QuickSort produces the output
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
59105431230
Sample Output
60
Source
Waterloo local 2005.02.05
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
——————————————————我是分割线————————————————————————
好题。归并排序求逆序对。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #include<cstdlib> 8 #include<iomanip> 9 #include<cassert>10 #include<climits>11 #define maxn 1000112 #define F(i,j,k) for(int i=j;i<=k;i++)13 #define FF(i,j,k) for(int i=j;i>=k;i--)14 #define inf 0x7fffffff15 #define NN 50000416 #define NL 100017 #define mem(a) memset(a, 0, sizeof(a))18 using namespace std;19 int N, A[500010], T[500010];20 __int64 ans;21 int read(){22 int x=0,f=1;char ch=getchar();23 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}24 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}25 return x*f;26 }27 __int64 res;28 int b[NN];29 void copy(int a[], int l, int r){30 int i;31 for (i = l; i <= r; i++){32 a[i] = b[i];33 }34 }35 void merge(int a[], int l, int mid, int r){36 int i = l;37 int j = mid + 1;38 int k = l;39 while(i <= mid && j <= r){40 if(a[i] < a[j]){41 b[k++] = a[i];42 i++;43 }else{44 b[k++] = a[j];45 j++;46 res += mid - i + 1;47 }48 }49 while(i <= mid){50 b[k++] = a[i];51 i++;52 }53 while(j <= r){54 b[k++] = a[j];55 j++;56 }57 }58 void mergeSort(int a[], int l, int r){59 if(l < r){60 int mid = (l + r) >> 1;61 mergeSort(a, l, mid);62 mergeSort(a, mid + 1, r);63 merge(a, l, mid, r);64 copy(a, l, r);65 }66 }67 int main() {68 int n, i;69 int f[NN];70 while(cin>>n&&n){71 if(n == 0) break;72 for (i = 1; i <= n; i++){73 cin>>f[i];74 }75 res = 0;76 mergeSort(f, 1, n);77 cout<<res<<endl;78 }79 return 0;80 }
0 0
- poj 2299 Ultra-QuickSort 题解
- POJ 2299 Ultra-QuickSort 题解
- POJ 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299Ultra-quicksort
- POJ-2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 - Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- 【汇总】2012-2016年东南大学物理系考研报录比
- TYVJ-P1864 守卫者的挑战 题解
- TYVJ P1933 绿豆蛙的归宿 题解(未完成)
- TYVJ 2002 扑克牌 题解
- POJ 1737 Connected Graph 题解(未完成)
- POJ 2299 Ultra-QuickSort 题解
- BZOJ 2179 FFT快速傅立叶 题解
- 随机规划自己的一天系列233
- poj 1995 Raising Modulo Numbers 题解
- 学习笔记: cdq分治
- leetcode 链表处理集锦
- 剑指offer(java版)
- 搜索下拉框
- poj 3258 River Hopscotch 题解