#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <climits> #include <queue> #include <vector> #include <map> //#define TEST using namespace std; const int MAX = 1000000; typedef long long int lli; ///// HEAP -------------------------- template <class T, int N> struct Heap { T* data[N+1]; int count; Heap(){ count = 0; } void siftUp(int i) { for(; i>>1 >= 1 && *data[i] < *data[i>>1]; i>>=1) {swap(data[i], data[i>>1]); swap(data[i]->hix,data[i>>1]->hix);} } void siftDown(int i) { while(true){ if((i<<1)+1 <= count && *data[(i<<1)+1] < *data[i<<1] && *data[(i<<1)+1] < *data[i]) { swap(data[i], data[(i<<1)+1]); swap(data[i]->hix,data[(i<<1)+1]->hix); i = (i<<1)+1; } else if ((i<<1)<=count && *data[i<<1] < *data[i]) { swap(data[i], data[(i<<1)]); swap(data[i]->hix,data[(i<<1)]->hix); i = (i<<1); }else{ break; } } } void quickAdd(T* item){ data[++count] = item; item->hix = count; } void add(T* item){ data[++count] = item; item->hix = count; siftUp(item->hix); } T* top(){ return count == 0 ? NULL : data[1]; } T* pop(){ if(count != 0) { T* result = top(); if(count == 1) { count = 0; return result; } swap(data[1], data[count]); data[1]->hix = 1; count--; siftDown(1); return result; } else { return NULL; } } void buildHeap(){ for(int i = (count>>1); i >0; --i) siftDown(i); } bool empty() { return count == 0; } void deleteAt(int i){ swap(data[i], data[count]); data[i]->hix = i; count--; T* temp = data[i]; siftDown(temp->hix); siftUp(temp->hix); } void clear() { count = 0; } }; //////----------------------- struct Sum { lli val; int hix; bool operator< (const Sum h) const { return val < h.val;} }; char c; int n, m; lli money[MAX]; lli outcome[MAX]; lli cycle[MAX]; int cs; lli cycleDecrease; Sum sums[MAX*2]; Heap<Sum,MAX+1> sinkHeap; int sinks[MAX]; typedef map<lli, map<int, int> > M; M tree; int gcd; int nextPlayerDelta; lli minNumberOfMoves = LLONG_MAX; int computeGcd( int a, int b){ while(a!=b){ if (a < b) swap(a,b); a = a-b; } return a; } void computeCycle(int k){ #ifdef TEST printf("\nthe Val in Cycle\n"); #endif // TEST for(int i = 0; i < cs; ++i) { int theVal = (int)((k+(lli)i*n)%m); cycle[i] = outcome[theVal]; if (theVal == (k+gcd)%m ) nextPlayerDelta = i; #ifdef TEST printf("%2i ",theVal); #endif // TEST } #ifdef TEST printf("\n"); for(int i = 0; i < 2*cs; ++i ) printf("%2i ",cycle[i%cs]); printf("\nnextPlayerDelta=%i\n",nextPlayerDelta); #endif // TEST } void computeSums(){ #ifdef TEST printf("\nSUMS\n"); #endif // TEST lli runningSum = 0; for(int i = 0; i < 2*cs; ++i){ if ( i == cs) cycleDecrease = runningSum; runningSum += cycle[i%cs]; sums[i].val = runningSum; #ifdef TEST printf("%2i ",sums[i].val); #endif // TEST } } void computeSinks(){ #ifdef TEST printf("\nSINKS\n"); #endif // TEST sinkHeap.clear(); for(int i = 0; i < cs; ++i){ sinkHeap.quickAdd(sums+i); } sinkHeap.buildHeap(); lli runningSum = 0; for(int i = 0; i < cs; ++i){ sinks[i] = sinkHeap.top()->val - runningSum; runningSum += cycle[i]; sinkHeap.quickAdd(sums+cs+i); sinkHeap.deleteAt(sums[i].hix); #ifdef TEST printf("%2i ",sinks[i]); #endif // TEST } } void computeTree(){ tree.clear(); for(int i = 0; i < cs*2; ++i) { M::iterator location = tree.lower_bound(sums[i].val); if( location == tree.end() || location->first != sums[i].val) location = tree.insert(location, pair<lli,map<int,int> >(sums[i].val,map<int,int>())); location->second.insert(pair<int,int>(i,i)); } #ifdef TEST printf("\nTREE\n"); for(M::iterator it = tree.begin(); it != tree.end(); ++it ) printf("%2i ",it->first); for(M::iterator it = tree.begin(); it != tree.end(); ++it ) { printf("\n sum=%i||",it->first); for(map<int,int>::iterator itt = it->second.begin(); itt != it->second.end(); itt++) printf("%i ",itt->first); } #endif // TEST } lli computeNumberOfPlays(int player, int cycleStart){ if(cycleDecrease >= 0 && sinks[cycleStart]+money[player] > 0) return INT_MAX; lli fullCycles; if(cycleDecrease >= 0 || money[player]+sinks[cycleStart] <= 0) fullCycles = 0; else if((money[player]+sinks[cycleStart]) %-cycleDecrease == 0) fullCycles = (money[player]+sinks[cycleStart])/-cycleDecrease; else fullCycles = (money[player]+sinks[cycleStart])/-cycleDecrease+1; money[player] += fullCycles*cycleDecrease; lli startHeight = 0; if(cycleStart>0) startHeight = sums[cycleStart-1].val; lli searchFor = -(money[player]-startHeight); int ix = tree.find(searchFor)->second.upper_bound(cycleStart-1)->first; #ifdef TEST printf("\n fullCycles=%i startHeight=%i searchFor=%i ix=%i",fullCycles,startHeight,searchFor,ix); #endif // TEST return fullCycles*cs+(ix-cycleStart)+1; } int main(){ scanf("%i", &n); for(int i = 0; i < n; ++i) scanf("%lli", &money[i]); scanf("%i\n", &m); for(int i = 0; i < m; ++i){ scanf("%c", &c); outcome[i] = (c == 'W' ? 1 : -1); } gcd = computeGcd(m,n); cs = m/gcd; #ifdef TEST printf("n=%i m=%i gcd=%i, cs=%i\n",n,m,gcd,cs); #endif // TEST for(int k = 0; k < gcd; ++k){ // Wyznaczamy cykl znajdź tablicę z +1 i -1 oraz delte do następnego elemenu czyli k+gcd computeCycle(k); computeSums(); computeSinks(); computeTree(); for(int i = k, j = 0; i < n; i+= gcd, j=(j+nextPlayerDelta)%cs){ lli numberOfPlaysByThisPlayer = computeNumberOfPlays(i, j); if( numberOfPlaysByThisPlayer != INT_MAX ) { lli totalNumberOfPlaysNeeded = (lli)(numberOfPlaysByThisPlayer-1)*n+i+1; if (totalNumberOfPlaysNeeded < minNumberOfMoves){ minNumberOfMoves = totalNumberOfPlaysNeeded; } #ifdef TEST printf("\nPLAYER %i starts at %i needs %i which totals to %lli",i, j ,numberOfPlaysByThisPlayer,totalNumberOfPlaysNeeded); #endif // TEST } else { #ifdef TEST printf("\nPLAYER %i starts at %i is winning player",i, j ); #endif // TEST } } } #ifdef TEST printf("\n"); #endif // TEST printf("%lli\n",minNumberOfMoves == LLONG_MAX ? -1 : minNumberOfMoves); return 0; } /* EXAMPLE 4 3 2 3 4 9 WPPPPWPWP 4 2 3 2 1 3 WPP */
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <climits> #include <queue> #include <vector> #include <map> //#define TEST using namespace std; const int MAX = 1000000; typedef long long int lli; ///// HEAP -------------------------- template <class T, int N> struct Heap { T* data[N+1]; int count; Heap(){ count = 0; } void siftUp(int i) { for(; i>>1 >= 1 && *data[i] < *data[i>>1]; i>>=1) {swap(data[i], data[i>>1]); swap(data[i]->hix,data[i>>1]->hix);} } void siftDown(int i) { while(true){ if((i<<1)+1 <= count && *data[(i<<1)+1] < *data[i<<1] && *data[(i<<1)+1] < *data[i]) { swap(data[i], data[(i<<1)+1]); swap(data[i]->hix,data[(i<<1)+1]->hix); i = (i<<1)+1; } else if ((i<<1)<=count && *data[i<<1] < *data[i]) { swap(data[i], data[(i<<1)]); swap(data[i]->hix,data[(i<<1)]->hix); i = (i<<1); }else{ break; } } } void quickAdd(T* item){ data[++count] = item; item->hix = count; } void add(T* item){ data[++count] = item; item->hix = count; siftUp(item->hix); } T* top(){ return count == 0 ? NULL : data[1]; } T* pop(){ if(count != 0) { T* result = top(); if(count == 1) { count = 0; return result; } swap(data[1], data[count]); data[1]->hix = 1; count--; siftDown(1); return result; } else { return NULL; } } void buildHeap(){ for(int i = (count>>1); i >0; --i) siftDown(i); } bool empty() { return count == 0; } void deleteAt(int i){ swap(data[i], data[count]); data[i]->hix = i; count--; T* temp = data[i]; siftDown(temp->hix); siftUp(temp->hix); } void clear() { count = 0; } }; //////----------------------- struct Sum { lli val; int hix; bool operator< (const Sum h) const { return val < h.val;} }; char c; int n, m; lli money[MAX]; lli outcome[MAX]; lli cycle[MAX]; int cs; lli cycleDecrease; Sum sums[MAX*2]; Heap<Sum,MAX+1> sinkHeap; int sinks[MAX]; typedef map<lli, map<int, int> > M; M tree; int gcd; int nextPlayerDelta; lli minNumberOfMoves = LLONG_MAX; int computeGcd( int a, int b){ while(a!=b){ if (a < b) swap(a,b); a = a-b; } return a; } void computeCycle(int k){ #ifdef TEST printf("\nthe Val in Cycle\n"); #endif // TEST for(int i = 0; i < cs; ++i) { int theVal = (int)((k+(lli)i*n)%m); cycle[i] = outcome[theVal]; if (theVal == (k+gcd)%m ) nextPlayerDelta = i; #ifdef TEST printf("%2i ",theVal); #endif // TEST } #ifdef TEST printf("\n"); for(int i = 0; i < 2*cs; ++i ) printf("%2i ",cycle[i%cs]); printf("\nnextPlayerDelta=%i\n",nextPlayerDelta); #endif // TEST } void computeSums(){ #ifdef TEST printf("\nSUMS\n"); #endif // TEST lli runningSum = 0; for(int i = 0; i < 2*cs; ++i){ if ( i == cs) cycleDecrease = runningSum; runningSum += cycle[i%cs]; sums[i].val = runningSum; #ifdef TEST printf("%2i ",sums[i].val); #endif // TEST } } void computeSinks(){ #ifdef TEST printf("\nSINKS\n"); #endif // TEST sinkHeap.clear(); for(int i = 0; i < cs; ++i){ sinkHeap.quickAdd(sums+i); } sinkHeap.buildHeap(); lli runningSum = 0; for(int i = 0; i < cs; ++i){ sinks[i] = sinkHeap.top()->val - runningSum; runningSum += cycle[i]; sinkHeap.quickAdd(sums+cs+i); sinkHeap.deleteAt(sums[i].hix); #ifdef TEST printf("%2i ",sinks[i]); #endif // TEST } } void computeTree(){ tree.clear(); for(int i = 0; i < cs*2; ++i) { M::iterator location = tree.lower_bound(sums[i].val); if( location == tree.end() || location->first != sums[i].val) location = tree.insert(location, pair<lli,map<int,int> >(sums[i].val,map<int,int>())); location->second.insert(pair<int,int>(i,i)); } #ifdef TEST printf("\nTREE\n"); for(M::iterator it = tree.begin(); it != tree.end(); ++it ) printf("%2i ",it->first); for(M::iterator it = tree.begin(); it != tree.end(); ++it ) { printf("\n sum=%i||",it->first); for(map<int,int>::iterator itt = it->second.begin(); itt != it->second.end(); itt++) printf("%i ",itt->first); } #endif // TEST } lli computeNumberOfPlays(int player, int cycleStart){ if(cycleDecrease >= 0 && sinks[cycleStart]+money[player] > 0) return INT_MAX; lli fullCycles; if(cycleDecrease >= 0 || money[player]+sinks[cycleStart] <= 0) fullCycles = 0; else if((money[player]+sinks[cycleStart]) %-cycleDecrease == 0) fullCycles = (money[player]+sinks[cycleStart])/-cycleDecrease; else fullCycles = (money[player]+sinks[cycleStart])/-cycleDecrease+1; money[player] += fullCycles*cycleDecrease; lli startHeight = 0; if(cycleStart>0) startHeight = sums[cycleStart-1].val; lli searchFor = -(money[player]-startHeight); int ix = tree.find(searchFor)->second.upper_bound(cycleStart-1)->first; #ifdef TEST printf("\n fullCycles=%i startHeight=%i searchFor=%i ix=%i",fullCycles,startHeight,searchFor,ix); #endif // TEST return fullCycles*cs+(ix-cycleStart)+1; } int main(){ scanf("%i", &n); for(int i = 0; i < n; ++i) scanf("%lli", &money[i]); scanf("%i\n", &m); for(int i = 0; i < m; ++i){ scanf("%c", &c); outcome[i] = (c == 'W' ? 1 : -1); } gcd = computeGcd(m,n); cs = m/gcd; #ifdef TEST printf("n=%i m=%i gcd=%i, cs=%i\n",n,m,gcd,cs); #endif // TEST for(int k = 0; k < gcd; ++k){ // Wyznaczamy cykl znajdź tablicę z +1 i -1 oraz delte do następnego elemenu czyli k+gcd computeCycle(k); computeSums(); computeSinks(); computeTree(); for(int i = k, j = 0; i < n; i+= gcd, j=(j+nextPlayerDelta)%cs){ lli numberOfPlaysByThisPlayer = computeNumberOfPlays(i, j); if( numberOfPlaysByThisPlayer != INT_MAX ) { lli totalNumberOfPlaysNeeded = (lli)(numberOfPlaysByThisPlayer-1)*n+i+1; if (totalNumberOfPlaysNeeded < minNumberOfMoves){ minNumberOfMoves = totalNumberOfPlaysNeeded; } #ifdef TEST printf("\nPLAYER %i starts at %i needs %i which totals to %lli",i, j ,numberOfPlaysByThisPlayer,totalNumberOfPlaysNeeded); #endif // TEST } else { #ifdef TEST printf("\nPLAYER %i starts at %i is winning player",i, j ); #endif // TEST } } } #ifdef TEST printf("\n"); #endif // TEST printf("%lli\n",minNumberOfMoves == LLONG_MAX ? -1 : minNumberOfMoves); return 0; } /* EXAMPLE 4 3 2 3 4 9 WPPPPWPWP 4 2 3 2 1 3 WPP */ |