Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
so easy太简单了吧! #include<stdlib.h> #include<stdio.h> #include<cstring> #include<algorithm> using std::sort; #define MAXN 500005 int n; struct p { int pos,val; }a[MAXN]; int b[MAXN]; int c[MAXN]; int lowbit(int x) { return x&(-x); } int sum(int i) { int ans=0; while(i>0){ ans+=c[i]; i-=lowbit(i); } return ans; } void add(int i,int w) { while(i<=n){ c[i]+=w; i+=lowbit(i); } } bool cmp(p a1,p a2) { return a1.val>a2.val; } int main() { int i; while(scanf("%d",&n)!=EOF&&n){ for(i=1;i<=n;i++){ scanf("%d",&a[i].val); a[i].pos=i; } sort(a+1,a+1+n,cmp); for(i=1;i<=n;i++) b[a[i].pos]=i; memset(c,0,sizeof(c)); long long res=0; for(i=1;i<=n;i++){ res+=sum(b[i]-1); add(b[i],1); } printf("%lld\n",res); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator