Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一维DP 水过

Posted by 3017218214 at 2020-08-29 16:57:08 on Problem 2385
/**
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator