#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 2e5+5;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define fr first
#define sc second
int n, bit[MAXN];
string v;
vector<int> L[26];
void update(int x, int val) {
for( ; x <= n; x += x&-x)
bit[x] += val;
}
int sum(int x) {
int cnt = 0;
while(x > 0) {
cnt += bit[x];
x -= x&-x;
}
return cnt;
}
int main() {
cin >> n >> v;
n *= 2;
for(int i = 0; i < n; i++) L[v[i] - 'a'].pb(i+1);
vector<pii> p;
for(int i = 0; i < 26; i++)
for(int j = 0; j < sz(L[i])/2; j++)
p.pb({L[i][j], L[i][j + sz(L[i])/2]});
vector<int> flag(n+1);
for(auto [a,b] : p) flag[a] = -1, flag[b] = a;//, cout << a << " " << b << "\n";
ll ans = 0;
for(int i = 1; i <= n; i++) {
if(flag[i] != -1) {
update(flag[i], -1);
ans += sum(flag[i]);
}
else {
update(i, 1);
int x = sum(i-1);
ans += ((i-1) - x)/2;
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1780 KB |
Output is correct |
3 |
Correct |
6 ms |
2660 KB |
Output is correct |
4 |
Correct |
7 ms |
3580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1780 KB |
Output is correct |
3 |
Correct |
6 ms |
2660 KB |
Output is correct |
4 |
Correct |
7 ms |
3580 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
1780 KB |
Output is correct |
3 |
Correct |
6 ms |
2660 KB |
Output is correct |
4 |
Correct |
7 ms |
3580 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
3768 KB |
Output is correct |
7 |
Correct |
10 ms |
3680 KB |
Output is correct |
8 |
Correct |
9 ms |
3748 KB |
Output is correct |
9 |
Correct |
8 ms |
3748 KB |
Output is correct |
10 |
Correct |
11 ms |
3592 KB |
Output is correct |
11 |
Correct |
9 ms |
3716 KB |
Output is correct |
12 |
Correct |
8 ms |
3688 KB |
Output is correct |
13 |
Correct |
8 ms |
3748 KB |
Output is correct |
14 |
Correct |
8 ms |
3420 KB |
Output is correct |
15 |
Correct |
9 ms |
3548 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 |
604 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 |
2 ms |
1780 KB |
Output is correct |
3 |
Correct |
6 ms |
2660 KB |
Output is correct |
4 |
Correct |
7 ms |
3580 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
8 ms |
3768 KB |
Output is correct |
11 |
Correct |
10 ms |
3680 KB |
Output is correct |
12 |
Correct |
9 ms |
3748 KB |
Output is correct |
13 |
Correct |
8 ms |
3748 KB |
Output is correct |
14 |
Correct |
11 ms |
3592 KB |
Output is correct |
15 |
Correct |
9 ms |
3716 KB |
Output is correct |
16 |
Correct |
8 ms |
3688 KB |
Output is correct |
17 |
Correct |
8 ms |
3748 KB |
Output is correct |
18 |
Correct |
8 ms |
3420 KB |
Output is correct |
19 |
Correct |
9 ms |
3548 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 |
604 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 |
10 ms |
3748 KB |
Output is correct |
33 |
Correct |
13 ms |
3824 KB |
Output is correct |
34 |
Correct |
10 ms |
3748 KB |
Output is correct |
35 |
Correct |
9 ms |
3556 KB |
Output is correct |
36 |
Correct |
10 ms |
3792 KB |
Output is correct |
37 |
Correct |
8 ms |
3836 KB |
Output is correct |
38 |
Correct |
9 ms |
3716 KB |
Output is correct |
39 |
Correct |
8 ms |
3748 KB |
Output is correct |
40 |
Correct |
9 ms |
3460 KB |
Output is correct |
41 |
Correct |
8 ms |
3460 KB |
Output is correct |