#include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, si> misi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 1000 * 1000 + 9; const ll ANSMAX = (ll) NMAX * NMAX; const ll RETMAX = (ll) ANSMAX * NMAX; char token; int n, m, d, cn, checked; int money[NMAX], game[NMAX], myCycle[NMAX], myPos[NMAX]; class Cycle{ public: vi vec, sum, mini, maxi; int id, suma, minSum, maxSum; misi reg; Cycle(int ci) { prd("Cycle %d\n", ci); id = ci; int pos = ci, i = 0; do { vec.pb(game[pos]); myCycle[pos] = ci; myPos[pos] = i++; pos = (pos + n) % m; } while(pos != ci); suma = 0, minSum = 0, maxSum = 0; d = vec.size(); sum.resize(d); mini.resize(d); maxi.resize(d); REP(i, d) { suma += vec[i]; minSum = min(minSum, suma); sum[i] = suma; mini[i] = minSum; reg[suma].insert(i); } suma = 0; FORD(i, d-1, 0) { suma += vec[i]; maxSum = max(maxSum, suma); maxi[i] = maxSum; } dbg REP(i, d) prd("%d: vec %d sum %d mini %d maxi %d\n", i, vec[i], sum[i], mini[i], maxi[i]); prd("\n"); } int myMin(int pos) { return (pos ? suma - sum[pos - 1] : suma) + min(-maxi[pos], (pos ? mini[pos - 1] : 0)); } int getOccur(int val, int pos) { misi::itr mit = reg.find(val); if(mit == reg.end()) return NMAX; si::itr sit = mit->yy.lower_bound(pos); if(sit == mit->yy.end()) return NMAX; return *sit; } int timeToOccur(int val, int pos) { // val < 0 //if(val >= 0) printf("SHIEEEEEEEET\n"); int val1 = val + (pos ? sum[pos - 1] : 0), val2 = val1 - suma; int occ = getOccur(val1, pos); int ret = (occ == NMAX ? (getOccur(val2, 0) + d) : occ) - pos + 1; prd("timeToOccur %d %d: %d\n", val, pos, ret); return ret; } ll howLong(int pos, int cash) { int minis = myMin(pos); prd("Cycle[%d].howLong %d %d, myMin = %d, ", id, pos, cash, minis); if(minis + cash <= 0) { prd("A\n"); return timeToOccur(-cash, pos); } else if(suma >= 0) { prd("B\n"); return ANSMAX; } else { prd("C\n"); int negSuma = -suma; // > 0 int turns = (minis + cash + negSuma - 1) / negSuma; cash -= turns * negSuma; ll tmp = (ll) turns * d; return tmp + timeToOccur(-cash, pos); } } }; int main() { scanf("%d", &n); REP(i, n) scanf("%d", &money[i]); scanf("%d ", &m); REP(i, m) { scanf("%c", &token); game[i] = token == 'W' ? 1 : - 1; } /*REP(i, m) printf("%d ", game[i]);*/ //step = n % m; vector<Cycle*> cycles; while(checked < m) { cycles.pb(new Cycle(cn)); checked += cycles[cn]->vec.size(); cn++; } ll out = RETMAX; REP(i, n) { ll tmp = cycles[myCycle[i % m]]->howLong(myPos[i % m], money[i]); prd("player %d needs %lld cycles\n", i, tmp); if(tmp == ANSMAX) continue; tmp = (tmp - 1) * n; prd("so his score is %lld\n\n", tmp + i + 1); out = min(out, tmp + i + 1); } printf("%lld", (out == RETMAX ? -1 : out)); 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 | #include<cstdio> #include<algorithm> #include<utility> #include<string> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<iostream> #define FORE(c, a, b) for(int c=a; c<= int(b); ++c) #define FORD(c, a, b) for(int c=a; c>= int(b); --c) #define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it) #define REP(i, n) for(int i = 0; i < (n); ++i) #define ALL(a) a.begin(), a.end() #define PR(n) printf("%d ", (int) (n)) #define PRL(n) printf("%lld ", (ll) (n)) #define SC(n) scanf("%d ", &n) #define xx first #define yy second #define pb push_back #define mp make_pair #define itr iterator #define dbg if(0) #define prd dbg printf using namespace std; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef vector<int> vi; typedef set<int> si; typedef map<int, si> misi; typedef pair<int, int> pi; typedef vector<pi> vii; typedef vector<vi> vvi; const int NMAX = 1000 * 1000 + 9; const ll ANSMAX = (ll) NMAX * NMAX; const ll RETMAX = (ll) ANSMAX * NMAX; char token; int n, m, d, cn, checked; int money[NMAX], game[NMAX], myCycle[NMAX], myPos[NMAX]; class Cycle{ public: vi vec, sum, mini, maxi; int id, suma, minSum, maxSum; misi reg; Cycle(int ci) { prd("Cycle %d\n", ci); id = ci; int pos = ci, i = 0; do { vec.pb(game[pos]); myCycle[pos] = ci; myPos[pos] = i++; pos = (pos + n) % m; } while(pos != ci); suma = 0, minSum = 0, maxSum = 0; d = vec.size(); sum.resize(d); mini.resize(d); maxi.resize(d); REP(i, d) { suma += vec[i]; minSum = min(minSum, suma); sum[i] = suma; mini[i] = minSum; reg[suma].insert(i); } suma = 0; FORD(i, d-1, 0) { suma += vec[i]; maxSum = max(maxSum, suma); maxi[i] = maxSum; } dbg REP(i, d) prd("%d: vec %d sum %d mini %d maxi %d\n", i, vec[i], sum[i], mini[i], maxi[i]); prd("\n"); } int myMin(int pos) { return (pos ? suma - sum[pos - 1] : suma) + min(-maxi[pos], (pos ? mini[pos - 1] : 0)); } int getOccur(int val, int pos) { misi::itr mit = reg.find(val); if(mit == reg.end()) return NMAX; si::itr sit = mit->yy.lower_bound(pos); if(sit == mit->yy.end()) return NMAX; return *sit; } int timeToOccur(int val, int pos) { // val < 0 //if(val >= 0) printf("SHIEEEEEEEET\n"); int val1 = val + (pos ? sum[pos - 1] : 0), val2 = val1 - suma; int occ = getOccur(val1, pos); int ret = (occ == NMAX ? (getOccur(val2, 0) + d) : occ) - pos + 1; prd("timeToOccur %d %d: %d\n", val, pos, ret); return ret; } ll howLong(int pos, int cash) { int minis = myMin(pos); prd("Cycle[%d].howLong %d %d, myMin = %d, ", id, pos, cash, minis); if(minis + cash <= 0) { prd("A\n"); return timeToOccur(-cash, pos); } else if(suma >= 0) { prd("B\n"); return ANSMAX; } else { prd("C\n"); int negSuma = -suma; // > 0 int turns = (minis + cash + negSuma - 1) / negSuma; cash -= turns * negSuma; ll tmp = (ll) turns * d; return tmp + timeToOccur(-cash, pos); } } }; int main() { scanf("%d", &n); REP(i, n) scanf("%d", &money[i]); scanf("%d ", &m); REP(i, m) { scanf("%c", &token); game[i] = token == 'W' ? 1 : - 1; } /*REP(i, m) printf("%d ", game[i]);*/ //step = n % m; vector<Cycle*> cycles; while(checked < m) { cycles.pb(new Cycle(cn)); checked += cycles[cn]->vec.size(); cn++; } ll out = RETMAX; REP(i, n) { ll tmp = cycles[myCycle[i % m]]->howLong(myPos[i % m], money[i]); prd("player %d needs %lld cycles\n", i, tmp); if(tmp == ANSMAX) continue; tmp = (tmp - 1) * n; prd("so his score is %lld\n\n", tmp + i + 1); out = min(out, tmp + i + 1); } printf("%lld", (out == RETMAX ? -1 : out)); return 0; } |