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 fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e5 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, cnt[30], p[N], ans = 0;
string s, a, b;
vector<int> t1[30], t2[30];
int bittree[N];
void update(int i) {
for (++i; i <= n; i += i & -i)
++bittree[i];
}
int getSum(int i) {
int r = 0;
for (++i; i; i -= i & -i)
r += bittree[i];
return r;
}
void process() {
cin >> n >> s;
for (auto c : s) cnt[c - 'a']++;
foru(i, 0, 25) cnt[i] /= 2;
int x = 0;
foru(i, 0, 2 * n - 1) {
if (cnt[s[i] - 'a']) {
a += s[i], ans += x; cnt[s[i] - 'a']--;
} else {
b += s[i]; x++;
}
}
foru(i, 0, n - 1) {
t1[a[i] - 'a'].pb(i);
t2[b[i] - 'a'].pb(i);
}
foru(i, 0, 25) {
for (int j = 0; j < t1[i].size(); ++j) {
p[t2[i][j]] = t1[i][j];
}
}
foru(i, 0, n - 1) {
ans += i - getSum(p[i]);
update(p[i]);
}
cout << ans;
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
Compilation message (stderr)
Main.cpp: In function 'void process()':
Main.cpp:58:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j = 0; j < t1[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... |