ABC369-D Bonus EXP

爆搜代码如下

void dfs(int pos,int st,ll exp)
{
    if(pos==n+1)
    {
        ans=max(ans,exp);
        return ;
    }
    if(st%2==0)
    {
        dfs(pos+1,st+1,exp+2*a[pos]);
    }
    else
    {
        dfs(pos+1,st+1,exp+a[pos]);
    }
    dfs(pos+1,st,exp);
}

 明显可以压成dp,st直接用1与0平替即可

dp[ i ][ j ]指的是到第i个怪物时已经击败 j 个击败后获得的最大exp,其中j被替换成0(偶)1(奇)

每到一个怪物,就比较奇偶各两个情况的最大值:

如果这次攻击是第偶数次:若选择放走,则为dp[ i-1 ][ j ]; 击败掉则为dp[ i-1 ][ (1-j)%2 ] + 2*(a[ i ]);

如果这次攻击是第奇数次:若选择放走,则为dp[ i-1 ][ j ]; 击败掉则为dp[ i-1 ][ (1-j)%2 ] + a[ i ]; 

/*I lv ya*/
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N = 2e5+5;
ll n,a[N];
ll s[N];
ll dp[N][2];
void solve()
{
    memset(dp,0,sizeof(dp));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    dp[0][1]=-1e9-5;
    for(int i=1;i<=n;i++)
    {
        dp[i][0]=max(dp[i-1][0],dp[i-1][1]+2*a[i]);
        dp[i][1]=max(dp[i-1][1],dp[i-1][0]+a[i]);
    }
    cout<<max(dp[n][0],dp[n][1])<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    int _;
    _=1;
    while(_--)
    {
        solve();
    }
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值