#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <limits> #include <unordered_map> using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> PII; #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 ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int) (x).size()) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define INF 1000000001 int n, m; VI S, C; struct Game { int seq_no; // id of cycle int id; // id in cycle int sb; // sum from begining int sum; int mn; // min from this game }; vector<Game> G; vector<unordered_map<int, vector<int>>> M; // ^ ^ ^ // | | |- positions // | |- game sum // |- # cycle void readin() { int a; cin >> n; REP(i, n) { cin >> a; S.PB(a); } string cs; cin >> m >> cs; REP(i, m) C.PB(cs[i] == 'P' ? -1 : 1); } int add(int i, int d) { return (i + d) % m; } int gcd(int a, int b) { while (b > 0) swap(a %= b, b); return a; } LL lcm(int a, int b) { return LL(a) * LL(b) / gcd(a, b); } void pre_process() { G.resize(m, {-1, -1, 0, 0, 0}); int seq_no = -1; REP(i, m) if (G[i].seq_no == -1) { M.PB({}); int k = 0; int s = 0; G[i].seq_no = ++seq_no; G[i].id = k; G[i].sb = s; M[seq_no][s].PB(k); s += C[i]; int mn = min(0, s); for (int j = add(i, n); j != i; j = add(j, n)) { G[j].seq_no = seq_no; G[j].id = ++k; G[j].sb = s; M[seq_no][s].PB(k); s += C[j]; mn = min(mn, s); } M[seq_no][s].PB(k); G[i].sum = s; G[i].mn = mn; int p = i; for (int j = add(i, n); j != i; j = add(j, n)) { mn -= C[p]; G[j].sum = s; G[j].mn = mn; p = j; } for (auto& e: M[seq_no]) sort(ALL(e.ND)); } } LL min_lost_turn(int i) { int p = i % m; if (S[i] + G[p].mn <= 0) return i; if (G[p].sum >= 0) return -1; LL s = -G[p].sum; LL l = (S[i] + G[p].mn + s - 1) / s; return l * lcm(n, m) + i; } LL find_lost_turn(LL beg, int i) { int k = S[i] + (beg / lcm(n, m)) * G[i % m].sum; if (k == 0 || k + C[beg % m] == 0) return beg; int p = beg % m; int s = k - G[p].sb; const auto& pos = M[G[p].seq_no][-s]; auto it = lower_bound(ALL(pos), G[p].id); if (it != pos.end()) { beg += (*it - G[p].id) * n; return beg; } beg += (m - G[p].id) * n; beg += pos[0] * n; return beg; } LL compute() { pre_process(); vector<pair<LL, int>> v; REP(i, n) { LL mn = min_lost_turn(i); if (mn >= 0) v.PB(MP(mn, i)); } if (v.empty()) return -1; sort(ALL(v)); LL ret = numeric_limits<LL>::max(); for (auto x : v) { if (x.ST >= ret) break; ret = min(ret, find_lost_turn(x.ST, x.ND)); } return ret + 1; } int main() { readin(); cout << compute() << "\n"; 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 | #include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <limits> #include <unordered_map> using namespace std; typedef vector<int> VI; typedef long long LL; typedef pair<int, int> PII; #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 ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int) (x).size()) #define FOREACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define INF 1000000001 int n, m; VI S, C; struct Game { int seq_no; // id of cycle int id; // id in cycle int sb; // sum from begining int sum; int mn; // min from this game }; vector<Game> G; vector<unordered_map<int, vector<int>>> M; // ^ ^ ^ // | | |- positions // | |- game sum // |- # cycle void readin() { int a; cin >> n; REP(i, n) { cin >> a; S.PB(a); } string cs; cin >> m >> cs; REP(i, m) C.PB(cs[i] == 'P' ? -1 : 1); } int add(int i, int d) { return (i + d) % m; } int gcd(int a, int b) { while (b > 0) swap(a %= b, b); return a; } LL lcm(int a, int b) { return LL(a) * LL(b) / gcd(a, b); } void pre_process() { G.resize(m, {-1, -1, 0, 0, 0}); int seq_no = -1; REP(i, m) if (G[i].seq_no == -1) { M.PB({}); int k = 0; int s = 0; G[i].seq_no = ++seq_no; G[i].id = k; G[i].sb = s; M[seq_no][s].PB(k); s += C[i]; int mn = min(0, s); for (int j = add(i, n); j != i; j = add(j, n)) { G[j].seq_no = seq_no; G[j].id = ++k; G[j].sb = s; M[seq_no][s].PB(k); s += C[j]; mn = min(mn, s); } M[seq_no][s].PB(k); G[i].sum = s; G[i].mn = mn; int p = i; for (int j = add(i, n); j != i; j = add(j, n)) { mn -= C[p]; G[j].sum = s; G[j].mn = mn; p = j; } for (auto& e: M[seq_no]) sort(ALL(e.ND)); } } LL min_lost_turn(int i) { int p = i % m; if (S[i] + G[p].mn <= 0) return i; if (G[p].sum >= 0) return -1; LL s = -G[p].sum; LL l = (S[i] + G[p].mn + s - 1) / s; return l * lcm(n, m) + i; } LL find_lost_turn(LL beg, int i) { int k = S[i] + (beg / lcm(n, m)) * G[i % m].sum; if (k == 0 || k + C[beg % m] == 0) return beg; int p = beg % m; int s = k - G[p].sb; const auto& pos = M[G[p].seq_no][-s]; auto it = lower_bound(ALL(pos), G[p].id); if (it != pos.end()) { beg += (*it - G[p].id) * n; return beg; } beg += (m - G[p].id) * n; beg += pos[0] * n; return beg; } LL compute() { pre_process(); vector<pair<LL, int>> v; REP(i, n) { LL mn = min_lost_turn(i); if (mn >= 0) v.PB(MP(mn, i)); } if (v.empty()) return -1; sort(ALL(v)); LL ret = numeric_limits<LL>::max(); for (auto x : v) { if (x.ST >= ret) break; ret = min(ret, find_lost_turn(x.ST, x.ND)); } return ret + 1; } int main() { readin(); cout << compute() << "\n"; return 0; } |