ABC 261.D - Flipping and Bonus ( DP )

问题陈述
抛硬币N次,有一个计数器,最初显示为0。

根据第i次抛硬币的结果,会发生以下情况:

如果它是正面的:将计数器的值增加1并得到Xi;
如果它是反面的:将计数器的值设为0(不加Xi)。
此外,有M种连胜奖金。

求能收到的最大金额。

Sample Input 1 Copy

6 3
2 7 1 8 2 8
2 10
3 1
5 5

Sample Output 1 Copy

48

If he gets head, head, tail, head, head, head, in this order, the following amounts of money are awarded.(头头尾头头头)

  • In the 1-st coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 2 yen.
  • In the 2-nd coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 7 yen. Additionally, get 10 yen as a streak bonus.
  • In the 3-rd coin toss, the coin tails. Change the counter's value from 2 to 0.
  • In the 4-th coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 8 yen.
  • In the 5-th coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 2 yen. Additionally, get 10 yen as a streak bonus.
  • In the 6-th coin toss, the coin heads. Change the counter's value from 22 to 3 and receive 8 yen. Additionally, get 1 yen as a streak bonus.

max操作:2+(7+10)+0+8+(2+10)+(8+1)=48

注意二维数组长度!

//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<deque>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=200200;
const int M=5002;
const int mod=998244353;
LL a[N],f[M][M];
LL mp[N];
int b[N];
bool vis[N];
int dx[]={-1,0,0,1},dy[]={0,1,-1,0};
string s[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=m;i++)
        {
            LL x,y;
            cin>>x>>y;
            mp[x]=y;
        }
        //f[i][j]共抛出i次且计数器为j
        for(int i=1;i<=n;i++)//共抛了i次硬币
        {
            for(int j=1;j<=i;j++)//计数器为j
            {
                //让当前所抛出的硬币通通体验一下处于什么位置时候的值
                f[i][j]=f[i-1][j-1]+a[i]+mp[j];
            }
            f[i][0]=0;
            for(int k=1;k<i;k++)
            {
                //记录上一行操作下能够得到的最大的值
                //放在第0个的位置是为了满足上边当计数器为1的时候
                f[i][0]=max(f[i][0],f[i-1][k]);
            }
        }
        /*for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                cout<<f[i][j]<<" ";
            }
            cout<<endl;
        }*/
        LL maxn=0;
        for(int i=1;i<=n;i++)
            maxn=max(maxn,f[n][i]);
        cout<<maxn<<endl;
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vijurria

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值