이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct BIT {
int n;
vector<int> d;
void init(int _n) {
n = _n;
d.assign(n + 1, 0);
}
void update(int x, int val) {
for (; x <= n; x += x & -x) {
d[x] += val;
}
}
int get(int x) {
int res = 0;
for (;x > 0; x -= x & -x) {
res += d[x];
}
return res;
}
} bit;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
string s;
cin >> n >> s;
vector<int> pos[26], newPos[26];
for (int i = 0; i < 2 * n; ++i) {
pos[s[i] - 'a'].push_back(i + 1);
}
vector<array<int, 2>> firstHalf;
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < (int) pos[i].size() / 2; ++j) {
firstHalf.push_back({pos[i][j], i});
}
}
sort(firstHalf.begin(), firstHalf.end());
for (int i = 0; i < 26; ++i) {
reverse(pos[i].begin(), pos[i].end());
}
bit.init(2 * n);
long long ans = 0;
for (int t = 0; t < 2; ++t) {
for (auto x : firstHalf) {
int p = pos[x[1]].back();
//cerr << p << endl;
pos[x[1]].pop_back();
ans += bit.get(2 * n + 1 - p);
bit.update(2 * n + 1 - p, 1);
}
}
cout << ans << '\n';
return 0;
}
# | 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... |