# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1003943 | vjudge1 | Ekoeko (COCI21_ekoeko) | C++17 | 5 ms | 3264 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
const int MAXN = 1e5 + 10;
int bit[MAXN];
void add(int idx, int x, int n) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += x;
}
int pref(int idx) {
int sum = 0;
for (; idx >= 0; idx = (idx & (idx + 1)) - 1)
sum += bit[idx];
return sum;
}
int sum(int l, int r) { return pref(r) - pref(l-1); }
int main() {
int n;
cin >> n;
n *= 2;
string s;
cin >> s;
vector<vector<int>> cnt(26);
for (int i = 0; i < n; ++i)
cnt[s[i] - 'a'].push_back(i);
vector<int> perm(n);
int atual = 0; string final;
vector<int> cnt2(26);
for (int i = 0; i < n; ++i) {
int c = s[i] - 'a';
if (cnt2[c] < cnt[c].size()/2) {
++cnt2[c];
perm[i] = atual++;
final.push_back(s[i]);
}
}
for (char c: final)
perm[cnt[c - 'a'][cnt2[c - 'a']++]] = atual++;
lli ans = 0;
for (int i = 0; i < n; ++i) {
ans += sum(perm[i], n);
add(perm[i], 1, n);
}
cout << ans << "\n";
}
Compilation message (stderr)
# | 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... |