이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <array>
#define ll long long
using namespace std;
ll n, k, dp[5000][5000];
vector <ll> A, B, nxA, ps;
string S;
ll C(ll l, ll r) {
if (nxA[l] > A[r]) return 0;
ll nx = max(l, nxA[l]), cnt = r-nx+1, cnt2 = max(0LL, l-nxA[l]);
return ps[r]-(nx ? ps[nx-1] : 0)-cnt*B[l]-cnt*(cnt-1)/2-(r-l+1)*cnt2;
}
int main() {
cin >> n >> k >> S;
for (int i=0; i<2*n; ++i) {
if (S[i] == 'A') {
ps.push_back((ps.empty() ? 0 : ps.back()) + i);
A.push_back(i);
}
else {
B.push_back(i);
nxA.push_back(A.size());
}
}
for (int i=0; i<n; ++i) dp[i][0] = C(0, i);
for (int x=1; x<k; ++x) {
for (int i=0; i<n; ++i) {
dp[i][x] = 1e18;
ll optid = -1;
for (int j=0; j<i; ++j) {
if (dp[i][x] > dp[j][x-1] + C(j+1, i)) {
dp[i][x] = dp[j][x-1] + C(j+1, i);
optid = j;
}
}
}
}
/*for (int x=0; x<k; ++x) {
for (int i=0; i<n; ++i) {
cout << dp[i][x] << " ";
}
cout << endl;
}*/
cout << dp[n-1][k-1] << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
chorus.cpp: In function 'int main()':
chorus.cpp:36:10: warning: variable 'optid' set but not used [-Wunused-but-set-variable]
36 | ll optid = -1;
| ^~~~~
# | 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... |