이 제출은 이전 버전의 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() {
srand(time(NULL));
ios::sync_with_stdio(false), cin.tie(0);
double T = clock();
int n;
string s;
cin >> n >> s;
vector <pii> a;
for (int i = 0; i < n; ++i) {
if (s[i] == s[i + n]) {
a.emplace_back(i, s[i] == 'B' ? 1 : 0);
}
}
int m = a.size();
ll ans = 1ll << 60;
if (!m) {
cout << 1ll * n * (n - 1) / 2 << '\n';
return 0;
}
while ((clock() - T) / CLOCKS_PER_SEC <= 1.9) {
int rt = rand() % m;
vector <pii> stk;
ll cur = 0;
for (int i = 0; i < m; ++i) {
auto [x, id] = a[(i + rt) % m];
if (!stk.empty() && stk.back().second != id) {
int p = stk.back().first; stk.pop_back();
int len = abs(p - x);
cur += min(len, n - len);
} else {
stk.emplace_back(x, id);
}
}
ans = min(ans, cur);
}
cout << 1ll * n * (n - 1) / 2 - ans << '\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... |