Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴代码#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1000+5; int dp[maxn][30+5]; int s[maxn]; int ans=-1,cnt=1,n,m; int A,B,C; int main() { scanf("%d%d",&n,&m); for(int i=1,p,q;i<=n;i++){ scanf("%d",&p); if(i==1) {s[cnt++]=i;A=p-1;q=p;} else if(p!=q) {s[cnt++]=i;q=p;} } s[cnt]=n+1; memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(int i=1;i<=cnt;i++){ for(int j=0;j<=m&&j<=i;j++){ C=i%2?A:1-A; B=j%2; int tmp=j?(dp[i-1][j]==-1?dp[i-1][j-1]:max(dp[i-1][j],dp[i-1][j-1])):dp[i-1][j]; dp[i][j]=tmp+(B==C?s[i+1]-s[i]:0); } } for(int i=0;i<=m&&i<=cnt;i++) if(ans<dp[cnt][i]) ans=dp[cnt][i]; printf("%d\n",ans); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator