没有网址qwq
没有oj
翻树状数组看到求逆序对先复习一下归并求逆序对qwq
逆序对真是个神奇的东西啊QAQ
纯属随手一打随手一贴quq
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 int n, a[100010], b[100010], cnt = 0; 5 void merge_sort(int l, int r) { 6 if(r-l > 0) {//if 打成 while 7 int mid = (l+r)/2; // int mid = (l+r)>>1 写成 >>2 ???? 8 int i = l, p = l, q = mid+1; 9 merge_sort(l, mid); 10 merge_sort(mid+1, r); 11 while(p <= mid || q <= r) { 12 if(q > r || ((p<=mid) && a[p] <= a[q])) //p<=mid !!!!!别打成p<mid!!! 13 b[i++] = a[p++]; 14 else b[i++] = a[q++], cnt = cnt + (mid - p + 1); 15 //mid - 当前已经比较到哪个元素了 (p) 16 //别减成i 或者 l 17 } 18 for(int i = l; i <= r; i++) 19 a[i] = b[i]; //把这个赋值的循环放到if(r - l > 0) 外 20 } 21 } 22 int main() { 23 scanf("%d", &n); 24 for(int i = 1; i <= n; i++) { 25 scanf("%d", &a[i]); 26 } 27 merge_sort(1, n); 28 printf("qwq排序后: "); 29 for(int i = 1; i <= n; i++) 30 cout<<a[i]<<' '; 31 cout<<endl<<"quq逆序对数:"<<cnt<<endl; 32 return 0; 33 }
一个merge_sort 你能错出这么多花样??