#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int merge(vector<int> &a, int l, int r);
int number_of_inversions(vector<int> &a, int l=0, int r=n-1) {
if(l>=r) return 0;
int mid=l+r>>1, ret=0;
ret+=number_of_inversions(a, l, mid);
ret+=number_of_inversions(a, mid+1, r);
ret+=merge(a, l, r);
return ret;
}
int merge(vector<int> &a, int l, int r) {
vector<int> temp(r-l+1);
int mid=l+r>>1;
int i=l, j=mid+1, k=0, ret=0;
while(i<=mid && j<=r) {
if(a[i]<=a[j]) temp[k++]=a[i++];
else temp[k++]=a[j++], ret+=mid-i+1;
}
while(i<=mid) temp[k++]=a[i++];
while(j<=r) temp[k++]=a[j++];
for(int I=l; I<=r; I++) a[I]=temp[I-l];
return ret;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
string s;
cin >> n >> s;
vector<int> a, cnt(26);
vector<deque<int>> idx(26);
vector<bool> vis(2*n);
for(char c : s) cnt[c-'a']++;
for(int &p : cnt) p>>=1;
int ans=0, pre=0;
for(int i=0; i<2*n; i++) {
if(cnt[s[i]-'a']) {
cnt[s[i]-'a']--;
vis[i]=1;
idx[s[i]-'a'].push_back(i);
ans+=pre;
} else pre++;
}
for(int i=0; i<2*n; i++) {
if(vis[i]) continue;
a.push_back(idx[s[i]-'a'].front());
idx[s[i]-'a'].pop_front();
}
ans+=number_of_inversions(a);
cout << ans << '\n';
return 0;
}
Compilation message
Main.cpp: In function 'long long int number_of_inversions(std::vector<long long int>&, long long int, long long int)':
Main.cpp:8:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
8 | int mid=l+r>>1, ret=0;
| ~^~
Main.cpp: In function 'long long int merge(std::vector<long long int>&, long long int, long long int)':
Main.cpp:16:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
5 ms |
2268 KB |
Output is correct |
4 |
Correct |
8 ms |
3340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
5 ms |
2268 KB |
Output is correct |
4 |
Correct |
8 ms |
3340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
5 ms |
2268 KB |
Output is correct |
4 |
Correct |
8 ms |
3340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
11 ms |
3272 KB |
Output is correct |
7 |
Correct |
11 ms |
3284 KB |
Output is correct |
8 |
Correct |
11 ms |
3284 KB |
Output is correct |
9 |
Correct |
11 ms |
3176 KB |
Output is correct |
10 |
Correct |
11 ms |
3156 KB |
Output is correct |
11 |
Correct |
13 ms |
3144 KB |
Output is correct |
12 |
Correct |
11 ms |
3160 KB |
Output is correct |
13 |
Correct |
10 ms |
3104 KB |
Output is correct |
14 |
Correct |
10 ms |
3036 KB |
Output is correct |
15 |
Correct |
11 ms |
3048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
5 ms |
2268 KB |
Output is correct |
4 |
Correct |
8 ms |
3340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
11 ms |
3272 KB |
Output is correct |
11 |
Correct |
11 ms |
3284 KB |
Output is correct |
12 |
Correct |
11 ms |
3284 KB |
Output is correct |
13 |
Correct |
11 ms |
3176 KB |
Output is correct |
14 |
Correct |
11 ms |
3156 KB |
Output is correct |
15 |
Correct |
13 ms |
3144 KB |
Output is correct |
16 |
Correct |
11 ms |
3160 KB |
Output is correct |
17 |
Correct |
10 ms |
3104 KB |
Output is correct |
18 |
Correct |
10 ms |
3036 KB |
Output is correct |
19 |
Correct |
11 ms |
3048 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
12 ms |
3364 KB |
Output is correct |
33 |
Correct |
12 ms |
3340 KB |
Output is correct |
34 |
Correct |
11 ms |
3328 KB |
Output is correct |
35 |
Correct |
12 ms |
3284 KB |
Output is correct |
36 |
Correct |
11 ms |
3156 KB |
Output is correct |
37 |
Correct |
11 ms |
3160 KB |
Output is correct |
38 |
Correct |
11 ms |
3132 KB |
Output is correct |
39 |
Correct |
11 ms |
3164 KB |
Output is correct |
40 |
Correct |
11 ms |
3060 KB |
Output is correct |
41 |
Correct |
11 ms |
3044 KB |
Output is correct |