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>
#define int long long
#define float double
#define pb push_back
#define F first
#define S second
#define T int t; cin >> t; while(t--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
/// Benzema is the best player in the world
const int N = 1e6 + 6;
const int M = 1e3 + 3;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int LOG = 28;
int n, cnt[LOG];
bool vis[N];
deque<int> pos[LOG];
string s;
main() {
IOS
cin >> n >> s;
n *= 2;
for(int i = 0; i < n; i++) cnt[s[i] - 'a']++, pos[s[i] - 'a'].push_back(i);
for(int i = 0; i < 26; i++) cnt[i] /= 2;
string s1, s2;
int ans = 0;
for(int i = 0; i < n / 2; i++) {
int c = -1;
for(int j = 0; j < 26; j++) {
if (cnt[j] == 0 || pos[j].size() == 0) continue;
if (c == -1 || pos[j].front() < pos[c].front()) c = j;
}
cnt[c]--;
vis[pos[c].front()] = 1;
ans += pos[c].front() - i;
pos[c].pop_front();
s1 += (c + 'a');
}
for(int i = 0; i < n; i++) if (!vis[i]) s2 += s[i];
for(int i = 0; i < 26; i++) pos[i].clear();
n /= 2;
for(int i = 0; i < n; i++) pos[s2[i] - 'a'].push_back(i);
multiset<int> st;
for(int i = 0; i < n; i++) {
int idx = pos[s1[i] - 'a'].front();
idx += st.size();
ans += max(0ll, idx - i);
st.insert(idx);
pos[s1[i] - 'a'].pop_front();
st.erase(*st.find(i));
}
cout << ans;
}
Compilation message (stderr)
Main.cpp:24:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
24 | main() {
| ^~~~
# | 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... |