其他分享
首页 > 其他分享> > 牛客-牛客练习赛100B 小红的子序列

牛客-牛客练习赛100B 小红的子序列

作者:互联网

小红的子序列

今日无事,勾栏..

今天心血来潮打了一下牛客,发现了一道有趣的题

dp

大概就是把 颜色奇偶 分成两个状态:当前位置是否是奇数,当前颜色是否是红色,类似于状态压缩一样压一下,然后 dp 不断维护最大值就好

状态转移就是从将当前状态 取反 转移过来就好

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll dp[10];
ll num[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=0; i<n; i++) cin >> num[i];
    string s;
    cin >> s;
    for(int i=0; i<n; i++)
    {
        int x = 0;
        if(s[i] == 'R') x++;
        if(num[i] & 1) x += 2;
        // cout << x << " " << (x ^ 3) << endl;
        dp[x ^ 3] = max(dp[x ^ 3], dp[x] + num[i]);
    }
    cout << max({dp[0], dp[1], dp[2], dp[3]}) << endl;
    return 0;
}

标签:练习赛,int,ll,100B,cin,牛客,include,dp
来源: https://www.cnblogs.com/dgsvygd/p/16365053.html