#include <bits/stdc++.h>
using namespace std;
#define int long long
struct SEGT{
vector < int > tree;
int sz;
SEGT(int x){
sz = x+3;
tree.assign(4 * sz , 0);
}
void _update(int ind , int l , int r , int qp , int qv){
if(l == r){
tree[ind] = qv;
return;
}
int mid = (l+r)/2;
if(mid >= qp)_update(ind*2 ,l , mid , qp , qv);
else _update(ind*2+1 , mid+1 , r , qp , qv);
tree[ind] = tree[ind*2] + tree[ind*2+1];
}
void update(int p , int v){
_update(1,1,sz,p,v);
}
int _query(int ind , int l, int r , int ql , int qr){
if(l >= ql and r <= qr)return tree[ind];
else if(l > qr or r < ql)return 0;
int mid = (l+r)/2;
return _query(ind*2 , l , mid , ql , qr) + _query(ind*2+1,mid+1,r,ql,qr);
}
int query(int l , int r){return _query(1,1,sz,l,r);}
};
int find_inversions(vector < int > a){
SEGT segt((int)a.size()+5);
int ret = 0;
for(int i = a.size()-1;i>=0;i--){
int res = segt.query(1,a[i]);
ret += res;
segt.update(a[i] , 1);
}
return ret;
}
int convert(int n , string &a , string &b){
queue < int > ata[26];
for(int i = 0;i<n;i++)ata[b[i] - 'a'].push(i);
vector < int > perm;
for(int i = 0;i<n;i++){
perm.push_back(ata[a[i] - 'a'].front() + 1);
ata[a[i] - 'a'].pop();
}
return find_inversions(perm);
}
void solve(){
int n,ans=0;cin >> n;
vector < int > ind[26];
int g[2 * n];
memset(g , 0 , sizeof(g));
string str,s1,s2;
cin >> str;
for(int i = 0;i<2*n;i++){
ind[str[i]-'a'].push_back(i);
}
for(int i = 0;i<26;i++){
int sz = (int)ind[i].size();
for(int j = 0;j < (sz/2);j++){
g[ind[i][j]] = 1;
}
}
int cnt0 = 0;// 1 ler için , benden önceki 0 lar
for(int i = 0;i<2 * n;i++){
if(g[i] == 1)ans += cnt0;
else cnt0++;
}
for(int i = 0;i<2 * n;i++){
if(g[i] == 0)s1 += str[i];
else s2 += str[i];
}
ans += convert(n,s1,s2);
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
3676 KB |
Output is correct |
3 |
Correct |
13 ms |
6688 KB |
Output is correct |
4 |
Correct |
19 ms |
9568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
3676 KB |
Output is correct |
3 |
Correct |
13 ms |
6688 KB |
Output is correct |
4 |
Correct |
19 ms |
9568 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 |
6 ms |
3676 KB |
Output is correct |
3 |
Correct |
13 ms |
6688 KB |
Output is correct |
4 |
Correct |
19 ms |
9568 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
24 ms |
9812 KB |
Output is correct |
7 |
Correct |
25 ms |
9820 KB |
Output is correct |
8 |
Correct |
24 ms |
9888 KB |
Output is correct |
9 |
Correct |
24 ms |
9568 KB |
Output is correct |
10 |
Correct |
23 ms |
9564 KB |
Output is correct |
11 |
Correct |
25 ms |
9564 KB |
Output is correct |
12 |
Correct |
23 ms |
9312 KB |
Output is correct |
13 |
Correct |
23 ms |
9420 KB |
Output is correct |
14 |
Correct |
23 ms |
9324 KB |
Output is correct |
15 |
Correct |
23 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
3676 KB |
Output is correct |
3 |
Correct |
13 ms |
6688 KB |
Output is correct |
4 |
Correct |
19 ms |
9568 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 |
24 ms |
9812 KB |
Output is correct |
11 |
Correct |
25 ms |
9820 KB |
Output is correct |
12 |
Correct |
24 ms |
9888 KB |
Output is correct |
13 |
Correct |
24 ms |
9568 KB |
Output is correct |
14 |
Correct |
23 ms |
9564 KB |
Output is correct |
15 |
Correct |
25 ms |
9564 KB |
Output is correct |
16 |
Correct |
23 ms |
9312 KB |
Output is correct |
17 |
Correct |
23 ms |
9420 KB |
Output is correct |
18 |
Correct |
23 ms |
9324 KB |
Output is correct |
19 |
Correct |
23 ms |
9048 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
24 ms |
10020 KB |
Output is correct |
33 |
Correct |
25 ms |
9820 KB |
Output is correct |
34 |
Correct |
26 ms |
9804 KB |
Output is correct |
35 |
Correct |
24 ms |
9564 KB |
Output is correct |
36 |
Correct |
24 ms |
9528 KB |
Output is correct |
37 |
Correct |
24 ms |
9568 KB |
Output is correct |
38 |
Correct |
23 ms |
9504 KB |
Output is correct |
39 |
Correct |
24 ms |
9308 KB |
Output is correct |
40 |
Correct |
23 ms |
9308 KB |
Output is correct |
41 |
Correct |
23 ms |
9052 KB |
Output is correct |