이 제출은 이전 버전의 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 < 2 * n; ++i) {
(s[i] == 'W' ? W : B).pb(i);
}
vector <vector <int>> w(n, vector <int> (n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int a = W[i], b = B[j];
if (a > b) {
swap(a, b);
}
int cnt0 = 0, cnt1 = 0;
for (int ii = a + 1; ii < b; ++ii) {
(s[ii] == 'W' ? cnt0 : cnt1)++;
}
w[i][j] = min(cnt0, n - 1 - cnt1) + min(cnt1, n - 1 - cnt0);
}
}
vector <int> dp(1 << n, 0);
for (int s = 0; s < (1 << n); ++s) {
int num = __builtin_popcount(s);
for (int i = 0; i < n; ++i) if (~s >> i & 1) {
dp[s | (1 << i)] = max(dp[s | (1 << i)], dp[s] + w[i][num]);
}
}
cout << dp.back() / 2 << '\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... |