Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=1394

View Code
 1 //1394
2 #include <stdio.h>
3 const int N=5010;
4 int st[4*N],a[N];
5 void newup(int rt)
6 {
7 st[rt]=st[rt*2]+st[rt*2+1];
8 }
9 void build(int l,int r,int rt)
10 {
11 if (l==r)
12 {
13 st[rt]=0;
14 return;
15 }
16 int m=(l+r)/2;
17 build(l,m,rt*2);
18 build(m+1,r,rt*2+1);
19 newup(rt);
20 }
21 void update(int p,int l,int r,int rt)
22 {
23 if (l==r)
24 {
25 st[rt]++;
26 return;
27 }
28 int m=(l+r)/2;
29 if (p<=m) update(p,l,m,rt*2);
30 else update(p,m+1,r,rt*2+1);
31 newup(rt);
32 }
33 int query(int a,int b,int l,int r,int rt)
34 {
35 if (a<=l && r<=b) return st[rt];
36 int m=(l+r)/2,s=0;
37 if (a<=m) s+=query(a,b,l,m,rt*2);
38 if (b>m) s+=query(a,b,m+1,r,rt*2+1);
39 return s;
40 }
41 int main()
42 {
43 int n,i;
44 while (~scanf("%d",&n))
45 {
46 build(0,n-1,1);
47 int s=0;
48 for (i=1;i<=n;i++)
49 {
50 scanf("%d",&a[i]);
51 s+=query(a[i],n-1,0,n-1,1);
52 update(a[i],0,n-1,1);
53 }
54 int min=s;
55 for (i=1;i<=n;i++)
56 {
57 s+=n-a[i]-a[i]-1;
58 if (s<min) min=s;
59 }
60 printf("%d\n",min);
61 }
62 return 0;
63 }

 

posted on 2012-02-16 15:04  Qiuqiqiu  阅读(123)  评论(0编辑  收藏  举报