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;
const int N = 2e5+1;
int segtree[4*N] = {0};
int freq[30];
vector<char>urt;
deque<int>dex[30];
vector<int>isi;
void update(int idx, int low, int high, int val) {
if(high==low && low==val) {
segtree[idx]=1;
return;
}
if(val<low || val>high) return;
int mid=(low+high)/2;
update(2*idx,low,mid,val);
update(2*idx+1,mid+1,high,val);
segtree[idx] = segtree[2*idx]+segtree[2*idx+1];
}
int quer(int idx, int low, int high, int l, int r) {
if(low>=l && high<=r) {
return segtree[idx];
}
if(low>r || high<l) return 0;
int mid = (low+high)/2;
int left = quer(2*idx,low,mid,l,r);
int right = quer(2*idx+1,mid+1,high,l,r);
return left+right;
}
int main() {
int n;
cin >> n;
n*=2;
string asli;
cin >> asli;
for(int i=0; i<n; i++) {
freq[asli[i]-97]++;
}
for(int i=0; i<n; i++) {
if(freq[asli[i]-97]>0) {
urt.push_back(asli[i]);
freq[asli[i]-97]-=2;
}
}
int sizeurt=urt.size();
for(int i=0; i<sizeurt; i++) {
urt.push_back(urt[i]);
}
for(int i=0; i<n; i++) {
dex[urt[i]-97].push_back(i);
}
for(int i=0; i<n; i++) {
isi.push_back(dex[asli[i]-97].front());
dex[asli[i]-97].pop_front();
}
int jwbn=0;
for(int i=0; i<n; i++) {
jwbn+=quer(1,0,n-1,isi[i]+1,n-1);
update(1,0,n-1,isi[i]);
}
cout << jwbn << endl;
return 0;
}
# | 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... |