其他分享
首页 > 其他分享> > BZOJ 5125 小Q的书架

BZOJ 5125 小Q的书架

作者:互联网

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int c[50005],tr,tl,f[30][50005],a[50005],s,n,k;
 7 void del(int x)
 8 {
 9     while (x<=n)
10     {
11         c[x]-=1;
12         x+=(x&(-x));
13     }
14 }
15 void add(int x)
16 {
17     while (x<=n)
18     {
19         c[x]+=1;
20         x+=(x&(-x));
21     }
22 }
23 int query(int x)
24 {
25     int sum=0;
26     while (x)
27     {
28         sum+=c[x];
29         x-=(x&(-x));
30     }
31     return sum;
32 }
33 int get(int l,int r)
34 {
35     while (tl<l) 
36     {
37         del(a[tl]);
38         s-=query(a[tl]);
39         tl++;
40     }
41     while (tl>l)
42     {
43         tl--;
44         s+=query(a[tl]);
45         add(a[tl]);
46     }
47     while (tr<r)
48     {
49         tr++;
50         s+=tr-tl-query(a[tr]);
51         add(a[tr]);
52     }
53     while (tr>r)
54     {    
55         del(a[tr]);
56         s-=tr-tl-query(a[tr]);
57         tr--;
58     } 
59     return s;
60 }
61 void solve(int x,int l,int r,int L,int R)
62 {int i,Mid;
63     if (l>r) return;
64     int mid=(l+r)/2;
65     Mid=mid;
66     for (i=L;i<=min(mid-1,R);i++)
67     {
68         int t=f[x-1][i]+get(i+1,mid);
69         if (f[x][mid]>=t) 
70         {
71             f[x][mid]=t;
72             Mid=i;
73         }
74     }
75     solve(x,l,mid-1,L,Mid);
76     solve(x,mid+1,r,Mid,R);
77 }
78 int main()
79 {int i,j;
80     scanf("%d%d",&n,&k);
81     for (i=1;i<=n;i++)
82     scanf("%d",&a[i]);
83     memset(f,127/2,sizeof(f));
84     tl=1;tr=0;
85     for (i=1;i<=n;i++)
86     {
87         f[1][i]=get(1,i);
88     } 
89     for (i=2;i<=k;i++)
90     {
91         solve(i,i,n,i-1,n);
92     }
93     printf("%d",f[k][n]);
94 }

 

标签:书架,int,tr,mid,Mid,tl,5125,include,BZOJ
来源: https://www.cnblogs.com/Y-E-T-I/p/12248723.html