#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
Main.cpp:24:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
24 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
980 KB |
Output is correct |
3 |
Correct |
7 ms |
1760 KB |
Output is correct |
4 |
Correct |
12 ms |
2400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
980 KB |
Output is correct |
3 |
Correct |
7 ms |
1760 KB |
Output is correct |
4 |
Correct |
12 ms |
2400 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
980 KB |
Output is correct |
3 |
Correct |
7 ms |
1760 KB |
Output is correct |
4 |
Correct |
12 ms |
2400 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
39 ms |
2520 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
980 KB |
Output is correct |
3 |
Correct |
7 ms |
1760 KB |
Output is correct |
4 |
Correct |
12 ms |
2400 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |