#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 2e5 + 7; const int base = 1<<20; ll tree[2 * base]; void add(int a, int b, int u){ a += base; b += base; tree[a] += u; if(a != b) tree[b] += u; while(a/2 != b/2){ if(a%2 == 0) tree[a + 1] += u; if(b%2 == 1) tree[b - 1] += u; a /= 2; b /= 2; } } int query(int pos){ pos += base; int ans = tree[pos]; pos /= 2; while(pos > 1){ ans += tree[pos]; pos /= 2; } return ans; } int solve(string s){ vector<vector<int>> idxs(26); vector<int> cnt(26); for (int i = 0; i < s.size(); i++){ idxs[s[i] - 'a'].push_back(i); cnt[s[i] - 'a']++; } int odd = 0; for(int i = 0; i < 26; i++){ if(cnt[i]%2 == 1) odd++; } if(odd > 1) return -1; vector<pair<int, int>> pairs; vector<int> targets(s.size()); for (auto idx : idxs){ for (int i = 0; i < idx.size()/2; i++){ pairs.push_back({idx[i], idx[(idx.size() - 1) - i]}); } if (idx.size()%2 > 0) { targets[idx[idx.size() / 2]] = s.size() / 2; } } sort(pairs.begin(), pairs.end()); for (int i = 0; i < pairs.size(); i++){ targets[pairs[i].first] = i; targets[pairs[i].second] = (s.size() - 1) - i; } int res = 0; for (auto i : targets){ res += i - query(i); // punkt add(i, s.size() + 1, 1); // przedzial } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; cout << solve(s) << '\n'; 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 2e5 + 7; const int base = 1<<20; ll tree[2 * base]; void add(int a, int b, int u){ a += base; b += base; tree[a] += u; if(a != b) tree[b] += u; while(a/2 != b/2){ if(a%2 == 0) tree[a + 1] += u; if(b%2 == 1) tree[b - 1] += u; a /= 2; b /= 2; } } int query(int pos){ pos += base; int ans = tree[pos]; pos /= 2; while(pos > 1){ ans += tree[pos]; pos /= 2; } return ans; } int solve(string s){ vector<vector<int>> idxs(26); vector<int> cnt(26); for (int i = 0; i < s.size(); i++){ idxs[s[i] - 'a'].push_back(i); cnt[s[i] - 'a']++; } int odd = 0; for(int i = 0; i < 26; i++){ if(cnt[i]%2 == 1) odd++; } if(odd > 1) return -1; vector<pair<int, int>> pairs; vector<int> targets(s.size()); for (auto idx : idxs){ for (int i = 0; i < idx.size()/2; i++){ pairs.push_back({idx[i], idx[(idx.size() - 1) - i]}); } if (idx.size()%2 > 0) { targets[idx[idx.size() / 2]] = s.size() / 2; } } sort(pairs.begin(), pairs.end()); for (int i = 0; i < pairs.size(); i++){ targets[pairs[i].first] = i; targets[pairs[i].second] = (s.size() - 1) - i; } int res = 0; for (auto i : targets){ res += i - query(i); // punkt add(i, s.size() + 1, 1); // przedzial } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; cout << solve(s) << '\n'; return 0; } |