#include<iostream> #include<cstdio> #include<cstring> using namespace std; int a[1001],t[10001],ans; void Merge(int l,int m,int r) { int i=l,j=m+1; int pos=l; while(i<=m && j<=r) { if(a[i] > a[j]) { t[pos++]=a[j++]; ans+=m-i+1; } else t[pos++]=a[i++]; } while(i<=m) t[pos++]=a[i++]; while(j<=r) t[pos++]=a[j++]; for(int z=l;z<=r;z++) a[z]=t[z]; } void merge_sort(int l,int r) { if(l<r) { int m=(l+r)/2; merge_sort(l,m); merge_sort(m+1,r); Merge(l,m,r); } } int main() { int t; cin>>t; for(int Case=1;Case<=t;Case++) { int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; ans=0; merge_sort(1,n); printf("Scenario #%d: ",Case); cout<<ans<<endl<<endl; //for(int i=1;i<=n;i++) cout<<a[i]; // cout<<endl; } return 0; }
http://poj.org/problem?id=1804