标签:des style blog color io os for sp 数据
1
2
3
4
|
3
3
1
2
|
1
|
2
|
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 1000010 int a[maxn]; long ans; int tmp[maxn]; void merge(int s1,int e1,int s2,int e2) { int i=s1,j=s2,k=0; while(i<=e1 && j<=e2) { if(a[i]<=a[j]) tmp[k++]=a[i++]; else { tmp[k++]=a[j++]; ans+=e1-i+1; } } while(i<=e1) tmp[k++]=a[i++]; while(j<=e2) tmp[k++]=a[j++]; //复制表中剩下部分 for(i=s1;i<=e2;i++) //复制回去 { a[i]=tmp[i-s1]; } } void merge_sort(int low,int high) { if(low<high) { int mid=(low+high)/2; merge_sort(low,mid); //后半部分 merge_sort(mid+1,high); //前半部分 merge(low,mid,mid+1,high); //各并表 } } int main() { int n,i; ans=0; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } merge_sort(0,n-1); printf("%ld\n",ans); return 0; }
逆序数 [SWUST OJ 443] Handsome Swap
标签:des style blog color io os for sp 数据
原文地址:http://www.cnblogs.com/hate13/p/4044771.html