#include <iostream>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
void Dodaj(set<int>& A, set<int>& B, int x)
{
A.insert(x);
B.insert(-x);
}
void Usun(set<int>& A, set<int>& B, int x)
{
A.erase(x);
B.erase(-x);
}
int main()
{
ios_base::sync_with_stdio(0);
string s;
cin >> s;
set<int> pierA, pierB;
set<int> ostA, ostB;
for (int i = 0; i < s.size(); ++i)
{
if (s[i] == 'a')
Dodaj(pierA, ostA, i);
else
Dodaj(pierB, ostB, i);
}
if (pierA.size() % 2 == 1 && pierB.size() % 2 == 1)
{
cout << "-1";
return 0;
}
ll odp = 0;
int L = 0, P = s.size() - 1;
while (L < P)
{
if (pierA.size() == 0 || pierB.size() == 0)
break;
int piera = *pierA.begin(), pierb = *pierB.begin();
int osta = -(*ostA.begin()), ostb = -(*ostB.begin());
if (pierA.size() >= 2 && (pierB.size() == 1 || (pierB.size() > 1 && piera - osta <= pierb - ostb)))
{
odp += piera - L + P - osta;
if (piera != L)
{
Usun(pierB, ostB, L);
Usun(pierA, ostA, piera);
Dodaj(pierB, ostB, piera);
}
else
Usun(pierA, ostA, L);
if (osta != P)
{
Usun(pierB, ostB, P);
Usun(pierA, ostA, osta);
Dodaj(pierB, ostB, osta);
}
else
Usun(pierA, ostA, P);
}
else
{
odp += pierb - L + P - ostb;
if (pierb != L)
{
Usun(pierA, ostA, L);
Usun(pierB, ostB, pierb);
Dodaj(pierA, ostA, pierb);
}
else
Usun(pierB, ostB, L);
if (ostb != P)
{
Usun(pierA, ostA, P);
Usun(pierB, ostB, ostb);
Dodaj(pierA, ostA, ostb);
}
else
Usun(pierB, ostB, P);
}
++L;
--P;
}
cout << odp;
return 0;
}
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 | #include <iostream> #include <set> #include <cmath> using namespace std; typedef long long ll; void Dodaj(set<int>& A, set<int>& B, int x) { A.insert(x); B.insert(-x); } void Usun(set<int>& A, set<int>& B, int x) { A.erase(x); B.erase(-x); } int main() { ios_base::sync_with_stdio(0); string s; cin >> s; set<int> pierA, pierB; set<int> ostA, ostB; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'a') Dodaj(pierA, ostA, i); else Dodaj(pierB, ostB, i); } if (pierA.size() % 2 == 1 && pierB.size() % 2 == 1) { cout << "-1"; return 0; } ll odp = 0; int L = 0, P = s.size() - 1; while (L < P) { if (pierA.size() == 0 || pierB.size() == 0) break; int piera = *pierA.begin(), pierb = *pierB.begin(); int osta = -(*ostA.begin()), ostb = -(*ostB.begin()); if (pierA.size() >= 2 && (pierB.size() == 1 || (pierB.size() > 1 && piera - osta <= pierb - ostb))) { odp += piera - L + P - osta; if (piera != L) { Usun(pierB, ostB, L); Usun(pierA, ostA, piera); Dodaj(pierB, ostB, piera); } else Usun(pierA, ostA, L); if (osta != P) { Usun(pierB, ostB, P); Usun(pierA, ostA, osta); Dodaj(pierB, ostB, osta); } else Usun(pierA, ostA, P); } else { odp += pierb - L + P - ostb; if (pierb != L) { Usun(pierA, ostA, L); Usun(pierB, ostB, pierb); Dodaj(pierA, ostA, pierb); } else Usun(pierB, ostB, L); if (ostb != P) { Usun(pierA, ostA, P); Usun(pierB, ostB, ostb); Dodaj(pierA, ostA, ostb); } else Usun(pierB, ostB, P); } ++L; --P; } cout << odp; return 0; } |
English