//Uch... //Please, work... #include <cstdio> #include <vector> // #define DEBUG #define ERRSTREAM stderr using namespace std; const int MAXN=1000005; const long long INF=1000LL*1000*1000*1000*1000*1000 + 1234; const int INT_INF = 1000*1000*1000; const int OFFSET=3000000; typedef long long LL; LL N, M; int savings[MAXN] = {}; int machine[MAXN] = {}; bool done[MAXN]={}; int memory[2*OFFSET]={}; int maxH, minH; LL record = INF; void resetMemory() { minH=1; maxH=-1; } void saveWhenWillBeHeight(int height, int step) { memory[height+OFFSET]=step; maxH=max(maxH, height); minH=min(minH, height); } int getWhenWillBeHeight(int height) { if (!(minH <= height && height <= maxH)) return INT_INF; return memory[height+OFFSET]; } void updateRecord(long long answer) { if (record > answer) record = answer; } int step; int cycLen; int cycSum; int cycMin; int height; void processBoyUsingCurCycleData(int boyInd) { done[boyInd]=true; long long answer; int when; when = getWhenWillBeHeight(height-savings[boyInd]); #ifdef DEBUG fprintf(ERRSTREAM, "######################################\n"); fprintf(ERRSTREAM, "height=%d\n", height); fprintf(ERRSTREAM, "savings[%d]=%d\n", boyInd, savings[boyInd]); fprintf(ERRSTREAM, "step=%d\n", step); #endif if (when != INT_INF) { answer = 1 + boyInd + (LL)(when-step-1)*N; #ifdef DEBUG fprintf(ERRSTREAM, "when=%d\n", when); fprintf(ERRSTREAM, "Boy %d loses in first round\n", boyInd); fprintf(ERRSTREAM, "answer = %lld\n", answer); #endif updateRecord(answer); return; } if (cycSum >= 0) { //Will never bankrupt return; } int cycles, rest; //Dirty 5am fix: int metaRest = height-minH; int metaCycles = (savings[boyInd]-metaRest)/(-cycSum); if (metaCycles * (-cycSum) == savings[boyInd]-metaRest) { cycles = metaCycles; rest = metaRest; } else { cycles = metaCycles+1; rest = savings[boyInd] + cycles*cycSum; } // int cycles = savings[boyInd]/(-cycSum); // int rest = savings[boyInd] + cycles*cycSum; if (rest==0) { --cycles; rest=(-cycSum); } when = getWhenWillBeHeight(height-rest); answer = 1 + boyInd + (LL)(when-step-1)*N + cycles*(LL)(cycLen)*N; #ifdef DEBUG fprintf(ERRSTREAM, "when=%d\n", when); fprintf(ERRSTREAM, "Boy %d loses in %d cycle.\n", boyInd, cycles+1); fprintf(ERRSTREAM, "cycles=%d\n", cycles); fprintf(ERRSTREAM, "rest=%d\n", rest); fprintf(ERRSTREAM, "cycLen=%d\n", cycLen); fprintf(ERRSTREAM, "cycSum=%d\n", cycSum); fprintf(ERRSTREAM, "answer = %lld\n", answer); #endif updateRecord(answer); } void processCycle(int boyInd) { if (done[boyInd] == true) return; resetMemory(); long long curInd = boyInd+N*M; step=0; cycLen=0; cycSum=0; cycMin=0; height=0; #ifdef DEBUG fprintf(ERRSTREAM, "save: curInd%%M=%lld, height=%d, step=%d\n", curInd%M, height, step); #endif saveWhenWillBeHeight(height, step); //First pass, gathering data: do { ++cycLen; cycSum = cycSum + machine[curInd%M]; curInd-=N; height -= machine[curInd%M]; --step; #ifdef DEBUG fprintf(ERRSTREAM, "save: curInd%%M=%lld, height=%d, step=%d\n", curInd%M, height, step); #endif saveWhenWillBeHeight(height, step); } while (curInd%M != boyInd%M); curInd=boyInd+N*M; do { for (int i=curInd%M; i<N; i+=M) processBoyUsingCurCycleData(i%N); curInd-=N; height -= machine[curInd%M]; --step; #ifdef DEBUG fprintf(ERRSTREAM, "save: curInd%%M=%lld, height=%d, step=%d\n", curInd%M, height, step); #endif saveWhenWillBeHeight(height, step); } while (curInd%M != boyInd%M); } int main() { scanf("%lld ", &N); for (int i=0; i<N; ++i) { scanf("%d ", &savings[i]); } scanf("%lld ", &M); char c; for (int i=0; i<M; ++i) { scanf("%c", &c); if (c=='W') machine[i]=1; else if (c=='P') machine[i]=-1; else { fprintf(ERRSTREAM, "Error while reading: unrecognized character with code: %d\n", (int)(c)); return 1; } } for (int i=0; i<N; ++i) { processCycle(i); } if (record == INF) printf("-1\n"); else printf("%lld\n", record); 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 | //Uch... //Please, work... #include <cstdio> #include <vector> // #define DEBUG #define ERRSTREAM stderr using namespace std; const int MAXN=1000005; const long long INF=1000LL*1000*1000*1000*1000*1000 + 1234; const int INT_INF = 1000*1000*1000; const int OFFSET=3000000; typedef long long LL; LL N, M; int savings[MAXN] = {}; int machine[MAXN] = {}; bool done[MAXN]={}; int memory[2*OFFSET]={}; int maxH, minH; LL record = INF; void resetMemory() { minH=1; maxH=-1; } void saveWhenWillBeHeight(int height, int step) { memory[height+OFFSET]=step; maxH=max(maxH, height); minH=min(minH, height); } int getWhenWillBeHeight(int height) { if (!(minH <= height && height <= maxH)) return INT_INF; return memory[height+OFFSET]; } void updateRecord(long long answer) { if (record > answer) record = answer; } int step; int cycLen; int cycSum; int cycMin; int height; void processBoyUsingCurCycleData(int boyInd) { done[boyInd]=true; long long answer; int when; when = getWhenWillBeHeight(height-savings[boyInd]); #ifdef DEBUG fprintf(ERRSTREAM, "######################################\n"); fprintf(ERRSTREAM, "height=%d\n", height); fprintf(ERRSTREAM, "savings[%d]=%d\n", boyInd, savings[boyInd]); fprintf(ERRSTREAM, "step=%d\n", step); #endif if (when != INT_INF) { answer = 1 + boyInd + (LL)(when-step-1)*N; #ifdef DEBUG fprintf(ERRSTREAM, "when=%d\n", when); fprintf(ERRSTREAM, "Boy %d loses in first round\n", boyInd); fprintf(ERRSTREAM, "answer = %lld\n", answer); #endif updateRecord(answer); return; } if (cycSum >= 0) { //Will never bankrupt return; } int cycles, rest; //Dirty 5am fix: int metaRest = height-minH; int metaCycles = (savings[boyInd]-metaRest)/(-cycSum); if (metaCycles * (-cycSum) == savings[boyInd]-metaRest) { cycles = metaCycles; rest = metaRest; } else { cycles = metaCycles+1; rest = savings[boyInd] + cycles*cycSum; } // int cycles = savings[boyInd]/(-cycSum); // int rest = savings[boyInd] + cycles*cycSum; if (rest==0) { --cycles; rest=(-cycSum); } when = getWhenWillBeHeight(height-rest); answer = 1 + boyInd + (LL)(when-step-1)*N + cycles*(LL)(cycLen)*N; #ifdef DEBUG fprintf(ERRSTREAM, "when=%d\n", when); fprintf(ERRSTREAM, "Boy %d loses in %d cycle.\n", boyInd, cycles+1); fprintf(ERRSTREAM, "cycles=%d\n", cycles); fprintf(ERRSTREAM, "rest=%d\n", rest); fprintf(ERRSTREAM, "cycLen=%d\n", cycLen); fprintf(ERRSTREAM, "cycSum=%d\n", cycSum); fprintf(ERRSTREAM, "answer = %lld\n", answer); #endif updateRecord(answer); } void processCycle(int boyInd) { if (done[boyInd] == true) return; resetMemory(); long long curInd = boyInd+N*M; step=0; cycLen=0; cycSum=0; cycMin=0; height=0; #ifdef DEBUG fprintf(ERRSTREAM, "save: curInd%%M=%lld, height=%d, step=%d\n", curInd%M, height, step); #endif saveWhenWillBeHeight(height, step); //First pass, gathering data: do { ++cycLen; cycSum = cycSum + machine[curInd%M]; curInd-=N; height -= machine[curInd%M]; --step; #ifdef DEBUG fprintf(ERRSTREAM, "save: curInd%%M=%lld, height=%d, step=%d\n", curInd%M, height, step); #endif saveWhenWillBeHeight(height, step); } while (curInd%M != boyInd%M); curInd=boyInd+N*M; do { for (int i=curInd%M; i<N; i+=M) processBoyUsingCurCycleData(i%N); curInd-=N; height -= machine[curInd%M]; --step; #ifdef DEBUG fprintf(ERRSTREAM, "save: curInd%%M=%lld, height=%d, step=%d\n", curInd%M, height, step); #endif saveWhenWillBeHeight(height, step); } while (curInd%M != boyInd%M); } int main() { scanf("%lld ", &N); for (int i=0; i<N; ++i) { scanf("%d ", &savings[i]); } scanf("%lld ", &M); char c; for (int i=0; i<M; ++i) { scanf("%c", &c); if (c=='W') machine[i]=1; else if (c=='P') machine[i]=-1; else { fprintf(ERRSTREAM, "Error while reading: unrecognized character with code: %d\n", (int)(c)); return 1; } } for (int i=0; i<N; ++i) { processCycle(i); } if (record == INF) printf("-1\n"); else printf("%lld\n", record); return 0; } |