#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #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 mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; int n,m; const int MXN=1e6+6; int in[MXN]; char inn[MXN]; int ID=1; int idincycle[MXN]; int nrincycle[MXN]; int idtree=1; namespace tree { const int BASE=1<<21; int t[BASE*2]; void init() { REP(i,BASE*2)t[i]=1e9; } void lazyinsert(int x,int v) { t[x+BASE]=v; } void calc() { FORD(i,BASE-1,1) t[i]=min(t[i*2], t[i*2+1]); } void insert(int x,int v) { x+=BASE; while(x) { mini(t[x],v); x/=2; } } int query(int a,int b) { a+=BASE-1; b+=BASE+1; int r=1e9; while((a>>1)!=(b>>1)) { if((a&1)==0)mini(r,t[a+1]); if(b&1)mini(r,t[b-1]); a>>=1; b>>=1; } return r; } int query2(int p,int x) { p+=BASE; if(t[p]<=x)return 0; int pocp=p; while(p) { if((p&1)==0 && t[p+1]<=x) { p++; break; } p>>=1; } while(p<BASE) { if(t[p<<1]<=x)p<<=1; else p=(p<<1)+1; } return p-pocp; } } struct cycle { int sum; int siz; int poctree; int firstel; int query(int a,int b) { return tree::query(a+poctree,b+poctree); } int query2(int a,int v) { return tree::query2(a+poctree,v); } } t[MXN]; void readint(int &a) { char ch=0; a=0; while(ch<33)ch=getchar_unlocked(); do { a=(a<<1)+(a<<3)+ch-'0'; ch=getchar_unlocked(); } while(ch>33); } main() { tree::init(); scanf("%d",&n); REP(i,n) { readint(in[i]); } scanf("%d",&m); scanf("%s",inn); REP(i,m) { if(idincycle[i])continue; int sum=0; int nr=0; int x=i; do { idincycle[x]=ID; nrincycle[x]=nr++; sum+=(inn[x]=='W')*2-1; x=(x+n)%m; } while(x!=i); t[ID].sum=sum; t[ID].siz=nr; t[ID].poctree=idtree; t[ID].firstel=i; idtree+=t[ID].siz*2; ID++; } ID--; //cerr<<"CYKLI: "<<ID<<endl; FOR(i,1,ID) { int poc=t[i].poctree; int w=t[i].firstel; int sum=0; REP(u,t[i].siz*2) { sum+=(inn[w]=='W')*2-1; tree::lazyinsert(poc+u,sum); w+=n; w%=m; } } tree::calc(); LL res=1e18; res++; REP(XX,n) { int i=XX%m; int razy=0; LL value=in[XX]; int id=idincycle[i]; int warini=t[id].query(nrincycle[i],nrincycle[i])-((inn[i]=='W')*2-1); value-=warini; // cerr<<XX<<" "<<i<<" v: "<<value<<" "<<warini<<" "<<t[id].sum<<endl; value+=t[id].query(nrincycle[i],nrincycle[i]+t[id].siz-1); // cerr<<"po: "<<value<<endl; if(value<=0)razy=0; else { if(t[id].sum>=0)continue; razy=value/(-t[id].sum)+(value%(-t[id].sum)>0); } if((LL)razy*n*t[id].siz-n>res)continue; value=in[XX]+razy*t[id].sum; // cerr<<XX<<" uu "<<value<<endl; int iledalej=0; iledalej=t[id].query2(nrincycle[i],warini-value)+1; LL resnow=(LL)razy*n*t[id].siz+(LL)n*(iledalej)-n+XX+1; // cerr<<XX<<" razy: "<<razy<<" iledalej: "<<iledalej<<" res i :"<<resnow<<endl; mini(res,resnow); } if(res>(LL)1e18) { puts("-1"); return 0; } printf("%lld\n",res); }
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 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #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 mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; int n,m; const int MXN=1e6+6; int in[MXN]; char inn[MXN]; int ID=1; int idincycle[MXN]; int nrincycle[MXN]; int idtree=1; namespace tree { const int BASE=1<<21; int t[BASE*2]; void init() { REP(i,BASE*2)t[i]=1e9; } void lazyinsert(int x,int v) { t[x+BASE]=v; } void calc() { FORD(i,BASE-1,1) t[i]=min(t[i*2], t[i*2+1]); } void insert(int x,int v) { x+=BASE; while(x) { mini(t[x],v); x/=2; } } int query(int a,int b) { a+=BASE-1; b+=BASE+1; int r=1e9; while((a>>1)!=(b>>1)) { if((a&1)==0)mini(r,t[a+1]); if(b&1)mini(r,t[b-1]); a>>=1; b>>=1; } return r; } int query2(int p,int x) { p+=BASE; if(t[p]<=x)return 0; int pocp=p; while(p) { if((p&1)==0 && t[p+1]<=x) { p++; break; } p>>=1; } while(p<BASE) { if(t[p<<1]<=x)p<<=1; else p=(p<<1)+1; } return p-pocp; } } struct cycle { int sum; int siz; int poctree; int firstel; int query(int a,int b) { return tree::query(a+poctree,b+poctree); } int query2(int a,int v) { return tree::query2(a+poctree,v); } } t[MXN]; void readint(int &a) { char ch=0; a=0; while(ch<33)ch=getchar_unlocked(); do { a=(a<<1)+(a<<3)+ch-'0'; ch=getchar_unlocked(); } while(ch>33); } main() { tree::init(); scanf("%d",&n); REP(i,n) { readint(in[i]); } scanf("%d",&m); scanf("%s",inn); REP(i,m) { if(idincycle[i])continue; int sum=0; int nr=0; int x=i; do { idincycle[x]=ID; nrincycle[x]=nr++; sum+=(inn[x]=='W')*2-1; x=(x+n)%m; } while(x!=i); t[ID].sum=sum; t[ID].siz=nr; t[ID].poctree=idtree; t[ID].firstel=i; idtree+=t[ID].siz*2; ID++; } ID--; //cerr<<"CYKLI: "<<ID<<endl; FOR(i,1,ID) { int poc=t[i].poctree; int w=t[i].firstel; int sum=0; REP(u,t[i].siz*2) { sum+=(inn[w]=='W')*2-1; tree::lazyinsert(poc+u,sum); w+=n; w%=m; } } tree::calc(); LL res=1e18; res++; REP(XX,n) { int i=XX%m; int razy=0; LL value=in[XX]; int id=idincycle[i]; int warini=t[id].query(nrincycle[i],nrincycle[i])-((inn[i]=='W')*2-1); value-=warini; // cerr<<XX<<" "<<i<<" v: "<<value<<" "<<warini<<" "<<t[id].sum<<endl; value+=t[id].query(nrincycle[i],nrincycle[i]+t[id].siz-1); // cerr<<"po: "<<value<<endl; if(value<=0)razy=0; else { if(t[id].sum>=0)continue; razy=value/(-t[id].sum)+(value%(-t[id].sum)>0); } if((LL)razy*n*t[id].siz-n>res)continue; value=in[XX]+razy*t[id].sum; // cerr<<XX<<" uu "<<value<<endl; int iledalej=0; iledalej=t[id].query2(nrincycle[i],warini-value)+1; LL resnow=(LL)razy*n*t[id].siz+(LL)n*(iledalej)-n+XX+1; // cerr<<XX<<" razy: "<<razy<<" iledalej: "<<iledalej<<" res i :"<<resnow<<endl; mini(res,resnow); } if(res>(LL)1e18) { puts("-1"); return 0; } printf("%lld\n",res); } |