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 ll long long
#define ar array
const int mxN=1e5;
int n, cnt[26], p[mxN], ft[mxN+1];
string s, a, b;
ll ans;
vector<int> oc1[26], oc2[26];
void upd(int i) {
for (++i; i <= n; i += i & -i) ++ft[i];
}
int qry(int i) {
int ans = 0;
for (++i; i > 0; i -= i & -i) ans += ft[i];
return ans;
}
int main() {
cin >> n >> s;
for (char c : s) ++cnt[c-'a'];
for (int i=0; i<26; ++i) cnt[i] /= 2;
for (int i = 0, x = 0; i < 2*n; ++i) {
if (cnt[s[i]-'a']) a += s[i], ans += x, --cnt[s[i]-'a'];
else b += s[i], ++x;
}
for (int i = 0; i < n; ++i) {
oc1[a[i] - 'a'].push_back(i);
oc2[b[i] - 'a'].push_back(i);
}
for (int i = 0; i < 26; ++i)
for (int j=0; j<oc1[i].size(); ++j)
p[oc2[i][j]] = oc1[i][j];
for (int i=0; i<n; ++i) {
ans += i - qry(p[i]);
upd(p[i]);
}
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int j=0; j<oc1[i].size(); ++j)
| ~^~~~~~~~~~~~~~
# | 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... |