#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:11: 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:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | int mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
2492 KB |
Output is correct |
4 |
Correct |
7 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
2492 KB |
Output is correct |
4 |
Correct |
7 ms |
3540 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
2492 KB |
Output is correct |
4 |
Correct |
7 ms |
3540 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
12 ms |
3528 KB |
Output is correct |
7 |
Correct |
14 ms |
3412 KB |
Output is correct |
8 |
Correct |
11 ms |
3404 KB |
Output is correct |
9 |
Correct |
14 ms |
3412 KB |
Output is correct |
10 |
Correct |
11 ms |
3404 KB |
Output is correct |
11 |
Correct |
11 ms |
3288 KB |
Output is correct |
12 |
Correct |
11 ms |
3280 KB |
Output is correct |
13 |
Correct |
11 ms |
3296 KB |
Output is correct |
14 |
Correct |
11 ms |
3244 KB |
Output is correct |
15 |
Correct |
13 ms |
3252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
1364 KB |
Output is correct |
3 |
Correct |
6 ms |
2492 KB |
Output is correct |
4 |
Correct |
7 ms |
3540 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
292 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
12 ms |
3528 KB |
Output is correct |
11 |
Correct |
14 ms |
3412 KB |
Output is correct |
12 |
Correct |
11 ms |
3404 KB |
Output is correct |
13 |
Correct |
14 ms |
3412 KB |
Output is correct |
14 |
Correct |
11 ms |
3404 KB |
Output is correct |
15 |
Correct |
11 ms |
3288 KB |
Output is correct |
16 |
Correct |
11 ms |
3280 KB |
Output is correct |
17 |
Correct |
11 ms |
3296 KB |
Output is correct |
18 |
Correct |
11 ms |
3244 KB |
Output is correct |
19 |
Correct |
13 ms |
3252 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
328 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
320 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
324 KB |
Output is correct |
32 |
Correct |
12 ms |
3572 KB |
Output is correct |
33 |
Correct |
11 ms |
3520 KB |
Output is correct |
34 |
Correct |
15 ms |
3528 KB |
Output is correct |
35 |
Correct |
16 ms |
3356 KB |
Output is correct |
36 |
Correct |
11 ms |
3388 KB |
Output is correct |
37 |
Correct |
12 ms |
3332 KB |
Output is correct |
38 |
Correct |
16 ms |
3280 KB |
Output is correct |
39 |
Correct |
15 ms |
3264 KB |
Output is correct |
40 |
Correct |
10 ms |
3248 KB |
Output is correct |
41 |
Correct |
11 ms |
3220 KB |
Output is correct |