#include <bits/stdc++.h> using namespace std; typedef long long LL; template<typename TH> void debug_vars(const char* data, TH head){ cerr << data << "=" << head << "\n"; } template<typename TH, typename... TA> void debug_vars(const char* data, TH head, TA... tail){ while(*data != ',') cerr << *data++; cerr << "=" << head << ","; debug_vars(data+1, tail...); } #ifdef LOCAL #define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif ///////////////////////////////////////////////////////// const int MaxN = 1000005, MaxM = 1000005; const int NoValue = -2, Infinity = -1; LL money[MaxN]; char process[MaxM]; bool visited[MaxM]; LL result[MaxN]; int N, M; void input(){ scanf("%d", &N); for(int i = 0; i < N; i++){ scanf("%lld", &money[i]); } scanf("%d%s", &M, process); fill(result, result+N, NoValue); } template<typename T> class cyclic_vector : public vector<T> { public: using vector<T>::vector; inline LL get_id(LL pos) const { LL S = (LL)vector<T>::size(); assert(S > 0); return (pos%S+S)%S; } const T& operator[](LL pos) const { return vector<T>::operator[](get_id(pos)); } T& operator[](LL pos) { return vector<T>::operator[](get_id(pos)); } }; void process_cycle(const vector<int>& cycle){ int S = (int)cycle.size(); debug(S); string descr(S*2, 0); for(int i = 0; i < S; i++){ descr[i] = descr[i+S] = process[cycle[i]]; } cyclic_vector<vector<int>> levels(2*S+1); cyclic_vector<int> positions(2*S+1, 0); int pref = 0, lowestPref = 0; levels[0].push_back(0); for(int i = 0; i < 2*S; i++){ pref += (descr[i]=='W' ? 1 : -1); lowestPref = min(lowestPref, pref); levels[pref].push_back(i+1); } int cycleDiff = pref/2; pref = 0; // dla kazdej pozycji w cyklu for(int i = 0; i < S; i++){ // bierzemy wszystkich gostkow, ktorzy zaczna z tego miejsca for(int guy = cycle[i]; guy < N; guy += M){ LL cash = money[guy]; debug(i, guy, cash, pref, lowestPref, cycleDiff); if(cycleDiff >= 0){ if(cash - (pref-lowestPref) >= 1){ // ten koles nie zbankrutuje result[guy] = Infinity; } else { // znajdujemy pierwszy moment, w ktorym zejdzie do 0 LL numPlays = levels[pref-cash][positions[pref-cash]] - i; debug(numPlays); result[guy] = (numPlays-1)*N + guy + 1; debug(result[guy]); } } else { // kiedys na pewno zbankrutuje! // na pewno zagra caly cykl pare razy LL numPlays = 0; LL numCycles = (cash - (pref-lowestPref) - cycleDiff - 1) / abs(cycleDiff); if(numCycles < 0) numCycles = 0; numPlays += numCycles * (LL)cycle.size(); LL nowCash = cash + numCycles*cycleDiff; debug(numCycles, nowCash, pref-nowCash); numPlays += levels[pref-nowCash][positions[pref-nowCash]] - i; debug(numPlays); result[guy] = (numPlays-1)*N + guy + 1; } } // uaktualniamy dane w 'levels', 'positions' i 'lowestPref' positions[pref]++; while(positions[lowestPref] == (int)levels[lowestPref].size()) lowestPref++; pref += (descr[i] == 'W' ? 1 : -1); } } void process_cycles(){ for(int start = 0; start < M; start++){ if(visited[start]) continue; vector<int> cycle; int pos = start; do { cycle.push_back(pos); pos = (pos+N)%M; visited[pos] = true; } while(pos != start); process_cycle(cycle); } } int main(){ input(); process_cycles(); assert(count(result, result+N, NoValue) == 0); if(count(result, result+N, Infinity) == N){ printf("-1\n"); } else { LL res = numeric_limits<LL>::max(); for(int i = 0; i < N; i++){ if(result[i] != Infinity) res = min(res, result[i]); } 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 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; template<typename TH> void debug_vars(const char* data, TH head){ cerr << data << "=" << head << "\n"; } template<typename TH, typename... TA> void debug_vars(const char* data, TH head, TA... tail){ while(*data != ',') cerr << *data++; cerr << "=" << head << ","; debug_vars(data+1, tail...); } #ifdef LOCAL #define debug(...) debug_vars(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #endif ///////////////////////////////////////////////////////// const int MaxN = 1000005, MaxM = 1000005; const int NoValue = -2, Infinity = -1; LL money[MaxN]; char process[MaxM]; bool visited[MaxM]; LL result[MaxN]; int N, M; void input(){ scanf("%d", &N); for(int i = 0; i < N; i++){ scanf("%lld", &money[i]); } scanf("%d%s", &M, process); fill(result, result+N, NoValue); } template<typename T> class cyclic_vector : public vector<T> { public: using vector<T>::vector; inline LL get_id(LL pos) const { LL S = (LL)vector<T>::size(); assert(S > 0); return (pos%S+S)%S; } const T& operator[](LL pos) const { return vector<T>::operator[](get_id(pos)); } T& operator[](LL pos) { return vector<T>::operator[](get_id(pos)); } }; void process_cycle(const vector<int>& cycle){ int S = (int)cycle.size(); debug(S); string descr(S*2, 0); for(int i = 0; i < S; i++){ descr[i] = descr[i+S] = process[cycle[i]]; } cyclic_vector<vector<int>> levels(2*S+1); cyclic_vector<int> positions(2*S+1, 0); int pref = 0, lowestPref = 0; levels[0].push_back(0); for(int i = 0; i < 2*S; i++){ pref += (descr[i]=='W' ? 1 : -1); lowestPref = min(lowestPref, pref); levels[pref].push_back(i+1); } int cycleDiff = pref/2; pref = 0; // dla kazdej pozycji w cyklu for(int i = 0; i < S; i++){ // bierzemy wszystkich gostkow, ktorzy zaczna z tego miejsca for(int guy = cycle[i]; guy < N; guy += M){ LL cash = money[guy]; debug(i, guy, cash, pref, lowestPref, cycleDiff); if(cycleDiff >= 0){ if(cash - (pref-lowestPref) >= 1){ // ten koles nie zbankrutuje result[guy] = Infinity; } else { // znajdujemy pierwszy moment, w ktorym zejdzie do 0 LL numPlays = levels[pref-cash][positions[pref-cash]] - i; debug(numPlays); result[guy] = (numPlays-1)*N + guy + 1; debug(result[guy]); } } else { // kiedys na pewno zbankrutuje! // na pewno zagra caly cykl pare razy LL numPlays = 0; LL numCycles = (cash - (pref-lowestPref) - cycleDiff - 1) / abs(cycleDiff); if(numCycles < 0) numCycles = 0; numPlays += numCycles * (LL)cycle.size(); LL nowCash = cash + numCycles*cycleDiff; debug(numCycles, nowCash, pref-nowCash); numPlays += levels[pref-nowCash][positions[pref-nowCash]] - i; debug(numPlays); result[guy] = (numPlays-1)*N + guy + 1; } } // uaktualniamy dane w 'levels', 'positions' i 'lowestPref' positions[pref]++; while(positions[lowestPref] == (int)levels[lowestPref].size()) lowestPref++; pref += (descr[i] == 'W' ? 1 : -1); } } void process_cycles(){ for(int start = 0; start < M; start++){ if(visited[start]) continue; vector<int> cycle; int pos = start; do { cycle.push_back(pos); pos = (pos+N)%M; visited[pos] = true; } while(pos != start); process_cycle(cycle); } } int main(){ input(); process_cycles(); assert(count(result, result+N, NoValue) == 0); if(count(result, result+N, Infinity) == N){ printf("-1\n"); } else { LL res = numeric_limits<LL>::max(); for(int i = 0; i < N; i++){ if(result[i] != Infinity) res = min(res, result[i]); } printf("%lld\n", res); } } |