[Bzoj3594] [SCOI2014] [Fang Bo's corn field] [dp + two-dimensional tree array], bzoj3594scoi2014
Description
Fang's uncle was walking along his farm, and he suddenly found a row of corn in the farm very bad.
There are N lines of corn in this row, and their height is uneven.
Fang believes that the monotonous sequence is very beautiful, so he decided to pull some corn first, and then remove the corn that damages the aesthetic, so that the height of the remaining corn constitutes a monotonous sequence.
Fang boo can select a range to increase all the corn in this range by 1 unit. He can perform this operation for a maximum of K times. You can select a collection of corn to unplug the corn.
Ask how many lines of corn can be left to form a row of beautiful corn.
Input
The row 1st contains two integers, n and K, indicating the number of corn in the row and the maximum number of operations that can be performed.
Row 2nd contains n integers. The I number indicates the height of the corn in the row from left to right.
Output
Output 1 integer, the maximum number of corn left.
Sample Input3 1
2 1 3
Sample Output3HINT
1 <N <random, 1 <K ≤ 5000, 1 ≤ ai ≤
Question: it can be found that this question is actually the maximum length of sub-sequence not to be dropped. The dp equation is similar to the common lis.
If f [I] [j] ends with I, the maximum number of subsequences that have been raised for j times is not decreased.
Then f [I] [j] = max (f [p] [q]) + 1 (p <I; q <= j; a [I] + j> = a [p] + q );
The violent transfer apparently times out. So we can optimize it with a two-dimensional tree array.
#include<iostream>#include<cstdio>using namespace std;int c[6000][600],n,k,maxx(-1),temp,a[10010],ans;inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int lowbit(int x){return x&(-x);}void updata(int x,int y,int v){ for (int i=x;i<=maxx;i+=lowbit(i)) for (int j=y;j<=k+1;j+=lowbit(j)) c[i][j]=max(c[i][j],v);}int query(int x,int y){ int ans(-1); for (int i=x;i;i-=lowbit(i)) for (int j=y;j;j-=lowbit(j)) ans=max(ans,c[i][j]); return ans;} int main(){ n=read();k=read(); for (int i=1;i<=n;i++) { a[i]=read(); maxx=max(a[i]+k,maxx); } for (int i=1;i<=n;i++) for (int j=k;j>=0;j--) { temp=query(a[i]+j,j+1)+1; updata(a[i]+j,j+1,temp); ans=max(temp,ans); } printf("%d\n",ans);}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.