This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |