#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n;
int d[N];
int tab[N];
int rev_tab[N];
string S;
int rev[2][N];
int num[N];
int licz[2];
bool initial_check() {
for (int i = 0; i < n; i++) {
licz[S[i] - 'a']++;
}
if ((licz[0] & 1) && (licz[1] & 1)) {
return false;
}
if (!(n & 1)) {
return true;
}
if (licz[0] & 1) {
return true;
}
for (int i = 0; i < n; i++) {
S[i] = 'b' + 'a' - S[i];
}
return true;
}
void compute_rev() {
licz[0] = licz[1] = 0;
for (int i = 0; i < n; i++) {
licz[S[i] - 'a']++;
num[i] = licz[S[i] - 'a'];
rev[S[i] - 'a'][licz[S[i] - 'a']] = i;
}
}
void compute_tab() {
int ind = 1;
for (int i = 0; i < n; i++) {
int c = S[i] - 'a';
if (2 * num[i] > licz[c] + 1) {
continue;
}
if (2 * num[i] == licz[c] + 1) {
tab[i] = (n + 1) / 2;
rev_tab[(n + 1) / 2] = i + 1;
continue;
}
// cout << "siema " << i << endl;
int s = rev[c][licz[c] + 1 - num[i]];
tab[i] = ind;
rev_tab[ind] = i + 1;
tab[s] = n - ind + 1;
rev_tab[n - ind + 1] = s + 1;
ind++;
}
// if (n & 1) {
// rev_tab[(n + 1) / 2] =
// }
}
void insert(int x, int ile) {
if (x > n) {
return;
}
d[x] += ile;
insert(x + (x & (-x)), ile);
}
int sum(int x) {
if (x <= 0) {
return 0;
}
return d[x] + sum(x - (x & (-x)));
}
int sum_int(int a, int b) {
return sum(b) - sum(a - 1);
}
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(0);
cin >> S;
n = S.length();
if (!initial_check()) {
cout << -1 << "\n";
return 0;
}
compute_rev();
compute_tab();
// for (int i = 0; i < n; i++) {
// cout << tab[i] << " ";
// }
// cout << endl;
long long res = 0;
for (int i = 1; i <= n; i++) {
insert(i, 1);
}
for (int i = 1; i <= n; i++) {
res += sum(rev_tab[i] - 1);
insert(rev_tab[i], -1);
}
cout << res << "\n";
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n; int d[N]; int tab[N]; int rev_tab[N]; string S; int rev[2][N]; int num[N]; int licz[2]; bool initial_check() { for (int i = 0; i < n; i++) { licz[S[i] - 'a']++; } if ((licz[0] & 1) && (licz[1] & 1)) { return false; } if (!(n & 1)) { return true; } if (licz[0] & 1) { return true; } for (int i = 0; i < n; i++) { S[i] = 'b' + 'a' - S[i]; } return true; } void compute_rev() { licz[0] = licz[1] = 0; for (int i = 0; i < n; i++) { licz[S[i] - 'a']++; num[i] = licz[S[i] - 'a']; rev[S[i] - 'a'][licz[S[i] - 'a']] = i; } } void compute_tab() { int ind = 1; for (int i = 0; i < n; i++) { int c = S[i] - 'a'; if (2 * num[i] > licz[c] + 1) { continue; } if (2 * num[i] == licz[c] + 1) { tab[i] = (n + 1) / 2; rev_tab[(n + 1) / 2] = i + 1; continue; } // cout << "siema " << i << endl; int s = rev[c][licz[c] + 1 - num[i]]; tab[i] = ind; rev_tab[ind] = i + 1; tab[s] = n - ind + 1; rev_tab[n - ind + 1] = s + 1; ind++; } // if (n & 1) { // rev_tab[(n + 1) / 2] = // } } void insert(int x, int ile) { if (x > n) { return; } d[x] += ile; insert(x + (x & (-x)), ile); } int sum(int x) { if (x <= 0) { return 0; } return d[x] + sum(x - (x & (-x))); } int sum_int(int a, int b) { return sum(b) - sum(a - 1); } int main() { // ios_base::sync_with_stdio(false); // cin.tie(0); cin >> S; n = S.length(); if (!initial_check()) { cout << -1 << "\n"; return 0; } compute_rev(); compute_tab(); // for (int i = 0; i < n; i++) { // cout << tab[i] << " "; // } // cout << endl; long long res = 0; for (int i = 1; i <= n; i++) { insert(i, 1); } for (int i = 1; i <= n; i++) { res += sum(rev_tab[i] - 1); insert(rev_tab[i], -1); } cout << res << "\n"; } |
English