#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T, typename comp = less<T>>
using ordered_set = tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) ;
#endif
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = (n); i < (m); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i)
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; }
template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; }
void solve() {
string s;
cin >> s;
ordered_set<int> allpos;
deque<int> a, b;
for (int i = 0; i < (int)s.size(); ++i) {
allpos.insert(i);
if (s[i] == 'a')
a.push_back(i);
else
b.push_back(i);
}
if (a.size() & b.size() & 1) {
cout << "-1\n";
return;
}
ll cost = 0;
while (a.size() + b.size() > 1) {
if (a.empty() || b.empty())
break;
if (a.back() < b.back())
swap(a, b);
if (a.size() == 1) {
cost += b.size() / 2;
break;
}
int al = a.front();
a.pop_front();
a.pop_back();
allpos.erase(al);
cost += allpos.order_of_key(al);
}
cout << cost << '\n';
}
int main() {
#ifndef DEBUG
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int z = 1;
// cin >> z; // REMOVE TC!
while (z--)
solve();
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T, typename comp = less<T>> using ordered_set = tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } void solve() { string s; cin >> s; ordered_set<int> allpos; deque<int> a, b; for (int i = 0; i < (int)s.size(); ++i) { allpos.insert(i); if (s[i] == 'a') a.push_back(i); else b.push_back(i); } if (a.size() & b.size() & 1) { cout << "-1\n"; return; } ll cost = 0; while (a.size() + b.size() > 1) { if (a.empty() || b.empty()) break; if (a.back() < b.back()) swap(a, b); if (a.size() == 1) { cost += b.size() / 2; break; } int al = a.front(); a.pop_front(); a.pop_back(); allpos.erase(al); cost += allpos.order_of_key(al); } cout << cost << '\n'; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // cin >> z; // REMOVE TC! while (z--) solve(); return 0; } |
English