#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 2e5 + 5;
int N;
string S;
int curCnt[26], totCnt[26], pos[26][MX];
vector<int> a;
struct fw {
int t[MX];
void upd(int x, int val) {
for(int i = x; i <= N; i += i & -i) t[i] += val;
}
int que(int x) {
int res = 0;
for(int i = x; i > 0; i -= i & -i) res += t[i];
return res;
}
int que(int l, int r) {
return que(r) - que(l - 1);
}
void reset() {
for(int i = 0; i < MX; i++) t[i] = 0;
}
} ft;
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N >> S;
for(auto x : S) a.push_back(x - 'a');
for(auto x : a)
totCnt[x]++;
int i = 0, r = 0;
vector<pair<int,int>> v;
ll ans = 0;
for(auto x : a) {
if(curCnt[x] < totCnt[x] / 2) {
pos[x][curCnt[x]] = i;
ans += r;
} else {
pos[x][curCnt[x]] = i;
v.push_back({pos[x][curCnt[x] - totCnt[x] / 2], r});
r++;
}
curCnt[x]++;
i++;
}
sort(v.begin(), v.end());
ft.reset();
for(auto [l, r] : v) {
ans += ft.que(r + 1, N);
ft.upd(r + 1, 1);
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
2128 KB |
Output is correct |
3 |
Correct |
5 ms |
3028 KB |
Output is correct |
4 |
Correct |
7 ms |
3800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
2128 KB |
Output is correct |
3 |
Correct |
5 ms |
3028 KB |
Output is correct |
4 |
Correct |
7 ms |
3800 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
2128 KB |
Output is correct |
3 |
Correct |
5 ms |
3028 KB |
Output is correct |
4 |
Correct |
7 ms |
3800 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
11 ms |
3928 KB |
Output is correct |
7 |
Correct |
11 ms |
3800 KB |
Output is correct |
8 |
Correct |
14 ms |
3800 KB |
Output is correct |
9 |
Correct |
11 ms |
3800 KB |
Output is correct |
10 |
Correct |
11 ms |
3824 KB |
Output is correct |
11 |
Correct |
12 ms |
3800 KB |
Output is correct |
12 |
Correct |
12 ms |
3776 KB |
Output is correct |
13 |
Correct |
11 ms |
3764 KB |
Output is correct |
14 |
Correct |
11 ms |
3860 KB |
Output is correct |
15 |
Correct |
11 ms |
3800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
2128 KB |
Output is correct |
3 |
Correct |
5 ms |
3028 KB |
Output is correct |
4 |
Correct |
7 ms |
3800 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
11 ms |
3928 KB |
Output is correct |
11 |
Correct |
11 ms |
3800 KB |
Output is correct |
12 |
Correct |
14 ms |
3800 KB |
Output is correct |
13 |
Correct |
11 ms |
3800 KB |
Output is correct |
14 |
Correct |
11 ms |
3824 KB |
Output is correct |
15 |
Correct |
12 ms |
3800 KB |
Output is correct |
16 |
Correct |
12 ms |
3776 KB |
Output is correct |
17 |
Correct |
11 ms |
3764 KB |
Output is correct |
18 |
Correct |
11 ms |
3860 KB |
Output is correct |
19 |
Correct |
11 ms |
3800 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
1 ms |
1108 KB |
Output is correct |
32 |
Correct |
11 ms |
3928 KB |
Output is correct |
33 |
Correct |
11 ms |
3928 KB |
Output is correct |
34 |
Correct |
13 ms |
3884 KB |
Output is correct |
35 |
Correct |
11 ms |
3864 KB |
Output is correct |
36 |
Correct |
12 ms |
3844 KB |
Output is correct |
37 |
Correct |
11 ms |
3800 KB |
Output is correct |
38 |
Correct |
12 ms |
3672 KB |
Output is correct |
39 |
Correct |
12 ms |
3764 KB |
Output is correct |
40 |
Correct |
11 ms |
3672 KB |
Output is correct |
41 |
Correct |
12 ms |
3672 KB |
Output is correct |