#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; struct Buddy { Buddy( int money ) : _money(money) , _cycle(-1) , _howfar(0) {} int _money; int _cycle; unsigned long long _howfar; }; struct Game { Game( char a ) : _result ( 'W' == a ? 1 : -1 ) {} int _result; vector<int> _guests; vector<int> _low_guests; }; struct LessMoney { bool operator() (const Buddy *x, const Buddy *y) const { return x->_money < y->_money; } }; int main() { ios_base::sync_with_stdio(false); long long N; cin >> N; vector<Buddy> team; team.reserve(N); for (int i = 0; i < N; i++) { int a; cin >> a; team.push_back(Buddy(a)); } long long M; cin >> M; vector<Game> machine; machine.reserve(M); for (int i = 0; i < M; i++) { char a; cin >> a; machine.push_back( Game(a) ); } for (int i = 0; i < N; i++) { machine[i%M]._guests.push_back(i); } const long long NO_RESULT = 1000000000000ULL; long long result = NO_RESULT; for (int i = 0; i < N; i++) { auto &b = team[i]; if (-1 == b._cycle) { b._cycle = i; long long cyclestart = i%M; long long cyclepos = (cyclestart+M*N-N)%M; int sum = machine[cyclestart]._result; int minsum = 0; int len = 1; auto &startgame = machine[cyclestart]; while (cyclestart != cyclepos) { auto &m = machine[cyclepos]; sum += m._result; minsum = min(minsum, sum); if (!m._guests.empty()) { for (auto &guest: m._guests) { auto &moveme = team[guest]; moveme._cycle = i; bool hi = moveme._money + sum > 0 && moveme._money + minsum > 0; moveme._money += sum; moveme._howfar += len; if (hi) startgame._guests.push_back(guest); else startgame._low_guests.push_back(guest); guest = -1; } m._guests.erase (remove_if (m._guests.begin(), m._guests.end(), [] (const int i) { return i == -1; } ), m._guests.end()); } cyclepos = (cyclepos-N+N*M)%M; len++; } minsum = machine[cyclepos]._result; cyclepos = (cyclestart+N)%M; int localsum = minsum; while (cyclepos != cyclestart) { localsum += machine[cyclepos]._result; minsum = min(minsum, localsum); cyclepos = (cyclepos+N)%M; } // sum = sum of cycle // minsum = minimum of cycle // len = length of cycle int target = min(sum,minsum); if (sum < 0) { // now everyone is either in a single position or in the last cycle // first, take guests from first position and add cycles for (auto &guest : startgame._guests) { auto &g = team[guest]; if (g._money + target > 0) // will go over at least one more cycle { // how many cycles until target hit unsigned long long cycles = (g._money + target) / (-sum); g._money -= cycles*(-sum); g._howfar += cycles*len; } } } // now everyone is on the last cycle, compute the last part int lastlen = 0; int lastsum = 0; cyclepos = cyclestart; multiset<Buddy*,LessMoney> buddies = multiset<Buddy*,LessMoney>(LessMoney()); for (auto guest : startgame._guests) { buddies.insert(&team[guest]); } startgame._guests.clear(); // having the prio-queue, let's go through the final cycle while (!buddies.empty()) { lastsum += machine[cyclepos]._result; while (!buddies.empty() && (*buddies.begin())->_money + lastsum <= 0) { auto buddy = *buddies.begin(); buddy->_howfar += lastlen; buddy->_money = 0; buddies.erase(buddies.begin()); } cyclepos = (cyclepos+N)%M; lastlen++; if (lastlen>M) break; } for (auto guest : startgame._low_guests) { buddies.insert(&team[guest]); } lastlen = 1; lastsum = 0; cyclepos = cyclestart; while (!buddies.empty()) { lastsum -= machine[cyclepos]._result; while (!buddies.empty() && (*buddies.rbegin())->_money + lastsum == 0) { auto buddy = *buddies.rbegin(); buddy->_howfar -= lastlen; buddy->_money = 0; buddies.erase(--buddies.end()); } cyclepos = (cyclepos+M*N-N)%M; lastlen++; if (lastlen>M) break; } } } // finally, compute the minimum for (int i = 0; i < N; i++) { if (0 == team[i]._money) { long long localres = i+1 + team[i]._howfar*N; result = min(localres, result); } } cout << ((NO_RESULT == result) ? -1 : result) << endl; 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; struct Buddy { Buddy( int money ) : _money(money) , _cycle(-1) , _howfar(0) {} int _money; int _cycle; unsigned long long _howfar; }; struct Game { Game( char a ) : _result ( 'W' == a ? 1 : -1 ) {} int _result; vector<int> _guests; vector<int> _low_guests; }; struct LessMoney { bool operator() (const Buddy *x, const Buddy *y) const { return x->_money < y->_money; } }; int main() { ios_base::sync_with_stdio(false); long long N; cin >> N; vector<Buddy> team; team.reserve(N); for (int i = 0; i < N; i++) { int a; cin >> a; team.push_back(Buddy(a)); } long long M; cin >> M; vector<Game> machine; machine.reserve(M); for (int i = 0; i < M; i++) { char a; cin >> a; machine.push_back( Game(a) ); } for (int i = 0; i < N; i++) { machine[i%M]._guests.push_back(i); } const long long NO_RESULT = 1000000000000ULL; long long result = NO_RESULT; for (int i = 0; i < N; i++) { auto &b = team[i]; if (-1 == b._cycle) { b._cycle = i; long long cyclestart = i%M; long long cyclepos = (cyclestart+M*N-N)%M; int sum = machine[cyclestart]._result; int minsum = 0; int len = 1; auto &startgame = machine[cyclestart]; while (cyclestart != cyclepos) { auto &m = machine[cyclepos]; sum += m._result; minsum = min(minsum, sum); if (!m._guests.empty()) { for (auto &guest: m._guests) { auto &moveme = team[guest]; moveme._cycle = i; bool hi = moveme._money + sum > 0 && moveme._money + minsum > 0; moveme._money += sum; moveme._howfar += len; if (hi) startgame._guests.push_back(guest); else startgame._low_guests.push_back(guest); guest = -1; } m._guests.erase (remove_if (m._guests.begin(), m._guests.end(), [] (const int i) { return i == -1; } ), m._guests.end()); } cyclepos = (cyclepos-N+N*M)%M; len++; } minsum = machine[cyclepos]._result; cyclepos = (cyclestart+N)%M; int localsum = minsum; while (cyclepos != cyclestart) { localsum += machine[cyclepos]._result; minsum = min(minsum, localsum); cyclepos = (cyclepos+N)%M; } // sum = sum of cycle // minsum = minimum of cycle // len = length of cycle int target = min(sum,minsum); if (sum < 0) { // now everyone is either in a single position or in the last cycle // first, take guests from first position and add cycles for (auto &guest : startgame._guests) { auto &g = team[guest]; if (g._money + target > 0) // will go over at least one more cycle { // how many cycles until target hit unsigned long long cycles = (g._money + target) / (-sum); g._money -= cycles*(-sum); g._howfar += cycles*len; } } } // now everyone is on the last cycle, compute the last part int lastlen = 0; int lastsum = 0; cyclepos = cyclestart; multiset<Buddy*,LessMoney> buddies = multiset<Buddy*,LessMoney>(LessMoney()); for (auto guest : startgame._guests) { buddies.insert(&team[guest]); } startgame._guests.clear(); // having the prio-queue, let's go through the final cycle while (!buddies.empty()) { lastsum += machine[cyclepos]._result; while (!buddies.empty() && (*buddies.begin())->_money + lastsum <= 0) { auto buddy = *buddies.begin(); buddy->_howfar += lastlen; buddy->_money = 0; buddies.erase(buddies.begin()); } cyclepos = (cyclepos+N)%M; lastlen++; if (lastlen>M) break; } for (auto guest : startgame._low_guests) { buddies.insert(&team[guest]); } lastlen = 1; lastsum = 0; cyclepos = cyclestart; while (!buddies.empty()) { lastsum -= machine[cyclepos]._result; while (!buddies.empty() && (*buddies.rbegin())->_money + lastsum == 0) { auto buddy = *buddies.rbegin(); buddy->_howfar -= lastlen; buddy->_money = 0; buddies.erase(--buddies.end()); } cyclepos = (cyclepos+M*N-N)%M; lastlen++; if (lastlen>M) break; } } } // finally, compute the minimum for (int i = 0; i < N; i++) { if (0 == team[i]._money) { long long localres = i+1 + team[i]._howfar*N; result = min(localres, result); } } cout << ((NO_RESULT == result) ? -1 : result) << endl; return 0; } |