/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
cout<<(x?"YES":"NO")<<endl;
}
struct BIT{
vector<int>bit;
int n;
void init(int _n){
bit.assign(_n+5,0LL);
n=_n;
}
void add(int i,int v){
while(i<=n)bit[i]+=v,i+=i&(-i);
}
int sum(int i){
int ret=0;
while(i)ret+=bit[i],i-=i&(-i);
return ret;
}
int query(int l,int r){
return sum(r)-sum(l-1);
}
}b;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#ifndef ONLINE_JUDGE
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
fast
int n;cin>>n;
string s;cin>>s;
b.init(2*n);
vector<int>g[26];
for(int i=0;i<2*n;i++){
g[s[i]-'a'].push_back(i);
}
vector<int>v(2*n+5);
int x=1;
string u="";
for(int i=0;i<2*n;i++){
if(i<g[s[i]-'a'][(int)g[s[i]-'a'].size()/2]){
v[x++]=i+1;
u+=s[i];
}
}
vector<int>cnt(26);
for(int i=0;i<26;i++){
cnt[i]=g[i].size()/2;
}
for(int j=n+1;j<=2*n;j++){
v[j]=g[u[j-n-1]-'a'][cnt[u[j-n-1]-'a']++]+1;
}
ll ans=0;
for(int i=n*2;i>=1;i--){
ans+=b.sum(v[i]);
b.add(v[i],1);
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1432 KB |
Output is correct |
3 |
Correct |
4 ms |
2260 KB |
Output is correct |
4 |
Correct |
9 ms |
3300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
1432 KB |
Output is correct |
3 |
Correct |
4 ms |
2260 KB |
Output is correct |
4 |
Correct |
9 ms |
3300 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 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 |
2 ms |
1432 KB |
Output is correct |
3 |
Correct |
4 ms |
2260 KB |
Output is correct |
4 |
Correct |
9 ms |
3300 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
3580 KB |
Output is correct |
7 |
Correct |
8 ms |
3304 KB |
Output is correct |
8 |
Correct |
7 ms |
3304 KB |
Output is correct |
9 |
Correct |
7 ms |
3304 KB |
Output is correct |
10 |
Correct |
7 ms |
3304 KB |
Output is correct |
11 |
Correct |
7 ms |
3292 KB |
Output is correct |
12 |
Correct |
9 ms |
3456 KB |
Output is correct |
13 |
Correct |
7 ms |
3300 KB |
Output is correct |
14 |
Correct |
7 ms |
3164 KB |
Output is correct |
15 |
Correct |
7 ms |
3304 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
2 ms |
1432 KB |
Output is correct |
3 |
Correct |
4 ms |
2260 KB |
Output is correct |
4 |
Correct |
9 ms |
3300 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 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 |
7 ms |
3580 KB |
Output is correct |
11 |
Correct |
8 ms |
3304 KB |
Output is correct |
12 |
Correct |
7 ms |
3304 KB |
Output is correct |
13 |
Correct |
7 ms |
3304 KB |
Output is correct |
14 |
Correct |
7 ms |
3304 KB |
Output is correct |
15 |
Correct |
7 ms |
3292 KB |
Output is correct |
16 |
Correct |
9 ms |
3456 KB |
Output is correct |
17 |
Correct |
7 ms |
3300 KB |
Output is correct |
18 |
Correct |
7 ms |
3164 KB |
Output is correct |
19 |
Correct |
7 ms |
3304 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 |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
7 ms |
3408 KB |
Output is correct |
33 |
Correct |
8 ms |
3560 KB |
Output is correct |
34 |
Correct |
8 ms |
3292 KB |
Output is correct |
35 |
Correct |
8 ms |
3492 KB |
Output is correct |
36 |
Correct |
8 ms |
3304 KB |
Output is correct |
37 |
Correct |
8 ms |
3560 KB |
Output is correct |
38 |
Correct |
7 ms |
3436 KB |
Output is correct |
39 |
Correct |
7 ms |
3300 KB |
Output is correct |
40 |
Correct |
7 ms |
3304 KB |
Output is correct |
41 |
Correct |
9 ms |
3736 KB |
Output is correct |