#include <bits/stdc++.h>
using namespace std;
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
n = 2 * n;
vector<vector<int>> pos(26);
for (int i = 0; i < n; i++) {
pos[s[i] - 'a'].push_back(i);
}
vector<int> cnt(26);
for (int i = 0; i < 26; i++) {
cnt[i] = (int) pos[i].size() / 2;
}
string t = "";
for (int i = 0; i < n; i++) {
int c = s[i] - 'a';
if (cnt[c] > 0) {
t += s[i];
cnt[c] -= 1;
}
}
t = t + t;
vector<int> a;
vector<int> nxt(26);
for (int i = 0; i < n; i++) {
int c = t[i] - 'a';
a.push_back(pos[c][nxt[c]]);
nxt[c] += 1;
}
long long ans = 0;
fenwick<int> fenw(n);
for (int i = 0; i < n; i++) {
ans += i - fenw.get(a[i]);
fenw.modify(a[i], 1);
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
1424 KB |
Output is correct |
3 |
Correct |
7 ms |
2788 KB |
Output is correct |
4 |
Correct |
10 ms |
3692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
1424 KB |
Output is correct |
3 |
Correct |
7 ms |
2788 KB |
Output is correct |
4 |
Correct |
10 ms |
3692 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
1424 KB |
Output is correct |
3 |
Correct |
7 ms |
2788 KB |
Output is correct |
4 |
Correct |
10 ms |
3692 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
11 ms |
3364 KB |
Output is correct |
7 |
Correct |
11 ms |
3364 KB |
Output is correct |
8 |
Correct |
11 ms |
3404 KB |
Output is correct |
9 |
Correct |
12 ms |
3376 KB |
Output is correct |
10 |
Correct |
11 ms |
3348 KB |
Output is correct |
11 |
Correct |
12 ms |
3188 KB |
Output is correct |
12 |
Correct |
12 ms |
3220 KB |
Output is correct |
13 |
Correct |
10 ms |
3456 KB |
Output is correct |
14 |
Correct |
11 ms |
3368 KB |
Output is correct |
15 |
Correct |
10 ms |
3348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
1424 KB |
Output is correct |
3 |
Correct |
7 ms |
2788 KB |
Output is correct |
4 |
Correct |
10 ms |
3692 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
11 ms |
3364 KB |
Output is correct |
11 |
Correct |
11 ms |
3364 KB |
Output is correct |
12 |
Correct |
11 ms |
3404 KB |
Output is correct |
13 |
Correct |
12 ms |
3376 KB |
Output is correct |
14 |
Correct |
11 ms |
3348 KB |
Output is correct |
15 |
Correct |
12 ms |
3188 KB |
Output is correct |
16 |
Correct |
12 ms |
3220 KB |
Output is correct |
17 |
Correct |
10 ms |
3456 KB |
Output is correct |
18 |
Correct |
11 ms |
3368 KB |
Output is correct |
19 |
Correct |
10 ms |
3348 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
308 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
11 ms |
3512 KB |
Output is correct |
33 |
Correct |
11 ms |
3464 KB |
Output is correct |
34 |
Correct |
11 ms |
3520 KB |
Output is correct |
35 |
Correct |
10 ms |
3376 KB |
Output is correct |
36 |
Correct |
13 ms |
3420 KB |
Output is correct |
37 |
Correct |
10 ms |
3504 KB |
Output is correct |
38 |
Correct |
13 ms |
3340 KB |
Output is correct |
39 |
Correct |
10 ms |
3448 KB |
Output is correct |
40 |
Correct |
12 ms |
3340 KB |
Output is correct |
41 |
Correct |
13 ms |
3220 KB |
Output is correct |