【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)
題干:
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 0Sample Output
6 0題目大意:
大概題意就是給你n個(gè)數(shù),讓你求逆序?qū)Φ膫€(gè)數(shù)。
解題報(bào)告:
? ? 這題是求逆序?qū)Φ膫€(gè)數(shù),直接上權(quán)值樹(shù)狀數(shù)組,然后求個(gè)逆序數(shù)就可以了,這里提供兩種思路,一種是枚舉每一個(gè)數(shù)然后看截止當(dāng)前,他前面出現(xiàn)過(guò)多少比他大的(查后綴1的個(gè)數(shù))。另一種是看截止當(dāng)前,他后面有多少比他小的,也就是還有多少比他小的還沒(méi)出現(xiàn)(查前綴0的個(gè)數(shù))。
在此附上對(duì)應(yīng)兩種方法的AC代碼:
AC代碼1:
#include<bits/stdc++.h> #define ll long long using namespace std; const int MAX = 500005 ; int a[500005],b[MAX],c[500005]; int n; int lowbit(int x){return x&-x;} void update(int x,int val) {while(x<MAX) {c[x] += val;x+=lowbit(x);} } int query(int x) {int res = 0;while(x>0) {res += c[x];x-=lowbit(x);}return res; } int main() {while(~scanf("%d",&n)) {if(n==0) break;ll ans = 0;memset(c,0,sizeof c);for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i];sort(b+1,b+n+1);int top = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) {int pos = lower_bound(b+1,b+top+1,a[i]) - b;update(pos,1);ans += query(MAX-1) - query(pos);//ans += (pos-1)-query(pos-1); // printf("yy\n");}printf("%lld\n",ans);}return 0 ;}總結(jié):
? ?1.注意update和query中while都不能寫等號(hào)。查詢query的時(shí)候越界問(wèn)題,所以需要查詢MAX-1;
AC代碼2:
//沒(méi)寫n==0的break? #include<bits/stdc++.h> #define ll long long using namespace std; const int MAX = 500005 ; int a[500005],b[MAX],c[500005]; int n; int lowbit(int x){return x&-x;} void update(int x,int val) {while(x<=MAX) {c[x] += val;x+=lowbit(x);} } int query(int x) {int res = 0;while(x>0) {res += c[x];x-=lowbit(x);}return res; } int main() {while(~scanf("%d",&n)) {if(n==0) break;ll ans = 0;memset(c,0,sizeof c);for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i];sort(b+1,b+n+1);int top = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) {int pos = lower_bound(b+1,b+top+1,a[i]) - b;update(pos,1);ans += (pos-1)-query(pos-1);}printf("%lld\n",ans);}return 0 ;}?
總結(jié)
以上是生活随笔為你收集整理的【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何避免洗钱陷阱?生活中有哪些洗钱行为?
- 下一篇: 算法讲解 -- 二分图之 匈牙利算法