#include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define DEBUG(x) cout << #x ": " << x << endl #define IDEBUG(i, x) FWD(__i,0,i) cout << "\t"; cout << #x ": " << x << endl #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &state){ out << "(" << state.st << ", " << state.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int64 INF = SQ(1000LL * 1000 * 1000); const int start = 2000020; int gcd(int a, int b){ while(a && b) a >= b ? a %= b : b %= a; return a | b; } int ceil(int a, int b){ return (a + b-1) / b; } int n, m, g; int C[1000010]; char W[1000010]; int weight(int i){ return W[i] == 'P' ? -1 : 1; } int when[2*start]; vector<int> who[1000010]; int main(){ scanf("%d", &n); FWD(i,0,n) scanf("%d", &C[i]); scanf("%d", &m); scanf("%s", W); int64 res = INF; { int s = 0; FWD(i,0,n){ who[s].push_back(i); ++s; if(s == m) s = 0; } } g = gcd(n, m); int d = m / g; int bn = (m - n % m) % m; FWD(i,0,g){ // rozwazamy rozgrywki na stanach maszyny == i % g int state = i; int val = start; int minval = start + 4; FWD(v,start-2*d-8,start+2*d+8) when[v] = 0; FWD(ts,1,d+1){ state += bn; if(state >= m) state -= m; val -= weight(state); minval = min(minval, val); when[val] = ts; //DEBUG(val); } int whole = start - val; FWD(ts,d+1,2*d+1){ state += bn; if(state >= m) state -= m; val -= weight(state); minval = min(minval, val); when[val] = ts; //DEBUG(val); for(int player : who[state]){ //DEBUG(player); // przegrana w ts ruchach if(val >= C[player] && when[val - C[player]] != 0){ //DEBUG(val - C[player]); int64 games = ts - when[val - C[player]]; //DEBUG(games); int64 cres = player + (games - 1) * n + 1; res = min(res, cres); }else{ // przegrana po kilku cyklach if(whole < 0){ int blost = val - minval; // ile cykli musi minac zanim spadniemy <= blost int c = ceil(C[player]-blost, -whole); //DEBUG(C[player]); //DEBUG(c); C[player] += c * whole; //DEBUG(C[player]); int s = ts - when[val - C[player]]; int64 games = ((int64)c * d + s); //DEBUG(games); int64 cres = player + (games - 1) * n + 1; res = min(res, cres); } } } } } if(res == INF) printf("-1\n"); else printf("%lld\n", res); 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 | #include <bits/stdc++.h> #define REP(a,b) for(int a=0; a<(b); ++a) #define FWD(a,b,c) for(int a=(b); a<(c); ++a) #define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d) #define BCK(a,b,c) for(int a=(b); a>(c); --a) #define ALL(a) (a).begin(), (a).end() #define SIZE(a) ((int)(a).size()) #define SQ(a) ((a)*(a)) #define VAR(x) #x ": " << x << " " #define DEBUG(x) cout << #x ": " << x << endl #define IDEBUG(i, x) FWD(__i,0,i) cout << "\t"; cout << #x ": " << x << endl #define popcount __builtin_popcount #define popcountll __builtin_popcountll #define gcd __gcd #define x first #define y second #define st first #define nd second #define pb push_back using namespace std; template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; } template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &state){ out << "(" << state.st << ", " << state.nd << ")"; return out; } typedef long long int64; typedef pair<int, int> PII; typedef pair<int64, int64> PLL; typedef long double K; typedef vector<int> VI; const int dx[] = {0,0,-1,1}; //1,1,-1,1}; const int dy[] = {-1,1,0,0}; //1,-1,1,-1}; const int64 INF = SQ(1000LL * 1000 * 1000); const int start = 2000020; int gcd(int a, int b){ while(a && b) a >= b ? a %= b : b %= a; return a | b; } int ceil(int a, int b){ return (a + b-1) / b; } int n, m, g; int C[1000010]; char W[1000010]; int weight(int i){ return W[i] == 'P' ? -1 : 1; } int when[2*start]; vector<int> who[1000010]; int main(){ scanf("%d", &n); FWD(i,0,n) scanf("%d", &C[i]); scanf("%d", &m); scanf("%s", W); int64 res = INF; { int s = 0; FWD(i,0,n){ who[s].push_back(i); ++s; if(s == m) s = 0; } } g = gcd(n, m); int d = m / g; int bn = (m - n % m) % m; FWD(i,0,g){ // rozwazamy rozgrywki na stanach maszyny == i % g int state = i; int val = start; int minval = start + 4; FWD(v,start-2*d-8,start+2*d+8) when[v] = 0; FWD(ts,1,d+1){ state += bn; if(state >= m) state -= m; val -= weight(state); minval = min(minval, val); when[val] = ts; //DEBUG(val); } int whole = start - val; FWD(ts,d+1,2*d+1){ state += bn; if(state >= m) state -= m; val -= weight(state); minval = min(minval, val); when[val] = ts; //DEBUG(val); for(int player : who[state]){ //DEBUG(player); // przegrana w ts ruchach if(val >= C[player] && when[val - C[player]] != 0){ //DEBUG(val - C[player]); int64 games = ts - when[val - C[player]]; //DEBUG(games); int64 cres = player + (games - 1) * n + 1; res = min(res, cres); }else{ // przegrana po kilku cyklach if(whole < 0){ int blost = val - minval; // ile cykli musi minac zanim spadniemy <= blost int c = ceil(C[player]-blost, -whole); //DEBUG(C[player]); //DEBUG(c); C[player] += c * whole; //DEBUG(C[player]); int s = ts - when[val - C[player]]; int64 games = ((int64)c * d + s); //DEBUG(games); int64 cres = player + (games - 1) * n + 1; res = min(res, cres); } } } } } if(res == INF) printf("-1\n"); else printf("%lld\n", res); return 0; } |