#include <cstdio> #include <algorithm> #include <deque> #include <cmath> using namespace std; typedef long long ll; #define pb push_back #define si size() #define lf(x) ((x)<<1) #define ri(x) (lf(x)+1) const int maxn=1000010; deque<int> L[maxn]; int T[maxn*3]; int a[maxn]; int b[maxn]; char s[maxn]; bool vis[maxn]; ll MIN(ll a, ll b){ if(b<0) return a; if(a<0) return b; return min(a, b); } int tSize(int n){ if(n&(n-1)){ frexp(n, &n); n=1<<n; } return n; } void addValue(int x, int v, int s){ if(T[x+s]) L[x].pb(v); else{ x+=s; T[x]=v; while(x>>=1) T[x]=max(T[lf(x)], T[ri(x)]); } } void eraseValue(int x, int s){ if(L[x].empty()) T[x+s]=0; else{ T[x+s]=L[x][0]; L[x].pop_front(); } x+=s; while(x>>=1) T[x]=max(T[lf(x)], T[ri(x)]); } int query(int a, int b){ int m=0; while(a<b){ if(a&1) m=max(m, T[a++]); if(!(b&1)) m=max(m, T[b--]); a>>=1; b>>=1; } if(a==b) m=max(m, T[a]); return m; } int findLast(int s){ if(!T[1]) return -1; int x=1; while(x<s){ x=ri(x); if(!T[x]) --x; } return x-s; } ll processPlayer(int j, int a, int n, int m, int s, int l, int vsi, int p, int i){ int r=query(a+s, l+s); if(r) return ll(vsi-r-i)*n+j+1; if(p<=0) return -1; r=findLast(s); if(r<0) return -1; r=(a-r)/p; a-=p*r; ll res=(ll)r*l*n; r=vsi-query(a+s, l+s); res+=ll(r-i)*n+j+1; return res; } ll processPeriod(int u, int n, int m){ deque<int> V; deque<int> P; while(!vis[u]){ V.pb(u); vis[u]=1; u=(u+n)%m; } int l=V.si; int s=tSize(l+1); for(int i=0;i<l;++i) V.pb(V[i]); P.pb(b[V[0]]); for(int i=1;i<V.si;++i) P.pb(P[i-1]+b[V[i]]); for(int i=0;i<l-1;++i) if(P[i]>=0) addValue(P[i], V.si-i, s); ll res=-1; for(int i=0;i<l;++i){ if(P[i+l-1]>=0) addValue(P[i+l-1], V.si-i-l+1, s); for(int j=V[i];j<n;j+=m) res=MIN(res, processPlayer(j, a[j]+((i>0)?P[i-1]:0), n, m, s, l, V.si, P[l-1], i)); if(P[i]>=0) eraseValue(P[i], s); } for(int i=l;i<V.si;++i) if(P[i]>=0) eraseValue(P[i], s); return res; } int main() { int n, m; scanf("%d", &n); for(int i=0;i<n;++i) scanf("%d", a+i); scanf("%d%s", &m, s); for(int i=0;i<m;++i) b[i]=(s[i]=='P')?1:-1; ll ans=-1; for(int i=0;i<m;++i) if(!vis[i]) ans=MIN(ans, processPeriod(i, n, m)); printf("%lld", ans); }
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 | #include <cstdio> #include <algorithm> #include <deque> #include <cmath> using namespace std; typedef long long ll; #define pb push_back #define si size() #define lf(x) ((x)<<1) #define ri(x) (lf(x)+1) const int maxn=1000010; deque<int> L[maxn]; int T[maxn*3]; int a[maxn]; int b[maxn]; char s[maxn]; bool vis[maxn]; ll MIN(ll a, ll b){ if(b<0) return a; if(a<0) return b; return min(a, b); } int tSize(int n){ if(n&(n-1)){ frexp(n, &n); n=1<<n; } return n; } void addValue(int x, int v, int s){ if(T[x+s]) L[x].pb(v); else{ x+=s; T[x]=v; while(x>>=1) T[x]=max(T[lf(x)], T[ri(x)]); } } void eraseValue(int x, int s){ if(L[x].empty()) T[x+s]=0; else{ T[x+s]=L[x][0]; L[x].pop_front(); } x+=s; while(x>>=1) T[x]=max(T[lf(x)], T[ri(x)]); } int query(int a, int b){ int m=0; while(a<b){ if(a&1) m=max(m, T[a++]); if(!(b&1)) m=max(m, T[b--]); a>>=1; b>>=1; } if(a==b) m=max(m, T[a]); return m; } int findLast(int s){ if(!T[1]) return -1; int x=1; while(x<s){ x=ri(x); if(!T[x]) --x; } return x-s; } ll processPlayer(int j, int a, int n, int m, int s, int l, int vsi, int p, int i){ int r=query(a+s, l+s); if(r) return ll(vsi-r-i)*n+j+1; if(p<=0) return -1; r=findLast(s); if(r<0) return -1; r=(a-r)/p; a-=p*r; ll res=(ll)r*l*n; r=vsi-query(a+s, l+s); res+=ll(r-i)*n+j+1; return res; } ll processPeriod(int u, int n, int m){ deque<int> V; deque<int> P; while(!vis[u]){ V.pb(u); vis[u]=1; u=(u+n)%m; } int l=V.si; int s=tSize(l+1); for(int i=0;i<l;++i) V.pb(V[i]); P.pb(b[V[0]]); for(int i=1;i<V.si;++i) P.pb(P[i-1]+b[V[i]]); for(int i=0;i<l-1;++i) if(P[i]>=0) addValue(P[i], V.si-i, s); ll res=-1; for(int i=0;i<l;++i){ if(P[i+l-1]>=0) addValue(P[i+l-1], V.si-i-l+1, s); for(int j=V[i];j<n;j+=m) res=MIN(res, processPlayer(j, a[j]+((i>0)?P[i-1]:0), n, m, s, l, V.si, P[l-1], i)); if(P[i]>=0) eraseValue(P[i], s); } for(int i=l;i<V.si;++i) if(P[i]>=0) eraseValue(P[i], s); return res; } int main() { int n, m; scanf("%d", &n); for(int i=0;i<n;++i) scanf("%d", a+i); scanf("%d%s", &m, s); for(int i=0;i<m;++i) b[i]=(s[i]=='P')?1:-1; ll ans=-1; for(int i=0;i<m;++i) if(!vis[i]) ans=MIN(ans, processPeriod(i, n, m)); printf("%lld", ans); } |