『MGOI』Simple Round I | B. 魔法照相馆 题解

xvl- / 2023-08-07 / 原文

题目传送门

一道模拟题。

并不复杂的模拟题,也不需要用到贪心。

我们可以创建一个数组来记录每个幕布是否被拉上,统计答案的时候,就看看这块幕布前面有多少个没拉上的,最后如果这块幕布拉上了,就重新放下来就行了。

#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
using namespace std;
ll n, ans;
string s;
bool vis[4]; // vis[i] = 1 表示第 i 个幕布当前是拉起来的
int main() {
    ios :: sync_with_stdio(0);
    cin >> n >> s;
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'W') {
            if (vis[3]) {
                ans++;
                vis[3] = 0;
            }
        }
        else if (s[i] == 'B') {
            if (!vis[3]) {
                ans++;
                vis[3] = 1;
            }
            if (vis[2]) {
                ans++;
                vis[2] = 0;
            }
        }
        else {
            if (!vis[3]) {
                ans++;
                vis[3] = 1;
            }
            if (!vis[2]) {
                ans++;
                vis[2] = 1;
            }
            if (vis[1]) {
                ans++;
                vis[1] = 0;
            }
        }
    }
    cout << ans;
    return 0;
}