이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n;
string s;
cin >> n >> s;
vector <int> W, B;
for (int i = 0; i < n; ++i) {
if (s[i] == s[i + n]) {
(s[i] == 'W' ? W : B).pb(i);
}
}
int m = W.size();
vector <vector <ll>> dp(m + 1, vector <ll> (m + 1, 1ll << 60));
for (int i = 0; i <= m; ++i) {
dp[i][i] = 0;
}
for (int len = 1; len <= m; ++len) {
for (int i = 0; i + len <= m; ++i) {
int j = i + len;
{
int cost = abs(W[len - 1] - B[i]);
dp[i][j] = min(dp[i][j], dp[i + 1][j] + min(cost, n - cost));
}
{
int cost = abs(W[len - 1] - B[j - 1]);
dp[i][j] = min(dp[i][j], dp[i][j - 1] + min(cost, n - cost));
}
}
}
cout << 1ll * n * (n - 1) / 2 - dp[0][m] << '\n';
}
# | 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... |