이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'A') {
a.push_back(i);
} else {
b.push_back(i);
}
}
vector<int> ia(2 * n);
for (int i = 0; i < n; i++) {
ia[a[i]] = i + 1;
}
vector<int> pref(2 * n);
for (int i = 0; i < 2 * n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
}
pref[i] += (s[i] == 'A' ? 1 : 0);
}
vector<int> ib(2 * n);
for (int i = 0; i < n; i++) {
ib[b[i]] = pref[b[i]];
}
auto Solve = [&](int L, int R) {
int res = 0;
for (int i = L; i <= R; i++) {
res += max(0, ia[a[R]] - ib[b[i]]);
}
return res;
};
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
} else {
pref[i] = 0;
}
pref[i] += ib[b[i]];
}
auto GetSum = [&](int L, int R) {
return pref[R] - (L == 0 ? 0 : pref[L - 1]);
};
const int inf = (int) 1e9;
vector<int> dp(n + 1, inf);
dp[0] = 0;
for (int foo = 0; foo < k; foo++) {
vector<int> new_dp(n + 1, inf);
int ptr = -1;
for (int i = 0; i < n; i++) {
while (ptr + 1 <= i && ia[a[i]] >= ib[b[ptr + 1]]) {
ptr += 1;
}
for (int j = ptr + 1; j <= i; j++) {
new_dp[i + 1] = min(new_dp[i + 1], dp[j]);
}
for (int j = 0; j <= ptr; j++) {
new_dp[i + 1] = min(new_dp[i + 1], dp[j] + ia[a[i]] * (ptr - j + 1) - GetSum(j, ptr));
}
}
swap(dp, new_dp);
}
cout << dp[n] << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
chorus.cpp: In function 'int main()':
chorus.cpp:35:8: warning: variable 'Solve' set but not used [-Wunused-but-set-variable]
35 | auto Solve = [&](int L, int R) {
| ^~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |