#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
const int maxn = 2e5+5;
int bit[maxn];
void upd(int x){
for(; x<maxn; x+=x&-x) bit[x]++;
}
int query(int x){
int rt=0;
for(; x>0; x-=x&-x) rt+=bit[x];
return rt;
}
int ord(char c){
return (int)(c-'a');
}
signed main(){
int n; cin >> n;
string s; cin >> s;
vector<int> mp(26,0), cnt(26,0);
for(char c : s) cnt[ord(c)]++;
int cnt1=0, cnt2=0;
vector<int> arr1, arr2;
vector<int> trans(2*n,0);
// cout << endl;
set<int> pos[26];
for(int i=0; i<2*n; i++){
int p = ord(s[i]);
if(mp[p]<cnt[p]/2){
mp[p]++;
cnt1++;
trans[i]=cnt1;
pos[p].insert(cnt1);
}
}
for(int i=0; i<2*n; i++) if(!trans[i]){
int p = ord(s[i]);
assert(pos[p].size()>0);
trans[i] = (*pos[p].begin())+n;
pos[p].erase(pos[p].begin());
}
// for(int x : trans) cout << x << " ";
// cout << endl;
int ans=0;
for(int i=0; i<2*n; i++ ){
ans += i-query(trans[i]-1);
upd(trans[i]);
}
cout << ans << '\n';
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:29:14: warning: unused variable 'cnt2' [-Wunused-variable]
29 | int cnt1=0, cnt2=0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
2908 KB |
Output is correct |
3 |
Correct |
13 ms |
5612 KB |
Output is correct |
4 |
Correct |
20 ms |
8392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
2908 KB |
Output is correct |
3 |
Correct |
13 ms |
5612 KB |
Output is correct |
4 |
Correct |
20 ms |
8392 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
2908 KB |
Output is correct |
3 |
Correct |
13 ms |
5612 KB |
Output is correct |
4 |
Correct |
20 ms |
8392 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
23 ms |
8364 KB |
Output is correct |
7 |
Correct |
25 ms |
8168 KB |
Output is correct |
8 |
Correct |
26 ms |
8164 KB |
Output is correct |
9 |
Correct |
24 ms |
8168 KB |
Output is correct |
10 |
Correct |
22 ms |
7916 KB |
Output is correct |
11 |
Correct |
23 ms |
7872 KB |
Output is correct |
12 |
Correct |
25 ms |
7916 KB |
Output is correct |
13 |
Correct |
27 ms |
7796 KB |
Output is correct |
14 |
Correct |
25 ms |
7648 KB |
Output is correct |
15 |
Correct |
25 ms |
7660 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
344 KB |
Output is correct |
2 |
Correct |
7 ms |
2908 KB |
Output is correct |
3 |
Correct |
13 ms |
5612 KB |
Output is correct |
4 |
Correct |
20 ms |
8392 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
23 ms |
8364 KB |
Output is correct |
11 |
Correct |
25 ms |
8168 KB |
Output is correct |
12 |
Correct |
26 ms |
8164 KB |
Output is correct |
13 |
Correct |
24 ms |
8168 KB |
Output is correct |
14 |
Correct |
22 ms |
7916 KB |
Output is correct |
15 |
Correct |
23 ms |
7872 KB |
Output is correct |
16 |
Correct |
25 ms |
7916 KB |
Output is correct |
17 |
Correct |
27 ms |
7796 KB |
Output is correct |
18 |
Correct |
25 ms |
7648 KB |
Output is correct |
19 |
Correct |
25 ms |
7660 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
1 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 |
344 KB |
Output is correct |
32 |
Correct |
29 ms |
8192 KB |
Output is correct |
33 |
Correct |
24 ms |
8184 KB |
Output is correct |
34 |
Correct |
21 ms |
8168 KB |
Output is correct |
35 |
Correct |
21 ms |
8216 KB |
Output is correct |
36 |
Correct |
23 ms |
8052 KB |
Output is correct |
37 |
Correct |
23 ms |
7876 KB |
Output is correct |
38 |
Correct |
25 ms |
7804 KB |
Output is correct |
39 |
Correct |
23 ms |
7916 KB |
Output is correct |
40 |
Correct |
25 ms |
7660 KB |
Output is correct |
41 |
Correct |
23 ms |
7708 KB |
Output is correct |