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 |
一维DP 水过/** dp[i][j]:第i次站移动j次下最多能吃多少苹果 取j关于w,因为可以通过首位和w计算出当前位置 j % 2 + 1 dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]) + now_get 移或者不移 优化为1维 **/ #include<cstdio> #include<string.h> using namespace std; int dp[35],T,W,apple[1005]; int max(int x,int y){ return x > y ? x : y; } int main(){ while(~scanf("%d%d",&T,&W)){ memset(dp,0,sizeof(dp)); for(int i = 1;i <= T;++i){ scanf("%d",&apple[i]); } for(int i = 1;i <= T;++i){ dp[0] += apple[i] % 2; //dp[0]实时更新 for(int j = 1;j <= W;++j){ int now = (j % 2) + 1; dp[j] = max(dp[j],dp[j-1]) + (apple[i] == now ? 1 : 0); } } printf("%d\n",dp[W]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator