标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18543 Accepted Submission(s): 11246
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN=1e6+100,INF=1e9+100; int a[MAXN],c[MAXN],ans[MAXN]; int lowbit(int x) { return x&(-x); } void add(int i,int val) { for(i; i<=MAXN; i+=lowbit(i)) c[i]+=val; } int sum(int i) { int s=0; for(i; i>0; i-=lowbit(i)) s+=c[i]; return s; } int main() { int i,j,t,n; while(scanf("%d",&n)!=EOF) { for(i=n; i>=1; i--) scanf("%d",&a[i]); memset(c,0,sizeof(c)); memset(ans,0,sizeof(ans)); int cou=0; for(i=1; i<=n; i++) { ans[i]=sum(a[i]+1); cou+=ans[i]; add(a[i]+1,1); } int Min=cou; for(i=n; i>1; i--) { for(j=1; j<=n; j++) { if(j==i) continue; if(a[i]<a[j]) { cou++; ans[j]++; } } cou-=ans[i]; ans[i]=0; if(cou<Min) Min=cou; } cout<<Min<<endl; } return 0; }
HDU 1394Minimum Inversion Number 数状数组 逆序对数量和
标签:
原文地址:http://www.cnblogs.com/GeekZRF/p/5932079.html