#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
constexpr int N = 1e5 + 5;
int fw[N];
void upd(int c){
for(;c<N;c+=c&-c) fw[c]++;
}
int query(int c,int res=0){
for(;c;c-=c&-c) res+=fw[c];
return res;
}
vector<int> convert(string a,string b,int n){
queue<int> q[26];
for(int i=0;i<n;i++) q[b[i]-'a'].push(i);
vector<int> res;
for(int i=0;i<n;i++){
res.pb(q[a[i]-'a'].front()+1);
q[a[i]-'a'].pop();
}
return res;
}
void solve(){
int n;
cin >> n;
string s;
cin >> s;
string s1="",s2="";
int cnt[26],cur[26];
for(int i=0;i<26;i++) cnt[i]=count(all(s),(char)('a'+i));
int ans=0,c2=0;
memset(cur,0,sizeof(cur));
for(int i=0;i<2*n;i++){
cur[s[i]-'a']++;
if(cur[s[i]-'a']>cnt[s[i]-'a']/2){
c2++;
s2.pb(s[i]);
}
else{
ans+=c2;
s1.pb(s[i]);
}
}
vector<int> perm = convert(s1,s2,n);
for(int i=0;i<n;i++){
ans+=query(n)-query(perm[i]);
upd(perm[i]);
}
cout << ans << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1500 KB |
Output is correct |
3 |
Correct |
6 ms |
2656 KB |
Output is correct |
4 |
Correct |
9 ms |
3816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1500 KB |
Output is correct |
3 |
Correct |
6 ms |
2656 KB |
Output is correct |
4 |
Correct |
9 ms |
3816 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 |
1500 KB |
Output is correct |
3 |
Correct |
6 ms |
2656 KB |
Output is correct |
4 |
Correct |
9 ms |
3816 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
10 ms |
3676 KB |
Output is correct |
7 |
Correct |
9 ms |
3680 KB |
Output is correct |
8 |
Correct |
10 ms |
3680 KB |
Output is correct |
9 |
Correct |
9 ms |
3684 KB |
Output is correct |
10 |
Correct |
9 ms |
3680 KB |
Output is correct |
11 |
Correct |
9 ms |
3632 KB |
Output is correct |
12 |
Correct |
10 ms |
3420 KB |
Output is correct |
13 |
Correct |
9 ms |
3576 KB |
Output is correct |
14 |
Correct |
9 ms |
3420 KB |
Output is correct |
15 |
Correct |
9 ms |
3416 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 |
1500 KB |
Output is correct |
3 |
Correct |
6 ms |
2656 KB |
Output is correct |
4 |
Correct |
9 ms |
3816 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 |
10 ms |
3676 KB |
Output is correct |
11 |
Correct |
9 ms |
3680 KB |
Output is correct |
12 |
Correct |
10 ms |
3680 KB |
Output is correct |
13 |
Correct |
9 ms |
3684 KB |
Output is correct |
14 |
Correct |
9 ms |
3680 KB |
Output is correct |
15 |
Correct |
9 ms |
3632 KB |
Output is correct |
16 |
Correct |
10 ms |
3420 KB |
Output is correct |
17 |
Correct |
9 ms |
3576 KB |
Output is correct |
18 |
Correct |
9 ms |
3420 KB |
Output is correct |
19 |
Correct |
9 ms |
3416 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 |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
11 ms |
3812 KB |
Output is correct |
33 |
Correct |
9 ms |
3680 KB |
Output is correct |
34 |
Correct |
10 ms |
3680 KB |
Output is correct |
35 |
Correct |
10 ms |
3684 KB |
Output is correct |
36 |
Correct |
10 ms |
3680 KB |
Output is correct |
37 |
Correct |
9 ms |
3580 KB |
Output is correct |
38 |
Correct |
9 ms |
3420 KB |
Output is correct |
39 |
Correct |
9 ms |
3544 KB |
Output is correct |
40 |
Correct |
9 ms |
3420 KB |
Output is correct |
41 |
Correct |
9 ms |
3384 KB |
Output is correct |