#include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> using namespace std; //#include<intrin.h> #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; #define MR 1000010 int t[MR]; LL res[MR]; char s[MR]; bool done[MR]; vector < int > S; int b, m; const int inf = 1e9; const int N = 2e6 + 10; // limit for array size int n; // array size int tree[2 * N]; int d[N]; int h; void apply(int p, int value, int *t) { t[p] += value; if (p < n) d[p] += value; } void build(int p, int *t) { while (p > 1) p >>= 1, t[p] = min(t[p<<1], t[p<<1|1]) + d[p]; } void push(int p, int *t) { for (int s = h; s > 0; --s) { int i = p >> s; if (d[i] != 0) { apply(i<<1, d[i], t); apply(i<<1|1, d[i], t); d[i] = 0; } } } void inc(int l, int r, int value, int *t) { l += n, r += n; int l0 = l, r0 = r; for (; l < r; l >>= 1, r >>= 1) { if (l&1) apply(l++, value, t); if (r&1) apply(--r, value, t); } build(l0, t); build(r0 - 1, t); } int query(int l, int r, int *t) { l += n, r += n; push(l, t); push(r - 1, t); int res = inf; for (; l < r; l >>= 1, r >>= 1) { if (l&1) res = min(res, t[l++]); if (r&1) res = min(t[--r], res); } return res; } int BSL(int p, int k, int v, int *t) { while(p+1 < k) { int w = (p+k)/2; if(query(p,w, t) <= v) k = w; else p = w; } return p; } int main() { scanf("%d", &b); REP(i,b) scanf("%d", &t[i]); scanf("%d%s", &m, s); REP(i,m) { if(!done[i]) { int j = i; while(true) { S.PB(j); done[j] = 1; j = (j+b)%m; if(done[j]) break; } // zbuduj drzewo int sum = 0; n = 2*S.size()-1; h = sizeof(int) * 8 - __builtin_clz(n); REP(i,n) { if(i < (int)S.size()) { sum += s[S[i]] == 'W' ? 1 : -1; *(tree + n + i) = sum; } else *(tree + n + i) = 0; build(n+i, tree); } int p = 0, k = S.size(); while(p < (int)S.size()) { int minv = query(p,k,tree); int c = S[p]; while(c < b) { // zobacz czy od razu nie da sie zalatwic if(-minv >= t[c]) { res[c] = BSL(p, k, -t[c], tree) + 1 - p; } else { // czy okrazajac cykl zejdziemy ponizej 0 if(sum < 0) { //assert(minv <= sum); // ile razy cykl musimy okrazyc - uwzgledniajac ze za jednym okrazeniem mozemy zbic max minv int ile = t[c] + minv; int ileC = ile/(-sum); if(ile%(-sum)) ileC++; //assert(ileC > 0); ile = t[c] + ileC*sum; //assert(ile >= 0 && ile <= -minv); res[c] += ileC*(LL)S.size(); res[c] += BSL(p, k, -ile, tree) + 1 - p; } else res[c] = -2; } c += m; } inc(p, k, s[S[p]] == 'W' ? -1 : 1, tree); inc(k, k+1, sum, tree); p++; k++; } REP(i,n) d[i] = 0; S.clear(); } } LL mn = inf*(LL)inf; REP(i,b) if(res[i] > 0) mn = min(mn, res[i]); if(mn == inf*(LL)inf) printf("-1\n"); else { LL finalRes = 0; LL initial = mn; REP(i,b) { finalRes += mn; if(res[i] == mn) { mn--; } } //assert(mn == initial-1); printf("%lld\n", finalRes); } 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 | #include <cstdio> #include <cstdlib> #include <iostream> #include <fstream> #include <sstream> #include <set> #include <map> #include <vector> #include <list> #include <algorithm> #include <cstring> #include <cmath> #include <string> #include <queue> #include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic #include <cassert> #include <iomanip> //do setprecision #include <ctime> #include <complex> using namespace std; //#include<intrin.h> #define FOR(i,b,e) for(int i=(b);i<(e);++i) #define FORQ(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ST first #define ND second #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned LL #define LD long double const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; #define MR 1000010 int t[MR]; LL res[MR]; char s[MR]; bool done[MR]; vector < int > S; int b, m; const int inf = 1e9; const int N = 2e6 + 10; // limit for array size int n; // array size int tree[2 * N]; int d[N]; int h; void apply(int p, int value, int *t) { t[p] += value; if (p < n) d[p] += value; } void build(int p, int *t) { while (p > 1) p >>= 1, t[p] = min(t[p<<1], t[p<<1|1]) + d[p]; } void push(int p, int *t) { for (int s = h; s > 0; --s) { int i = p >> s; if (d[i] != 0) { apply(i<<1, d[i], t); apply(i<<1|1, d[i], t); d[i] = 0; } } } void inc(int l, int r, int value, int *t) { l += n, r += n; int l0 = l, r0 = r; for (; l < r; l >>= 1, r >>= 1) { if (l&1) apply(l++, value, t); if (r&1) apply(--r, value, t); } build(l0, t); build(r0 - 1, t); } int query(int l, int r, int *t) { l += n, r += n; push(l, t); push(r - 1, t); int res = inf; for (; l < r; l >>= 1, r >>= 1) { if (l&1) res = min(res, t[l++]); if (r&1) res = min(t[--r], res); } return res; } int BSL(int p, int k, int v, int *t) { while(p+1 < k) { int w = (p+k)/2; if(query(p,w, t) <= v) k = w; else p = w; } return p; } int main() { scanf("%d", &b); REP(i,b) scanf("%d", &t[i]); scanf("%d%s", &m, s); REP(i,m) { if(!done[i]) { int j = i; while(true) { S.PB(j); done[j] = 1; j = (j+b)%m; if(done[j]) break; } // zbuduj drzewo int sum = 0; n = 2*S.size()-1; h = sizeof(int) * 8 - __builtin_clz(n); REP(i,n) { if(i < (int)S.size()) { sum += s[S[i]] == 'W' ? 1 : -1; *(tree + n + i) = sum; } else *(tree + n + i) = 0; build(n+i, tree); } int p = 0, k = S.size(); while(p < (int)S.size()) { int minv = query(p,k,tree); int c = S[p]; while(c < b) { // zobacz czy od razu nie da sie zalatwic if(-minv >= t[c]) { res[c] = BSL(p, k, -t[c], tree) + 1 - p; } else { // czy okrazajac cykl zejdziemy ponizej 0 if(sum < 0) { //assert(minv <= sum); // ile razy cykl musimy okrazyc - uwzgledniajac ze za jednym okrazeniem mozemy zbic max minv int ile = t[c] + minv; int ileC = ile/(-sum); if(ile%(-sum)) ileC++; //assert(ileC > 0); ile = t[c] + ileC*sum; //assert(ile >= 0 && ile <= -minv); res[c] += ileC*(LL)S.size(); res[c] += BSL(p, k, -ile, tree) + 1 - p; } else res[c] = -2; } c += m; } inc(p, k, s[S[p]] == 'W' ? -1 : 1, tree); inc(k, k+1, sum, tree); p++; k++; } REP(i,n) d[i] = 0; S.clear(); } } LL mn = inf*(LL)inf; REP(i,b) if(res[i] > 0) mn = min(mn, res[i]); if(mn == inf*(LL)inf) printf("-1\n"); else { LL finalRes = 0; LL initial = mn; REP(i,b) { finalRes += mn; if(res[i] == mn) { mn--; } } //assert(mn == initial-1); printf("%lld\n", finalRes); } return 0; } |