#include <iostream> #include <vector> #include <cassert> #include <map> #include <algorithm> #include <limits.h> using namespace std; #define REP(a,n) for(int a=0;a<n;++a) #define REP1(a,n) for(int a=1;a<=n;++a) #define P(a) cout<<#a<<": "<<(a)<<endl #define LL long long #define assert(a) 1; #define P(a) 1; const int MAXN=1000*1000; int n,m; int mon[MAXN]; char d[MAXN+1]; bool visited[MAXN]; //bool added[MAXN]; //map<int,int> cycle; vector<int> pref; vector<int> prefMaxL; vector<int> prefMaxR; map<int,vector<int> > wys; /* * |lllllllrrrrrrrrrr| <- pref sum array * ^ * j */ int ceil(int a,int b){ return (a+b-1)/b; } LL check(int k,int j){ P(k); P(j); // j is index in cycle int l=0; if(j>0)l=pref[j-1]; int r=*pref.rbegin() - l; P(l); P(r); //int curMoney=0; // maximum in cycle int maxv=prefMaxR[j]-l; if(j>0)maxv=max(maxv,prefMaxL[j-1]+r); P(maxv); LL ret=0; if(maxv<k){ if(*pref.rbegin()<=0)return -1; int cyclesCount=ceil(k-maxv,*pref.rbegin()); P(cyclesCount); ret+=((LL)cyclesCount)*pref.size(); k-=cyclesCount*(*pref.rbegin()); } P(ret); P(k); if(k==0)return ret; P(k+l); vector<int>& vec=wys[k+l]; for(auto a:vec){ //cout<<a<<" "; } //cout<<endl; vector<int>::iterator iter=lower_bound(vec.begin(),vec.end(),j); P(iter==vec.end()); if(iter==vec.end()){ vector<int>& vec2=wys[k-r]; assert(!vec2.empty()); assert(vec2[0]<j); ret+=vec2[0]+1+r; }else{ ret+=(*iter)-j+1; } return ret; } int main() { //ios_base::sync_with_stdio(0); cin>>n; REP(a,n){ cin>>mon[a]; } cin>>m; cin>>d; int len=__gcd(n,m); int siz=m/len; LL ret=LLONG_MAX; P(len); P(siz); REP(a,len){ pref.clear(); prefMaxL.clear(); prefMaxR.clear(); //cycle.clear(); wys.clear(); int i=a; while(!visited[i]){ //cycle[i]=pref.size(); pref.push_back((d[i]=='W')?-1:1); visited[i]=1; i=(i+n)%m; } // making prefix sums //cout<<pref[0]<<" "; for(i=1;i<pref.size();++i){ pref[i]+=pref[i-1]; //cout<<pref[i]<<" "; } //cout<<endl; // making wys REP(a,pref.size()){ wys[pref[a]].push_back(a); } // making prefix max from left prefMaxL.push_back(pref[0]); for(i=1;i<pref.size();++i){ prefMaxL.push_back(max(pref[i-1],pref[i])); } // from right prefMaxR.push_back(pref[pref.size()-1]); for(i=pref.size()-2;i>=0;--i){ prefMaxR.push_back(max(pref[i+1],pref[i])); } assert(siz==pref.size()); assert(siz==prefMaxL.size()); assert(siz==prefMaxR.size()); for(i=a;i<n;i+=len){ LL checkRet=check(mon[i],(i/len)&m); P(checkRet); ret=min(ret,(LL)i+(LL)(checkRet-1)*n +1); if(ret==-1){ cout<<-1; return 0; } } } cout<<ret; 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 | #include <iostream> #include <vector> #include <cassert> #include <map> #include <algorithm> #include <limits.h> using namespace std; #define REP(a,n) for(int a=0;a<n;++a) #define REP1(a,n) for(int a=1;a<=n;++a) #define P(a) cout<<#a<<": "<<(a)<<endl #define LL long long #define assert(a) 1; #define P(a) 1; const int MAXN=1000*1000; int n,m; int mon[MAXN]; char d[MAXN+1]; bool visited[MAXN]; //bool added[MAXN]; //map<int,int> cycle; vector<int> pref; vector<int> prefMaxL; vector<int> prefMaxR; map<int,vector<int> > wys; /* * |lllllllrrrrrrrrrr| <- pref sum array * ^ * j */ int ceil(int a,int b){ return (a+b-1)/b; } LL check(int k,int j){ P(k); P(j); // j is index in cycle int l=0; if(j>0)l=pref[j-1]; int r=*pref.rbegin() - l; P(l); P(r); //int curMoney=0; // maximum in cycle int maxv=prefMaxR[j]-l; if(j>0)maxv=max(maxv,prefMaxL[j-1]+r); P(maxv); LL ret=0; if(maxv<k){ if(*pref.rbegin()<=0)return -1; int cyclesCount=ceil(k-maxv,*pref.rbegin()); P(cyclesCount); ret+=((LL)cyclesCount)*pref.size(); k-=cyclesCount*(*pref.rbegin()); } P(ret); P(k); if(k==0)return ret; P(k+l); vector<int>& vec=wys[k+l]; for(auto a:vec){ //cout<<a<<" "; } //cout<<endl; vector<int>::iterator iter=lower_bound(vec.begin(),vec.end(),j); P(iter==vec.end()); if(iter==vec.end()){ vector<int>& vec2=wys[k-r]; assert(!vec2.empty()); assert(vec2[0]<j); ret+=vec2[0]+1+r; }else{ ret+=(*iter)-j+1; } return ret; } int main() { //ios_base::sync_with_stdio(0); cin>>n; REP(a,n){ cin>>mon[a]; } cin>>m; cin>>d; int len=__gcd(n,m); int siz=m/len; LL ret=LLONG_MAX; P(len); P(siz); REP(a,len){ pref.clear(); prefMaxL.clear(); prefMaxR.clear(); //cycle.clear(); wys.clear(); int i=a; while(!visited[i]){ //cycle[i]=pref.size(); pref.push_back((d[i]=='W')?-1:1); visited[i]=1; i=(i+n)%m; } // making prefix sums //cout<<pref[0]<<" "; for(i=1;i<pref.size();++i){ pref[i]+=pref[i-1]; //cout<<pref[i]<<" "; } //cout<<endl; // making wys REP(a,pref.size()){ wys[pref[a]].push_back(a); } // making prefix max from left prefMaxL.push_back(pref[0]); for(i=1;i<pref.size();++i){ prefMaxL.push_back(max(pref[i-1],pref[i])); } // from right prefMaxR.push_back(pref[pref.size()-1]); for(i=pref.size()-2;i>=0;--i){ prefMaxR.push_back(max(pref[i+1],pref[i])); } assert(siz==pref.size()); assert(siz==prefMaxL.size()); assert(siz==prefMaxR.size()); for(i=a;i<n;i+=len){ LL checkRet=check(mon[i],(i/len)&m); P(checkRet); ret=min(ret,(LL)i+(LL)(checkRet-1)*n +1); if(ret==-1){ cout<<-1; return 0; } } } cout<<ret; return 0; } |