跳到正文

题解

题解:P16210 [ECUSTPC 2025] 时间谜题

Velleity约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年6月21日
更新
2026年6月21日
篇幅
约 1 分钟
题目
P16210

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<int> pre1(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        int x;
        cin >> x;
        pre1[i] = pre1[i - 1] + (x == 1);
    }
    vector<int> dp(n + 1, n + 1);
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < i; ++j) {
            int cnt1 = pre1[i] - pre1[j];
            int len = i - j;
            int cnt2 = len - cnt1;
            if (cnt1 == 0 || cnt2 == 0 || abs(cnt1 - cnt2) <= m) {
                dp[i] = min(dp[i], dp[j] + 1);
            }
        }
    }
    cout << dp[n] << '\n';
    return 0;
}
原文