CF2025D Attribute Checks 题解
Super_Cube · · 题解
Solution
设
若
若
若
区间加用差分维护,
时间复杂度:
Code
#include<bits/stdc++.h>
int dp[5005];
int d[5005];
int n,m;
int main(){
scanf("%d%d",&n,&m);
memset(dp+1,192,m<<2);
for(int i=1,x,s=0;i<=n;++i){
scanf("%d",&x);
if(x>0){
if(x<=s)++d[x],--d[s+1];
}else if(x){
if(0<=s+x)++d[0],--d[s+x+1];
}else{
++s;dp[0]+=d[0];
for(int j=1;j<=s;++j)dp[j]+=(d[j]+=d[j-1]);
for(int j=s;j;--j)dp[j]=std::max(dp[j],dp[j-1]);
memset(d,0,s+1<<2);
}
}
dp[0]+=d[0];
for(int j=1;j<=m;++j)dp[j]+=(d[j]+=d[j-1]);
printf("%d",*std::max_element(dp,dp+m+1));
return 0;
}