F - Purple Rain (最大子段和dp)2022暑假做题

Purple Rain Purple rain falls in the magic kingdom of Linearland which is a straight, thin peninsula. On close observation however, Professor Nelson Rogers finds that the purple rain is actually a mix of red and blue raindrops. In his zeal, he records the location and color of the raindrops in different locations along the peninsula. Looking at the data, Professor Rogers wants to know which part of Linearland had the “least” purple rain. After some thought, he decides to model this problem as follows. Divide the peninsula into n sections and number them west to east from 1 to n. Then, describe the raindrops as a sequence of R and B, depending on whether the rainfall in each section is primarily red or blue. Finally, find a subsequence of contiguous sections where the difference between the number of R and the number of B is maximized. Input The input consists of a single line containing a string of n characters (1 ≤ n ≤ 105 ), describing the color of the raindrops in sections 1 to n. It is guaranteed that the string consists of uppercase ASCII letters ‘R’ and ‘B’ only. Output Print, on a single line, two space-separated integers that describe the starting and ending positions of the part of Linearland that had the least purple rain. These two numbers should describe an inclusive range; both numbers you print describe sections included in the range. If there are multiple possible answers, print the one that has the westernmost starting section. If there are multiple answers with the same westernmost starting section, print the one with the westernmost ending section. 2017 Pacific Northwest Region Programming Contest 15 Sample Input and Output

BBRRBRRBRB 3 7

BBRBBRRB 1 5

思路:可以分两种情况进行分别讨论更新左右端点位置

R多于B的情况和B多于R的情况。前者将R出现时记为1,B出现时记为-1

后者反之即可。

#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 1e5 + 5;
const double inf = 0x3f3f3f3f;
int n;
int r[N], b[N];
char str[N];
int minx = -1;
int ll, rr;
void dp(int ma[]) {
    int l = 1, r = 1, res = 0;
    for (int i = 1; i <= n; i++) {
        res += ma[i];
        if (res < 0) {
            res = 0;
            l = i + 1;
        }
        if (res > minx) {
            minx = res;
            r = i;
            rr = r;
            ll = l;
        } else if (res == minx) {
            if (l < ll) {
                ll = l;
                rr = i;
            } else if (ll == l && i < rr) {
                ll = l, rr = i;
            }
        }
    }
    return;
}
int main() {
    scanf("%s", str);
    n = strlen(str);
    for (int i = 1; i <= n; i++) {
        if (str[i - 1] == 'B') {
            b[i] = 1;
            r[i] = -1;
        } else if (str[i - 1] == 'R') {
            r[i] = 1;
            b[i] = -1;
        }
    }
    dp(r);
    dp(b);
    printf("%d %d\n", ll, rr);
    return 0;
}

其中核心就是扫的时候计数变成负的了就表示左顶点需要移动了(重新记录区间)

注意相等时判断是否更新端点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Monster_six

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

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

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

打赏作者

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

抵扣说明:

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

余额充值