Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

so easy

Posted by wujingyuf27 at 2011-09-17 21:15:17 on Problem 2299
太简单了吧!
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator