#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
const int maxn = 5e5+10;
const int inf = 1e18+10;
const int lim = 2e5;
int bit[lim+10];
void upd(int pos, int val) {
for(int i = pos; i <= lim; i+= i&-i) {
bit[i]+= val;
}
}
int qry(int pos) {
int val = 0;
for(int i = pos; i > 0; i-= i&-i) {
val+= bit[i];
}
return val;
}
int32_t main() {
// #ifndef ONLINE_JUDGE
// freopen("in.in","r",stdin);
// freopen("out.out","w",stdout);
// #endif
int n;
cin >> n;
string s;
cin >> s;
map<char,int> cnt;
for(auto x : s) {
cnt[x]++;
}
string t;
for(int i = 0; i < 2*n; i++) {
if(cnt[s[i]] > 0) {
cnt[s[i]]-= 2;
t+= s[i];
}
}
map<char,vector<int>> post;
for(int i = 0; i < t.size(); i++) {
post[t[i]].pb(i);
}
for(auto x : post) {
reverse(all(post[x.fr]));
}
for(auto x : s) {
cnt[x]++;
}
int l = 1;
int r = n+1;
vector<int> pos(2*n);
for(int i = 0; i < 2*n; i++) {
if(cnt[s[i]] > 0) {
cnt[s[i]]-= 2;
pos[i] = l;
l++;
}
else {
pos[i] = r+post[s[i]].back();
post[s[i]].pop_back();
}
}
int ans = 0;
for(int i = 0; i < 2*n; i++) {
ans+= i-qry(pos[i]);
upd(pos[i],1);
}
cout << ans << endl;
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:51:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 0; i < t.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
2164 KB |
Output is correct |
3 |
Correct |
8 ms |
3620 KB |
Output is correct |
4 |
Correct |
13 ms |
5276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
2164 KB |
Output is correct |
3 |
Correct |
8 ms |
3620 KB |
Output is correct |
4 |
Correct |
13 ms |
5276 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
2164 KB |
Output is correct |
3 |
Correct |
8 ms |
3620 KB |
Output is correct |
4 |
Correct |
13 ms |
5276 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
29 ms |
5028 KB |
Output is correct |
7 |
Correct |
29 ms |
4828 KB |
Output is correct |
8 |
Correct |
28 ms |
4832 KB |
Output is correct |
9 |
Correct |
28 ms |
4836 KB |
Output is correct |
10 |
Correct |
28 ms |
4716 KB |
Output is correct |
11 |
Correct |
33 ms |
4844 KB |
Output is correct |
12 |
Correct |
27 ms |
4832 KB |
Output is correct |
13 |
Correct |
28 ms |
4800 KB |
Output is correct |
14 |
Correct |
27 ms |
4832 KB |
Output is correct |
15 |
Correct |
27 ms |
4580 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 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 |
2164 KB |
Output is correct |
3 |
Correct |
8 ms |
3620 KB |
Output is correct |
4 |
Correct |
13 ms |
5276 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
29 ms |
5028 KB |
Output is correct |
11 |
Correct |
29 ms |
4828 KB |
Output is correct |
12 |
Correct |
28 ms |
4832 KB |
Output is correct |
13 |
Correct |
28 ms |
4836 KB |
Output is correct |
14 |
Correct |
28 ms |
4716 KB |
Output is correct |
15 |
Correct |
33 ms |
4844 KB |
Output is correct |
16 |
Correct |
27 ms |
4832 KB |
Output is correct |
17 |
Correct |
28 ms |
4800 KB |
Output is correct |
18 |
Correct |
27 ms |
4832 KB |
Output is correct |
19 |
Correct |
27 ms |
4580 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 |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
444 KB |
Output is correct |
27 |
Correct |
1 ms |
440 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 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 |
29 ms |
4912 KB |
Output is correct |
33 |
Correct |
29 ms |
4844 KB |
Output is correct |
34 |
Correct |
29 ms |
4844 KB |
Output is correct |
35 |
Correct |
28 ms |
4836 KB |
Output is correct |
36 |
Correct |
28 ms |
4836 KB |
Output is correct |
37 |
Correct |
29 ms |
4748 KB |
Output is correct |
38 |
Correct |
28 ms |
4576 KB |
Output is correct |
39 |
Correct |
27 ms |
4776 KB |
Output is correct |
40 |
Correct |
27 ms |
4516 KB |
Output is correct |
41 |
Correct |
27 ms |
4512 KB |
Output is correct |