SCOI2014 bzoj3594 Fang Bobo corn fields (two-dimensional array of tree + dp)

3594: [Scoi2014] Fang Bobo cornfield

Time Limit: 60 Sec  Memory Limit: 128 MB
Submit: 1971  Solved: 961
[Submit][Status][Discuss]

Description

Fang Bobo in his farm while walking, he suddenly found a row of corn fields is not very beautiful.
This row of a total of N lines of corn, their height varies.
Fang Bobo does not think monotonically decreasing sequence of beautiful, so he decided to put some of the corn overstating, then destroy the beauty of fall corn removal, making the rest of the height of the corn does not constitute a monotonic decline sequence.
Fang Bobo can select a range, the range of overstating all this corn 1 unit height, he can be up to K times such an operation. Pulling the maize corn can choose a set of unplugged.
Q. How much energy left most strains of corn, to form a row of beautiful corn.

Input


Line 1 comprises two integers n, K, which represent the number of rows of corn and up to how many operations can be performed.
Line 2 contains the integers n, denotes the i-th row of corn, left to right, the height of the i-ai maize strains.

Output


1 outputs an integer number of the remaining most corn.

Sample Input

3 1
2 1 3

Sample Output

3

HINT

1 < N < 10000,1 < K ≤ 500,1 ≤ ai ≤5000、


 

First, let's simplify the meaning of problems, that is, after performing k times overstating operation, seeking the longest monotone does not drop sequence.

First we prove a conclusion: the optimal decision-making overstating operation is certainly overstating the number of overstating the number of times greater than or equal behind the front, and overstating the number does not decrease monotonically.

With this nature, we know overstating operating every time he carried out the operation must be carried out for a suffix.

So you can come up with a complexity is $ O (n ^ 2k ^ 2) $ violent, that is similar to the $ (n ^ 2) $ solving process LIS O just one more restrictions

Provided $ F [i] [j] to $ is the i-th, j for most operations does not decrease on this sequence length

$f[i][j]=\max{f[k][l]+1},(a[i]+j>=a[k]+l,i>k,j>l)$

Since this problem of data than the strong, so on top of this algorithm can get 0 points, good results do not ask me how I know

T will obviously be to fly

Now we consider optimization, we can see that this restriction is a conditional branch two partial order, so two-dimensional array of tree maintenance intervals to a maximum

Points to note: a two-dimensional array of tree height after overstating a deposit, a deposit overstating the number of times, do not pay attention to save enough standard, otherwise it will MLE.

      In order to avoid the tree array subscript is 0, 1 are added to higher and higher frequency.

      Note that the definition of state array which determines that you are in the f array each update update ans, or updated in the last time.

      The second-dimensional reverse enumeration (do not know why qwq, big brother would have pointed out).

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<vector>
 7 #define lowbit(x) x&(-x)
 8 const int N=2e4+10;
 9 using namespace std;
10 int c[15005][10505];int n,k,Max=0;
11 int f[N][505],a[N];
12 void add(int x,int y,int add){//i,j
13     for(int i=x;i<=k+1;i+=lowbit(i)) for(int j=y;j<=Max+k;j+=lowbit(j)) c[i][j]=max(add,c[i][j]);//buhui
14 }
15 int query(int x,int y){
16     int res=0;
17     for(int i=x;i;i-=lowbit(i)) for(int j=y;j;j-=lowbit(j)) res=max(res,c[i][j]);
18     return res;
19 }
20 main(){
21     scanf("%d%d",&n,&k);
22     for(int i=1;i<=n;i++) scanf("%d",&a[i]),Max=max(a[i],Max);
23     int ans=0;
24     //for(int i=1;i<=k;i++) {f[i][0]=1;add(1,a[i],1);}//buzhidao
25     for(int i=1;i<=n;i++){
26         for(int j=k;j>=0;j--){//buzhidao
27             //mingbai
28             f[i][j]=query(j+1,a[i]+j)+1;
29             ans=max(f[i][j],ans);
30             add(j+1,a[i]+j,f[i][j]);//mingbai
31         }
32     }
33     /*int ans=-1;//mingbai
34     for(int i=0;i<=k;i++) ans=max(ans,f[n][i]);*/
35     printf("%d",ans);
36 }
View Code

 

Guess you like

Origin www.cnblogs.com/leom10/p/11281714.html