#include <bits/stdc++.h>
using namespace std ;
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
const int MAX = 2e5 + 10 ;
int arr[MAX] , val[MAX] ;
int n ;
string s ;
int freq[30] , freq2[30] ;
vector<int>occ[30] ;
long long solve(string s1 , string s2)
{
for(int i = n-1 ; i >= 0 ; --i)
occ[s2[i]-'a'].push_back(i) ;
for(int i = 0 ; i < n ; ++i)
val[occ[s1[i]-'a'].back()] = i , occ[s1[i]-'a'].pop_back() ;
ordered_set<int>s ;
long long cnt = 0 ;
for(int i = 0 ; i < n ; ++i)
{
cnt += s.size() - s.order_of_key(val[i]) ;
s.insert(val[i]) ;
}
return cnt ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
cin>>s ;
for(auto &c : s)
freq[c-'a']++ ;
string s1 = "" , s2 = "" ;
long long ans = 0 , cnt = 0 ;
for(auto &c : s)
{
freq2[c-'a']++ ;
if(freq2[c-'a']*2 <= freq[c-'a'])
s1.push_back(c) , ans += cnt ;
else
s2.push_back(c) , cnt++ ;
}
ans += solve(s1 , s2) ;
return cout<<ans<<"\n" , 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
21 ms |
2436 KB |
Output is correct |
3 |
Correct |
46 ms |
4424 KB |
Output is correct |
4 |
Correct |
72 ms |
6552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
21 ms |
2436 KB |
Output is correct |
3 |
Correct |
46 ms |
4424 KB |
Output is correct |
4 |
Correct |
72 ms |
6552 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
21 ms |
2436 KB |
Output is correct |
3 |
Correct |
46 ms |
4424 KB |
Output is correct |
4 |
Correct |
72 ms |
6552 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
44 ms |
6624 KB |
Output is correct |
7 |
Correct |
43 ms |
6544 KB |
Output is correct |
8 |
Correct |
42 ms |
6484 KB |
Output is correct |
9 |
Correct |
40 ms |
6356 KB |
Output is correct |
10 |
Correct |
46 ms |
6388 KB |
Output is correct |
11 |
Correct |
42 ms |
6268 KB |
Output is correct |
12 |
Correct |
41 ms |
6216 KB |
Output is correct |
13 |
Correct |
40 ms |
6228 KB |
Output is correct |
14 |
Correct |
38 ms |
6188 KB |
Output is correct |
15 |
Correct |
40 ms |
6068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
21 ms |
2436 KB |
Output is correct |
3 |
Correct |
46 ms |
4424 KB |
Output is correct |
4 |
Correct |
72 ms |
6552 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
44 ms |
6624 KB |
Output is correct |
11 |
Correct |
43 ms |
6544 KB |
Output is correct |
12 |
Correct |
42 ms |
6484 KB |
Output is correct |
13 |
Correct |
40 ms |
6356 KB |
Output is correct |
14 |
Correct |
46 ms |
6388 KB |
Output is correct |
15 |
Correct |
42 ms |
6268 KB |
Output is correct |
16 |
Correct |
41 ms |
6216 KB |
Output is correct |
17 |
Correct |
40 ms |
6228 KB |
Output is correct |
18 |
Correct |
38 ms |
6188 KB |
Output is correct |
19 |
Correct |
40 ms |
6068 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
328 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
44 ms |
6636 KB |
Output is correct |
33 |
Correct |
49 ms |
6680 KB |
Output is correct |
34 |
Correct |
42 ms |
6400 KB |
Output is correct |
35 |
Correct |
45 ms |
6360 KB |
Output is correct |
36 |
Correct |
45 ms |
6308 KB |
Output is correct |
37 |
Correct |
44 ms |
6316 KB |
Output is correct |
38 |
Correct |
39 ms |
6224 KB |
Output is correct |
39 |
Correct |
40 ms |
6188 KB |
Output is correct |
40 |
Correct |
45 ms |
6148 KB |
Output is correct |
41 |
Correct |
45 ms |
6104 KB |
Output is correct |