QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#526473 | #7876. Cyclic Substrings | rxzfn639 | RE | 0ms | 0kb | C++23 | 1.2kb | 2024-08-21 16:38:18 | 2024-08-21 16:38:18 |
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 P = 998244353;
const int inf = 1e9;
void solve() {
string s;
int n, m, k;
cin >> n >> m >> k >> s;
n++;
s = "00" + s;
vector<int> sum(n + 1);
for (int i = 2; i <= n; i++) sum[i] = sum[i - 1] + (s[i] == '1');
auto check = [&](int x) -> bool {
vector dp(n + 1, vector<int>(k + 1, inf));
dp[0][0] = 0;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < k; j++) {
dp[i][j] = min(dp[i - 1][j], dp[i][j]);
if (i + x - 1 <= n && s[i - 1] == '0')
dp[i + x - 1][j + 1] = min(dp[i + x - 1][j + 1], dp[i - 2][j] + (x - sum[i + x - 1] + sum[i - 1]));
}
dp[i][k] = min(dp[i - 1][k], dp[i][k]);
}
return dp[n][k] <= m;
};
int l = 1, r = n;
while (l < r) {
int mid = (l + r + 1) / 2;
if (check(mid)) l = mid;
else r = mid - 1;
}
if (check(l)) cout << l << '\n';
else cout << -1 << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
详细
Test #1:
score: 0
Runtime Error
input:
5 01010