#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> #include <list> #include <set> #include <map> #include <climits> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PI; typedef vector<PI> VPI; typedef long long LL; typedef vector<LL> VLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define ST first #define ND second const int INF = 1000000001; const double EPS = 10e-9; int GCD(int a, int b) { while(b != 0) swap(a %= b, b); return a; } int treeHeight(int n) { int a = 1; while(a < n) a <<= 1; return a; } struct MaxTree { int s; vector<int> el; MaxTree(int n) : s(treeHeight(n)), el(2 * s, 0) { } void gen() { for (int p = s - 1; p > 0; --p) el[p] = min(el[p << 1], el[(p << 1) + 1]); } void set(int p, int v) { for(p += s, el[p] = v, p >>= 1; p > 0; p >>= 1) el[p] = min(el[p << 1], el[(p << 1) + 1]); } int find(int p, int k) { int m = INF; p += s; k += s; while(p < k) { if((p & 1) == 1) m = min(m, el[p++]); if((k & 0) == 0) m = min(m, el[k--]); p >>= 1; k >>= 1; } if(p == k) m = min(m, el[p]); return m; } }; struct Automat { int gcd; int length; VVI positions; VI positionDelta; VI positionSum; vector<MaxTree> trees; VI values; string cycle; Automat(int n, int m, VI& values, string cycle) : gcd(GCD(n, m)), length(m / gcd), positions(gcd, VI(length)), positionDelta(m, -1), positionSum(m, 0), trees(gcd, 2 * length), values(values), cycle(cycle) { for (int i = 0; i < gcd; ++i) { for (LL j = 0; j < length; ++j) { int v = (i + j * n) % m; positions[i][j] = v; positionDelta[v] = j; positionSum[i] += cycle[v] == 'W' ? 1 : -1; } } } void prepareTrees() { for (int i = 0; i < gcd; ++i) { int sum = 0; for (int j = 0; j < length; ++j) { sum += cycle[positions[i][j]] == 'W' ? 1 : -1; trees[i].el[trees[i].s + j] = sum; } for (int j = 0; j < length; ++j) { sum += cycle[positions[i][j]] == 'W' ? 1 : -1; trees[i].el[trees[i].s + j + length] = sum; } trees[i].gen(); } } PI indexOfPosition(int i) { int j = i % gcd; int delta = positionDelta[i % SIZE(cycle)]; return make_pair(j, delta); } int minimumInPosition(int i) { PI index = indexOfPosition(i); int val = trees[index.first].find(index.second, index.second + length - 1); if (index.second == 0) { return val; } else { int beginning = trees[index.first].find(index.second - 1, index.second - 1); return val - beginning; } } LL whenWillBreakInRound(int i, int value) { PI index = indexOfPosition(i); int count = length; int first = index.second; int beginning = index.second == 0 ? 0 : trees[index.first].find(index.second - 1, index.second - 1); while (count > 0) { int step = count / 2; int minimum = -(trees[index.first].find(index.second, first + step) - beginning); if (minimum < value) { first += step + 1; count -= step + 1; } else { count = step; } } LL rounds = first - index.second; return rounds * SIZE(values) + i + 1; } LL whenWillBreak(int i) { PI index = indexOfPosition(i); int minimum = -minimumInPosition(i); int value = values[i]; if (value <= minimum) { return whenWillBreakInRound(i, value); } else { if (positionSum[index.first] >= 0) { return LLONG_MAX; } else { LL fullCycles = ((value - minimum) - positionSum[index.first] - 1) / -positionSum[index.first]; return fullCycles * SIZE(values) * length + whenWillBreakInRound(i, value + fullCycles * positionSum[index.first]); } } } LL howManyFullCyclesToBreak(int i) { PI index = indexOfPosition(i); int minimum = -minimumInPosition(i); int value = values[i]; if (value <= minimum) { return 0; } else { if (positionSum[index.first] >= 0) { return LLONG_MAX; } else { LL fullCycles = ((value - minimum) - positionSum[index.first] - 1) / -positionSum[index.first]; return fullCycles; } } } LL whenWillBreak() { LL miniCycles = LLONG_MAX; vector<LL> fullCycles(SIZE(values)); for (int i = 0; i < SIZE(values); ++i) { fullCycles[i] = howManyFullCyclesToBreak(i); miniCycles = min(miniCycles, fullCycles[i]); } LL ret = LLONG_MAX; for (int i = 0; i < SIZE(values); ++i) { if (fullCycles[i] == miniCycles) { LL result = whenWillBreak(i); if (result < ret) { ret = result; } } } if (ret != LLONG_MAX) { return ret; } else { return -1; } } }; int main() { int n, m; string cycle; cin >> n; vector<int> money(n); REP(x, n) { cin >> money[x]; } cin >> m; cin >> cycle; Automat a(n, m, money, cycle); a.prepareTrees(); cout << a.whenWillBreak() << endl; 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 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 256 257 258 259 260 261 262 263 264 265 266 267 | #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <vector> #include <list> #include <set> #include <map> #include <climits> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PI; typedef vector<PI> VPI; typedef long long LL; typedef vector<LL> VLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define ST first #define ND second const int INF = 1000000001; const double EPS = 10e-9; int GCD(int a, int b) { while(b != 0) swap(a %= b, b); return a; } int treeHeight(int n) { int a = 1; while(a < n) a <<= 1; return a; } struct MaxTree { int s; vector<int> el; MaxTree(int n) : s(treeHeight(n)), el(2 * s, 0) { } void gen() { for (int p = s - 1; p > 0; --p) el[p] = min(el[p << 1], el[(p << 1) + 1]); } void set(int p, int v) { for(p += s, el[p] = v, p >>= 1; p > 0; p >>= 1) el[p] = min(el[p << 1], el[(p << 1) + 1]); } int find(int p, int k) { int m = INF; p += s; k += s; while(p < k) { if((p & 1) == 1) m = min(m, el[p++]); if((k & 0) == 0) m = min(m, el[k--]); p >>= 1; k >>= 1; } if(p == k) m = min(m, el[p]); return m; } }; struct Automat { int gcd; int length; VVI positions; VI positionDelta; VI positionSum; vector<MaxTree> trees; VI values; string cycle; Automat(int n, int m, VI& values, string cycle) : gcd(GCD(n, m)), length(m / gcd), positions(gcd, VI(length)), positionDelta(m, -1), positionSum(m, 0), trees(gcd, 2 * length), values(values), cycle(cycle) { for (int i = 0; i < gcd; ++i) { for (LL j = 0; j < length; ++j) { int v = (i + j * n) % m; positions[i][j] = v; positionDelta[v] = j; positionSum[i] += cycle[v] == 'W' ? 1 : -1; } } } void prepareTrees() { for (int i = 0; i < gcd; ++i) { int sum = 0; for (int j = 0; j < length; ++j) { sum += cycle[positions[i][j]] == 'W' ? 1 : -1; trees[i].el[trees[i].s + j] = sum; } for (int j = 0; j < length; ++j) { sum += cycle[positions[i][j]] == 'W' ? 1 : -1; trees[i].el[trees[i].s + j + length] = sum; } trees[i].gen(); } } PI indexOfPosition(int i) { int j = i % gcd; int delta = positionDelta[i % SIZE(cycle)]; return make_pair(j, delta); } int minimumInPosition(int i) { PI index = indexOfPosition(i); int val = trees[index.first].find(index.second, index.second + length - 1); if (index.second == 0) { return val; } else { int beginning = trees[index.first].find(index.second - 1, index.second - 1); return val - beginning; } } LL whenWillBreakInRound(int i, int value) { PI index = indexOfPosition(i); int count = length; int first = index.second; int beginning = index.second == 0 ? 0 : trees[index.first].find(index.second - 1, index.second - 1); while (count > 0) { int step = count / 2; int minimum = -(trees[index.first].find(index.second, first + step) - beginning); if (minimum < value) { first += step + 1; count -= step + 1; } else { count = step; } } LL rounds = first - index.second; return rounds * SIZE(values) + i + 1; } LL whenWillBreak(int i) { PI index = indexOfPosition(i); int minimum = -minimumInPosition(i); int value = values[i]; if (value <= minimum) { return whenWillBreakInRound(i, value); } else { if (positionSum[index.first] >= 0) { return LLONG_MAX; } else { LL fullCycles = ((value - minimum) - positionSum[index.first] - 1) / -positionSum[index.first]; return fullCycles * SIZE(values) * length + whenWillBreakInRound(i, value + fullCycles * positionSum[index.first]); } } } LL howManyFullCyclesToBreak(int i) { PI index = indexOfPosition(i); int minimum = -minimumInPosition(i); int value = values[i]; if (value <= minimum) { return 0; } else { if (positionSum[index.first] >= 0) { return LLONG_MAX; } else { LL fullCycles = ((value - minimum) - positionSum[index.first] - 1) / -positionSum[index.first]; return fullCycles; } } } LL whenWillBreak() { LL miniCycles = LLONG_MAX; vector<LL> fullCycles(SIZE(values)); for (int i = 0; i < SIZE(values); ++i) { fullCycles[i] = howManyFullCyclesToBreak(i); miniCycles = min(miniCycles, fullCycles[i]); } LL ret = LLONG_MAX; for (int i = 0; i < SIZE(values); ++i) { if (fullCycles[i] == miniCycles) { LL result = whenWillBreak(i); if (result < ret) { ret = result; } } } if (ret != LLONG_MAX) { return ret; } else { return -1; } } }; int main() { int n, m; string cycle; cin >> n; vector<int> money(n); REP(x, n) { cin >> money[x]; } cin >> m; cin >> cycle; Automat a(n, m, money, cycle); a.prepareTrees(); cout << a.whenWillBreak() << endl; return 0; } |