#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 2e5 + 10;
array<int, MAXN> bit;
void update(int id, int val)
{
while (id < MAXN)
{
bit[id] += val;
id += id&-id;
}
}
int query(int id)
{
int resp = 0;
while (id > 0)
{
resp += bit[id];
id -= id&-id;
}
return resp;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
string s;
cin >> s;
vector<vector<int>> freq(30);
for (int i = 0; i < 2*N; i++) freq[s[i]-'a'].push_back(i);
vector<int> v1(2*N);
vector<pair<int, int>> pares;
for (int c = 0; c < 30; c++)
{
int sz = freq[c].size();
for (int i = 0; i < sz/2; i++)
{
v1[freq[c][i]] = 0; v1[freq[c][i+sz/2]] = 1;
pares.emplace_back(freq[c][i], freq[c][i+sz/2]);
}
}
int resp = 0, cnt1 = 0;
for (int i = 0; i < 2*N; i++)
{
if (v1[i]) cnt1++;
else resp += cnt1;
}
sort(pares.rbegin(), pares.rend());
for (auto &[x, y] : pares)
{
++y;
resp += query(y);
update(y, +1);
}
cout << resp << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
2712 KB |
Output is correct |
3 |
Correct |
7 ms |
4708 KB |
Output is correct |
4 |
Correct |
7 ms |
6240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
2712 KB |
Output is correct |
3 |
Correct |
7 ms |
4708 KB |
Output is correct |
4 |
Correct |
7 ms |
6240 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
2712 KB |
Output is correct |
3 |
Correct |
7 ms |
4708 KB |
Output is correct |
4 |
Correct |
7 ms |
6240 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
11 ms |
6496 KB |
Output is correct |
7 |
Correct |
12 ms |
6440 KB |
Output is correct |
8 |
Correct |
11 ms |
6232 KB |
Output is correct |
9 |
Correct |
11 ms |
6228 KB |
Output is correct |
10 |
Correct |
14 ms |
6232 KB |
Output is correct |
11 |
Correct |
11 ms |
6360 KB |
Output is correct |
12 |
Correct |
11 ms |
5980 KB |
Output is correct |
13 |
Correct |
10 ms |
5980 KB |
Output is correct |
14 |
Correct |
11 ms |
5980 KB |
Output is correct |
15 |
Correct |
11 ms |
6048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
2712 KB |
Output is correct |
3 |
Correct |
7 ms |
4708 KB |
Output is correct |
4 |
Correct |
7 ms |
6240 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
11 ms |
6496 KB |
Output is correct |
11 |
Correct |
12 ms |
6440 KB |
Output is correct |
12 |
Correct |
11 ms |
6232 KB |
Output is correct |
13 |
Correct |
11 ms |
6228 KB |
Output is correct |
14 |
Correct |
14 ms |
6232 KB |
Output is correct |
15 |
Correct |
11 ms |
6360 KB |
Output is correct |
16 |
Correct |
11 ms |
5980 KB |
Output is correct |
17 |
Correct |
10 ms |
5980 KB |
Output is correct |
18 |
Correct |
11 ms |
5980 KB |
Output is correct |
19 |
Correct |
11 ms |
6048 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
11 ms |
6488 KB |
Output is correct |
33 |
Correct |
11 ms |
6444 KB |
Output is correct |
34 |
Correct |
11 ms |
6232 KB |
Output is correct |
35 |
Correct |
11 ms |
6232 KB |
Output is correct |
36 |
Correct |
11 ms |
6228 KB |
Output is correct |
37 |
Correct |
11 ms |
6232 KB |
Output is correct |
38 |
Correct |
11 ms |
5980 KB |
Output is correct |
39 |
Correct |
11 ms |
6232 KB |
Output is correct |
40 |
Correct |
10 ms |
6136 KB |
Output is correct |
41 |
Correct |
10 ms |
6040 KB |
Output is correct |