#include <string> #include <deque> #include <stdio.h> #include <set> int main() { char tab[200001]; scanf("%s", tab); std::string s = tab; std::set<int> first, second; for(int i =0;i<s.size();i++) { if(s[i] == 'b') second.insert(i); else first.insert(i); } long long res =0 ; bool wrong = 0; for(int i=0;i<s.size()/2;i++) { char b = s[i]; char e = s[s.size()- 1 - i]; if(b== e) continue; long long res1 = 1000000000, res2 = 1000000000; //first case change first to second int pos, pos2; std::set<int>* firstPtr; std::set<int>* secondPtr; auto a = first.find(i); if(a != first.end()) first.erase(a); else second.erase(second.erase(i)); a = first.find(s.size() - i - 1); if(a != first.end()) first.erase(a); else second.erase(second.erase(s.size() - 1 -i)); if(e == 'a') // c { if(!first.empty()) { firstPtr = &first; auto v = *first.begin(); if(v > i) res1 = v - i; } } else { if(!second.empty()) { firstPtr = &second; auto v= *second.begin(); if(v > i) res1 = v - i; } } //change end to begin if(b == 'a') // c { int index = s.size() - i - 1; if(!first.empty()) { int v = *first.rbegin(); if(index > v) res2 = index- v; secondPtr = &first; } } else { int index = s.size() - i - 1; if(!second.empty()) { int v = *second.rbegin(); if(index > v) res2 = index - v; secondPtr = &second; } } if(res1 == 1000000000 && res2==1000000000) { wrong = true; break; } if(res1<=res2) // firstCase { if(s[i] == 'a') first.insert(*firstPtr->begin()); else second.insert(*firstPtr->begin()); std::swap(s[i], s[*firstPtr->begin()]); firstPtr->erase(firstPtr->begin()); res+=res1; } else { if(s[s.size()-1-i] == 'a') first.insert(*secondPtr->rbegin()); else second.insert(*secondPtr->rbegin()); std::swap(s[s.size() - 1- i], s[*secondPtr->rbegin()]); auto toRemove = secondPtr->end(); toRemove--; secondPtr->erase(toRemove); res+=res2; } } if(wrong) printf("-1\n"); else printf("%lld\n", res); }
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 115 116 117 118 119 120 121 122 123 124 | #include <string> #include <deque> #include <stdio.h> #include <set> int main() { char tab[200001]; scanf("%s", tab); std::string s = tab; std::set<int> first, second; for(int i =0;i<s.size();i++) { if(s[i] == 'b') second.insert(i); else first.insert(i); } long long res =0 ; bool wrong = 0; for(int i=0;i<s.size()/2;i++) { char b = s[i]; char e = s[s.size()- 1 - i]; if(b== e) continue; long long res1 = 1000000000, res2 = 1000000000; //first case change first to second int pos, pos2; std::set<int>* firstPtr; std::set<int>* secondPtr; auto a = first.find(i); if(a != first.end()) first.erase(a); else second.erase(second.erase(i)); a = first.find(s.size() - i - 1); if(a != first.end()) first.erase(a); else second.erase(second.erase(s.size() - 1 -i)); if(e == 'a') // c { if(!first.empty()) { firstPtr = &first; auto v = *first.begin(); if(v > i) res1 = v - i; } } else { if(!second.empty()) { firstPtr = &second; auto v= *second.begin(); if(v > i) res1 = v - i; } } //change end to begin if(b == 'a') // c { int index = s.size() - i - 1; if(!first.empty()) { int v = *first.rbegin(); if(index > v) res2 = index- v; secondPtr = &first; } } else { int index = s.size() - i - 1; if(!second.empty()) { int v = *second.rbegin(); if(index > v) res2 = index - v; secondPtr = &second; } } if(res1 == 1000000000 && res2==1000000000) { wrong = true; break; } if(res1<=res2) // firstCase { if(s[i] == 'a') first.insert(*firstPtr->begin()); else second.insert(*firstPtr->begin()); std::swap(s[i], s[*firstPtr->begin()]); firstPtr->erase(firstPtr->begin()); res+=res1; } else { if(s[s.size()-1-i] == 'a') first.insert(*secondPtr->rbegin()); else second.insert(*secondPtr->rbegin()); std::swap(s[s.size() - 1- i], s[*secondPtr->rbegin()]); auto toRemove = secondPtr->end(); toRemove--; secondPtr->erase(toRemove); res+=res2; } } if(wrong) printf("-1\n"); else printf("%lld\n", res); } |