G - A Question of Ingestion Gym - 101673G

  记忆化搜索

#include<bits/stdc++.h>
using namespace std;
#define maxn
int dp[105][30][5],a[105],n,m;
vector<int>v;
int dfs(int now,int sta,int rest)
{
    if(rest>=2) //休息2次及以上产生的效果是一样的
        rest=2,sta=0;
    if(dp[now][sta][rest]) return dp[now][sta][rest];
    int tmp=0;
    if(now>n) return dp[now][sta][rest]=tmp;
    if(sta==0)
        tmp=dfs(now+1,sta,rest+1);
    else
        tmp=dfs(now+1,sta-1,rest+1);
    if(v[sta]!=0)// 防止出现调用v[sta]越界,顺便剪枝了
        tmp=max(tmp,dfs(now+1,sta+1,0)+min(v[sta],a[now]));
    return dp[now][sta][rest]=tmp;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int tmp=m;
    v.clear();
    v.push_back(tmp);
    while(tmp)
    {
        tmp=tmp*2/3;
        v.push_back(tmp);
    }
    printf("%d\n",dfs(1,0,0));
    return 0;
}

 

posted @ 2018-11-22 23:31  eason99  阅读(53)  评论(0编辑  收藏  举报