日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)

發(fā)布時(shí)間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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

題目大意:

大概題意就是給你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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。