//source:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10, M = 26;
int a[N], cnt[M];
queue<int> pos[M];
struct BinaryIndexedTree {
int bit[N];
void up(int x, int val) {
while (x) {
bit[x] += val;
x -= x & (-x);
}
}
int get(int x) {
int ret = 0;
while (x < N) {
ret += bit[x];
x += x & (-x);
}
return ret;
}
} bit;
signed main() {
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL_MACHINE
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
#endif
int n; cin >> n;
string s; cin >> s;
for (int i = 0; i < 2 * n; ++i) ++cnt[a[i] = s[i] - 'a'];
for (int i = 0; i < M; ++i) cnt[i] /= 2;
vector<int> b;
for (int i = 0; i < 2 * n; ++i) if (cnt[a[i]]) {
b.push_back(a[i]);
--cnt[a[i]];
}
for (int i = 0; i < n; ++i) b.push_back(b[i]);
for (int i = 0; i < 2 * n; ++i) pos[b[i]].push(i + 1);
ll res = 0;
for (int i = 0; i < 2 * n; ++i) {
int x = pos[a[i]].front(); pos[a[i]].pop();
res += bit.get(x); bit.up(x, 1);
}
cout << res << "\n";
}
// ඞඞඞඞඞ you sus
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
1448 KB |
Output is correct |
3 |
Correct |
7 ms |
2520 KB |
Output is correct |
4 |
Correct |
9 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
1448 KB |
Output is correct |
3 |
Correct |
7 ms |
2520 KB |
Output is correct |
4 |
Correct |
9 ms |
3712 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
1448 KB |
Output is correct |
3 |
Correct |
7 ms |
2520 KB |
Output is correct |
4 |
Correct |
9 ms |
3712 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
11 ms |
3628 KB |
Output is correct |
7 |
Correct |
11 ms |
3668 KB |
Output is correct |
8 |
Correct |
11 ms |
3672 KB |
Output is correct |
9 |
Correct |
11 ms |
3576 KB |
Output is correct |
10 |
Correct |
10 ms |
3540 KB |
Output is correct |
11 |
Correct |
11 ms |
3560 KB |
Output is correct |
12 |
Correct |
10 ms |
3516 KB |
Output is correct |
13 |
Correct |
11 ms |
3444 KB |
Output is correct |
14 |
Correct |
12 ms |
3412 KB |
Output is correct |
15 |
Correct |
12 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
284 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
1448 KB |
Output is correct |
3 |
Correct |
7 ms |
2520 KB |
Output is correct |
4 |
Correct |
9 ms |
3712 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
11 ms |
3628 KB |
Output is correct |
11 |
Correct |
11 ms |
3668 KB |
Output is correct |
12 |
Correct |
11 ms |
3672 KB |
Output is correct |
13 |
Correct |
11 ms |
3576 KB |
Output is correct |
14 |
Correct |
10 ms |
3540 KB |
Output is correct |
15 |
Correct |
11 ms |
3560 KB |
Output is correct |
16 |
Correct |
10 ms |
3516 KB |
Output is correct |
17 |
Correct |
11 ms |
3444 KB |
Output is correct |
18 |
Correct |
12 ms |
3412 KB |
Output is correct |
19 |
Correct |
12 ms |
3412 KB |
Output is correct |
20 |
Correct |
1 ms |
284 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
11 ms |
3692 KB |
Output is correct |
33 |
Correct |
13 ms |
3668 KB |
Output is correct |
34 |
Correct |
11 ms |
3612 KB |
Output is correct |
35 |
Correct |
11 ms |
3540 KB |
Output is correct |
36 |
Correct |
11 ms |
3584 KB |
Output is correct |
37 |
Correct |
12 ms |
3500 KB |
Output is correct |
38 |
Correct |
10 ms |
3540 KB |
Output is correct |
39 |
Correct |
11 ms |
3412 KB |
Output is correct |
40 |
Correct |
11 ms |
3440 KB |
Output is correct |
41 |
Correct |
10 ms |
3364 KB |
Output is correct |