#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 25;
struct bit {
int tree[MAXN] = {};
void add (int x) {
if (x < 0 || x > MAXN) return;
for (; x < MAXN; x += x & (-x)) tree[x]++;
}
int get (int x) {
if (x < 0 || x > MAXN) return 0;
int sum = 0; for (; x; x -= x & (-x)) sum += tree[x];
return sum;
}
} cur;
int n;
string s;
vector <int> occ[26];
int pref[MAXN];
signed main () {
ios::sync_with_stdio(0);
cin.tie(0); memset(cur.tree, 0, sizeof(cur.tree));
cin >> n >> s;
s.insert(s.begin(), '0');
for (int i = 1; i <= 2 * n; i++) occ[s[i] - 'a'].push_back(i);
vector <pair <int, int>> ll2;
for (int i = 0; i < 26; i++) {
if (occ[i].empty()) continue;
for (int j = (int)occ[i].size() / 2; j < (int)occ[i].size(); j++) {
ll2.push_back({occ[i][j - (int)occ[i].size() / 2], occ[i][j]});
pref[occ[i][j]]++;
}
}
sort(ll2.begin(), ll2.end()); ll2.insert(ll2.begin(), {0, 0});
for (int i = 1; i <= 2 * n; i++) pref[i] += pref[i - 1];
int sum = 0; int sum2 = 0; for (int i = 1; i <= n; i++) {
sum += pref[ll2[i].first];
sum2 += cur.get(ll2[i].second);
cur.add(ll2[i].second);
}
cout << sum + n * (n - 1) / 2 - sum2 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
5272 KB |
Output is correct |
3 |
Correct |
6 ms |
6752 KB |
Output is correct |
4 |
Correct |
8 ms |
7432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
5272 KB |
Output is correct |
3 |
Correct |
6 ms |
6752 KB |
Output is correct |
4 |
Correct |
8 ms |
7432 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
5272 KB |
Output is correct |
3 |
Correct |
6 ms |
6752 KB |
Output is correct |
4 |
Correct |
8 ms |
7432 KB |
Output is correct |
5 |
Correct |
1 ms |
3416 KB |
Output is correct |
6 |
Correct |
11 ms |
7772 KB |
Output is correct |
7 |
Correct |
12 ms |
7772 KB |
Output is correct |
8 |
Correct |
12 ms |
7772 KB |
Output is correct |
9 |
Correct |
12 ms |
7772 KB |
Output is correct |
10 |
Correct |
11 ms |
7516 KB |
Output is correct |
11 |
Correct |
13 ms |
7904 KB |
Output is correct |
12 |
Correct |
11 ms |
7516 KB |
Output is correct |
13 |
Correct |
11 ms |
7652 KB |
Output is correct |
14 |
Correct |
11 ms |
7512 KB |
Output is correct |
15 |
Correct |
11 ms |
7768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3416 KB |
Output is correct |
3 |
Correct |
1 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
1 ms |
3416 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3416 KB |
Output is correct |
8 |
Correct |
1 ms |
3416 KB |
Output is correct |
9 |
Correct |
1 ms |
3420 KB |
Output is correct |
10 |
Correct |
1 ms |
3420 KB |
Output is correct |
11 |
Correct |
1 ms |
3420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
3 ms |
5272 KB |
Output is correct |
3 |
Correct |
6 ms |
6752 KB |
Output is correct |
4 |
Correct |
8 ms |
7432 KB |
Output is correct |
5 |
Correct |
1 ms |
3420 KB |
Output is correct |
6 |
Correct |
1 ms |
3420 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3420 KB |
Output is correct |
9 |
Correct |
1 ms |
3416 KB |
Output is correct |
10 |
Correct |
11 ms |
7772 KB |
Output is correct |
11 |
Correct |
12 ms |
7772 KB |
Output is correct |
12 |
Correct |
12 ms |
7772 KB |
Output is correct |
13 |
Correct |
12 ms |
7772 KB |
Output is correct |
14 |
Correct |
11 ms |
7516 KB |
Output is correct |
15 |
Correct |
13 ms |
7904 KB |
Output is correct |
16 |
Correct |
11 ms |
7516 KB |
Output is correct |
17 |
Correct |
11 ms |
7652 KB |
Output is correct |
18 |
Correct |
11 ms |
7512 KB |
Output is correct |
19 |
Correct |
11 ms |
7768 KB |
Output is correct |
20 |
Correct |
1 ms |
3160 KB |
Output is correct |
21 |
Correct |
1 ms |
3416 KB |
Output is correct |
22 |
Correct |
1 ms |
3420 KB |
Output is correct |
23 |
Correct |
1 ms |
3420 KB |
Output is correct |
24 |
Correct |
1 ms |
3416 KB |
Output is correct |
25 |
Correct |
1 ms |
3416 KB |
Output is correct |
26 |
Correct |
1 ms |
3416 KB |
Output is correct |
27 |
Correct |
1 ms |
3416 KB |
Output is correct |
28 |
Correct |
1 ms |
3420 KB |
Output is correct |
29 |
Correct |
1 ms |
3420 KB |
Output is correct |
30 |
Correct |
1 ms |
3420 KB |
Output is correct |
31 |
Correct |
1 ms |
3164 KB |
Output is correct |
32 |
Correct |
12 ms |
7700 KB |
Output is correct |
33 |
Correct |
12 ms |
7772 KB |
Output is correct |
34 |
Correct |
14 ms |
7768 KB |
Output is correct |
35 |
Correct |
11 ms |
7772 KB |
Output is correct |
36 |
Correct |
11 ms |
7516 KB |
Output is correct |
37 |
Correct |
12 ms |
7516 KB |
Output is correct |
38 |
Correct |
11 ms |
7712 KB |
Output is correct |
39 |
Correct |
11 ms |
7772 KB |
Output is correct |
40 |
Correct |
10 ms |
7516 KB |
Output is correct |
41 |
Correct |
11 ms |
7772 KB |
Output is correct |