#include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MX = 1000010, OFFSET = 1000010; int n, saving[MX], m, automat[MX], jumpLength, j, ct; int cycleSum, closest[MX], minCycleSum; int entrySum, entryClosest[OFFSET + MX], entryCt, g; int guy, gamesInLastCycle[MX], howManyFullCycles[MX], entryOffset[MX]; int cycleSize; bool markedInCycle[MX], winner[MX]; char s[MX]; vector<int> cycle; void initNewCycle() { for (int i = 0; i <= cycle.size(); ++i) closest[i] = 0; int beg = -(int)cycle.size(); int end = cycle.size(); for (int i = beg; i <= end; ++i) { entryClosest[OFFSET + i] = 0; } ct = 1; minCycleSum = 0; cycleSum = 0; cycle.clear(); entrySum = 0; entryCt = 2; g = 0; } int main() { scanf ("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &saving[i]); scanf("%d", &m); scanf("%s", s); for (int i = 0; i < m; ++i) s[i] == 'W' ? automat[i] = 1 : automat[i] = -1; // for (int i = 0; i < m; ++i) // printf("%d", automat[i]); jumpLength = n % m; for (int i = 0; i < m; ++i) { j = i; initNewCycle(); while (!markedInCycle[j]) { markedInCycle[j] = true; cycle.push_back(j); cycleSum += automat[j]; if (cycleSum < 0 && !closest[-cycleSum]) closest[-cycleSum] = ct; minCycleSum = min(minCycleSum, cycleSum); j += jumpLength; j %= m; ct++; } // printf("------------------Nowy Cykl--------------------\n"); // printf("Cykl: "); // for (int q = 0; q < cycle.size(); ++q) // printf("%d ", cycle[q]); entryClosest[OFFSET] = 1; for (int k = (int)cycle.size() - 1; k >= 0; --k) { cycleSize = cycle.size(); entrySum += automat[cycle[k]]; g -= automat[cycle[k]]; entryClosest[OFFSET + g] = entryCt; // printf("\nJestem %d na cyklu\n", k); // printf("Koszt dojścia do cyklu: %d\n", entrySum); // printf("Najbliższe ujemne:\n"); // for (int q = -100; q < 100; ++q) { // if (entryClosest[OFFSET + q + g] != 0) // printf("Będę miał: %d, po %d grach\n", q, entryCt - entryClosest[OFFSET + q + g]); // } // printf("\n"); // for every guy who starts in cycle[k] guy = cycle[k]; while (guy < n) { // printf("Analizuję gościa %d\n", guy); // If I won't manage to enter the cycle if (entryClosest[OFFSET - saving[guy] + g] != 0) { gamesInLastCycle[guy] = entryCt - entryClosest[OFFSET - saving[guy] + g]; // printf("Nie uda mi się dojść do cyklu. Polegnę po: %d grach\n", gamesInLastCycle[guy]); guy += m; continue; } entryOffset[guy] = entryCt - 1; // printf("Aby dojsc do cyklu zagralem: %d gier\n", entryOffset[guy]); saving[guy] += entrySum; // printf("Zaczynając cykl będę miał: %d oszczędności, min cycle: %d\n", saving[guy], minCycleSum); if (saving[guy] <= -minCycleSum) { gamesInLastCycle[guy] = closest[saving[guy]]; // printf("Przegram odrazu\n"); } else if (cycleSum < 0) { howManyFullCycles[guy] = (saving[guy] -1 + minCycleSum) / -cycleSum; // printf("Mam %d oszczędności, stracę najwięcej: %d, suma cyklu: %d\n", saving[guy], minCycleSum, cycleSum); // printf("Zrobię: %d pełnych obrotów\n", howManyFullCycles[guy]); int savingBeforeLastCycle = saving[guy] + howManyFullCycles[guy]*cycleSum; gamesInLastCycle[guy] = closest[savingBeforeLastCycle]; } else { winner[guy] = true; } guy += m; } entryCt++; } } long long minGames = 1000000000000000000; int whichPlayedLeast = -1; long long howManyGamesPlayed = 0; for (int i = 0; i < n; ++i) { if (winner[i]) continue; // printf("Gosciu: %d", i); // printf("pełnych gier: %d, entryOffset: %d, games in last cycle %d\n", howManyFullCycles[i], entryOffset[i], gamesInLastCycle[i]); howManyGamesPlayed = (long long)howManyFullCycles[i] * cycleSize + entryOffset[i] + gamesInLastCycle[i]; if (howManyGamesPlayed < minGames) { // printf("howManyGamesPlayed: %lld", howManyGamesPlayed); minGames = howManyGamesPlayed; whichPlayedLeast = i; } } if (whichPlayedLeast == -1) printf("-1\n"); else printf("%lld\n", (minGames - 1)*n + whichPlayedLeast + 1); }
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 | #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int MX = 1000010, OFFSET = 1000010; int n, saving[MX], m, automat[MX], jumpLength, j, ct; int cycleSum, closest[MX], minCycleSum; int entrySum, entryClosest[OFFSET + MX], entryCt, g; int guy, gamesInLastCycle[MX], howManyFullCycles[MX], entryOffset[MX]; int cycleSize; bool markedInCycle[MX], winner[MX]; char s[MX]; vector<int> cycle; void initNewCycle() { for (int i = 0; i <= cycle.size(); ++i) closest[i] = 0; int beg = -(int)cycle.size(); int end = cycle.size(); for (int i = beg; i <= end; ++i) { entryClosest[OFFSET + i] = 0; } ct = 1; minCycleSum = 0; cycleSum = 0; cycle.clear(); entrySum = 0; entryCt = 2; g = 0; } int main() { scanf ("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &saving[i]); scanf("%d", &m); scanf("%s", s); for (int i = 0; i < m; ++i) s[i] == 'W' ? automat[i] = 1 : automat[i] = -1; // for (int i = 0; i < m; ++i) // printf("%d", automat[i]); jumpLength = n % m; for (int i = 0; i < m; ++i) { j = i; initNewCycle(); while (!markedInCycle[j]) { markedInCycle[j] = true; cycle.push_back(j); cycleSum += automat[j]; if (cycleSum < 0 && !closest[-cycleSum]) closest[-cycleSum] = ct; minCycleSum = min(minCycleSum, cycleSum); j += jumpLength; j %= m; ct++; } // printf("------------------Nowy Cykl--------------------\n"); // printf("Cykl: "); // for (int q = 0; q < cycle.size(); ++q) // printf("%d ", cycle[q]); entryClosest[OFFSET] = 1; for (int k = (int)cycle.size() - 1; k >= 0; --k) { cycleSize = cycle.size(); entrySum += automat[cycle[k]]; g -= automat[cycle[k]]; entryClosest[OFFSET + g] = entryCt; // printf("\nJestem %d na cyklu\n", k); // printf("Koszt dojścia do cyklu: %d\n", entrySum); // printf("Najbliższe ujemne:\n"); // for (int q = -100; q < 100; ++q) { // if (entryClosest[OFFSET + q + g] != 0) // printf("Będę miał: %d, po %d grach\n", q, entryCt - entryClosest[OFFSET + q + g]); // } // printf("\n"); // for every guy who starts in cycle[k] guy = cycle[k]; while (guy < n) { // printf("Analizuję gościa %d\n", guy); // If I won't manage to enter the cycle if (entryClosest[OFFSET - saving[guy] + g] != 0) { gamesInLastCycle[guy] = entryCt - entryClosest[OFFSET - saving[guy] + g]; // printf("Nie uda mi się dojść do cyklu. Polegnę po: %d grach\n", gamesInLastCycle[guy]); guy += m; continue; } entryOffset[guy] = entryCt - 1; // printf("Aby dojsc do cyklu zagralem: %d gier\n", entryOffset[guy]); saving[guy] += entrySum; // printf("Zaczynając cykl będę miał: %d oszczędności, min cycle: %d\n", saving[guy], minCycleSum); if (saving[guy] <= -minCycleSum) { gamesInLastCycle[guy] = closest[saving[guy]]; // printf("Przegram odrazu\n"); } else if (cycleSum < 0) { howManyFullCycles[guy] = (saving[guy] -1 + minCycleSum) / -cycleSum; // printf("Mam %d oszczędności, stracę najwięcej: %d, suma cyklu: %d\n", saving[guy], minCycleSum, cycleSum); // printf("Zrobię: %d pełnych obrotów\n", howManyFullCycles[guy]); int savingBeforeLastCycle = saving[guy] + howManyFullCycles[guy]*cycleSum; gamesInLastCycle[guy] = closest[savingBeforeLastCycle]; } else { winner[guy] = true; } guy += m; } entryCt++; } } long long minGames = 1000000000000000000; int whichPlayedLeast = -1; long long howManyGamesPlayed = 0; for (int i = 0; i < n; ++i) { if (winner[i]) continue; // printf("Gosciu: %d", i); // printf("pełnych gier: %d, entryOffset: %d, games in last cycle %d\n", howManyFullCycles[i], entryOffset[i], gamesInLastCycle[i]); howManyGamesPlayed = (long long)howManyFullCycles[i] * cycleSize + entryOffset[i] + gamesInLastCycle[i]; if (howManyGamesPlayed < minGames) { // printf("howManyGamesPlayed: %lld", howManyGamesPlayed); minGames = howManyGamesPlayed; whichPlayedLeast = i; } } if (whichPlayedLeast == -1) printf("-1\n"); else printf("%lld\n", (minGames - 1)*n + whichPlayedLeast + 1); } |