#include <bits/stdc++.h>
#define MAX 200001
#define INF LLONG_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define gett(x,m) get<m>(x)
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define sorta(a,sz) sort(a,a+sz)
#define inputar(a,b){\
for(int i=0;i<b;i++){\
cin >> a[i];\
}\
}
#define inputvec(a,b){\
for(int i=0;i<b;i++){\
ll num;\
cin >> num;\
a.pb(num);\
}\
}
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << "\n";\
}
#define outputvec(a){\
for(auto x:a){\
cout << x << " ";\
}\
cout << "\n";\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
ll n,q,t=1,m,n2,m2,k,cnt=0,x,y,z,x2,y2,z2,res1=0,a[MAX],b[MAX];
ll c[1501][1501];
ll fact[MAX];
ll inv_fact[MAX];
string str[MAX];
string s1,s2;
const int mod = 998244353;
ll dx[4]={1,0,0,-1};
ll dy[4]={0,1,-1,0};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
ll sum[MAX];
void update(ll i,ll v){
while(i<=n){
sum[i]+=v;
i+=(i&(-i));
}
}
ll query(ll i){
ll res=0;
while(i>=1){
res+=sum[i];
i-=(i&(-i));
}
return res;
}
void solve(){
cin >> n;
cin >> s1;
for(int i=0;i<2*n;i++){
a[s1[i]-'a']++;
}
string str1,str2;
str1=str2="";
ll res=0;
for(int i=0;i<2*n;i++){
if(b[s1[i]-'a']<a[s1[i]-'a']/2){
res+=i-str1.size();
str1+=s1[i];
}
else{
str2+=s1[i];
}
b[s1[i]-'a']++;
}
reset(a,26,0);
ll c[26][n];
for(int i=0;i<n;i++){
c[str1[i]-'a'][a[str1[i]-'a']]=i+1;
a[str1[i]-'a']++;
}
reset(a,26,0);
for(int i=0;i<n;i++){
res+=query(n)-query(c[str2[i]-'a'][a[str2[i]-'a']]);
update(c[str2[i]-'a'][a[str2[i]-'a']],1);
a[str2[i]-'a']++;
}
cout << res << "\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//USACO("poetry");
//freopen("input.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ifstream fin("template.in");
ofstream fout("template.out");
*/
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ifstream fin("template.in");
ofstream fout("template.out");
*/
/*
ll b[51][51];
b[0][0] = 1;
for (int n = 1; n <= 50; ++n){
b[n][0] = b[n][n] = 1;
for (int k = 1; k < n; ++k)
b[n][k] = b[n - 1][k - 1] + b[n - 1][k];
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6552 KB |
Output is correct |
2 |
Correct |
12 ms |
13780 KB |
Output is correct |
3 |
Correct |
14 ms |
21072 KB |
Output is correct |
4 |
Correct |
20 ms |
28216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6552 KB |
Output is correct |
2 |
Correct |
12 ms |
13780 KB |
Output is correct |
3 |
Correct |
14 ms |
21072 KB |
Output is correct |
4 |
Correct |
20 ms |
28216 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6552 KB |
Output is correct |
2 |
Correct |
12 ms |
13780 KB |
Output is correct |
3 |
Correct |
14 ms |
21072 KB |
Output is correct |
4 |
Correct |
20 ms |
28216 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
24 ms |
28256 KB |
Output is correct |
7 |
Correct |
20 ms |
28212 KB |
Output is correct |
8 |
Correct |
21 ms |
27940 KB |
Output is correct |
9 |
Correct |
22 ms |
27700 KB |
Output is correct |
10 |
Correct |
23 ms |
27484 KB |
Output is correct |
11 |
Correct |
20 ms |
27276 KB |
Output is correct |
12 |
Correct |
20 ms |
27104 KB |
Output is correct |
13 |
Correct |
19 ms |
26884 KB |
Output is correct |
14 |
Correct |
20 ms |
26712 KB |
Output is correct |
15 |
Correct |
19 ms |
26432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6740 KB |
Output is correct |
3 |
Correct |
3 ms |
6740 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
3 ms |
6740 KB |
Output is correct |
6 |
Correct |
4 ms |
6740 KB |
Output is correct |
7 |
Correct |
4 ms |
6732 KB |
Output is correct |
8 |
Correct |
4 ms |
6720 KB |
Output is correct |
9 |
Correct |
4 ms |
6724 KB |
Output is correct |
10 |
Correct |
5 ms |
6740 KB |
Output is correct |
11 |
Correct |
4 ms |
6740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6552 KB |
Output is correct |
2 |
Correct |
12 ms |
13780 KB |
Output is correct |
3 |
Correct |
14 ms |
21072 KB |
Output is correct |
4 |
Correct |
20 ms |
28216 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6484 KB |
Output is correct |
8 |
Correct |
3 ms |
6552 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
24 ms |
28256 KB |
Output is correct |
11 |
Correct |
20 ms |
28212 KB |
Output is correct |
12 |
Correct |
21 ms |
27940 KB |
Output is correct |
13 |
Correct |
22 ms |
27700 KB |
Output is correct |
14 |
Correct |
23 ms |
27484 KB |
Output is correct |
15 |
Correct |
20 ms |
27276 KB |
Output is correct |
16 |
Correct |
20 ms |
27104 KB |
Output is correct |
17 |
Correct |
19 ms |
26884 KB |
Output is correct |
18 |
Correct |
20 ms |
26712 KB |
Output is correct |
19 |
Correct |
19 ms |
26432 KB |
Output is correct |
20 |
Correct |
3 ms |
6484 KB |
Output is correct |
21 |
Correct |
3 ms |
6740 KB |
Output is correct |
22 |
Correct |
3 ms |
6740 KB |
Output is correct |
23 |
Correct |
4 ms |
6740 KB |
Output is correct |
24 |
Correct |
3 ms |
6740 KB |
Output is correct |
25 |
Correct |
4 ms |
6740 KB |
Output is correct |
26 |
Correct |
4 ms |
6732 KB |
Output is correct |
27 |
Correct |
4 ms |
6720 KB |
Output is correct |
28 |
Correct |
4 ms |
6724 KB |
Output is correct |
29 |
Correct |
5 ms |
6740 KB |
Output is correct |
30 |
Correct |
4 ms |
6740 KB |
Output is correct |
31 |
Correct |
4 ms |
6484 KB |
Output is correct |
32 |
Correct |
20 ms |
28400 KB |
Output is correct |
33 |
Correct |
22 ms |
28168 KB |
Output is correct |
34 |
Correct |
24 ms |
27996 KB |
Output is correct |
35 |
Correct |
21 ms |
27784 KB |
Output is correct |
36 |
Correct |
22 ms |
27548 KB |
Output is correct |
37 |
Correct |
20 ms |
27272 KB |
Output is correct |
38 |
Correct |
20 ms |
27048 KB |
Output is correct |
39 |
Correct |
22 ms |
26848 KB |
Output is correct |
40 |
Correct |
20 ms |
26628 KB |
Output is correct |
41 |
Correct |
24 ms |
26460 KB |
Output is correct |