#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> #include <list> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// // struktura sledzaca minimum w przesuwajacym sie oknie template<class T> class WindowMin { deque<T> t ; public: void insert(T a) { while(!t.empty() && t.back() > a) t.pop_back() ; t.PB(a) ; } void remove(T a) { if(t.front()==a) t.pop_front() ; } T getMin() { return t.front() ; } void clear() { t.clear() ; } } ; ////////////////////////////////////////// int ceil(int a, int b) { // assert(a>=0 && b>0) ; return (a+b-1)/b ; } const int MAXN = 1000100 ; int h[MAXN] ; short autom[MAXN] ; bool used[MAXN] ; typedef queue<int, list<int> > QUEUE ; QUEUE __where[4*MAXN+1] ; QUEUE *where = &__where[2*MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, m, i, j ; char c ; cin >> n ; REP(i, n) cin >> h[i] ; cin >> m ; REP(i, m) { cin >> c ; if(c=='W') autom[i] = 1 ; else if(c=='P') autom[i] = -1 ; // else assert(false) ; } const int move = n%m ; LL ret=INFLL ; VI cycle, s ; WindowMin<int> window ; REP(i, m) { if(used[i]) continue ; cycle.clear() ; s.clear() ; window.clear() ; j=i ; do { cycle.PB(j) ; used[j]=true ; j = (j+move)%m ; } while(!used[j]) ; const int len = cycle.size() ; for(j=-2*len ; j<=2*len ; j++) // czyszczenie where[j] = QUEUE() ; int sum=0 ; s.PB(0) ; REP(j, len) { sum += autom[cycle[j]] ; s.PB(sum) ; where[sum].push(j+1) ; window.insert(sum) ; } for(j=1 ; j<=len ; j++) { const int a = cycle[j-1] ; for(int b=a ; b<n ; b+=m) { int H=h[b] ; const int _min = window.getMin()-s[j-1] ; if(sum>=0 && H+_min>0) continue ; LL act_ret=b+1 ; // tyle ruchow trzeba by do niego dojsc i dostac pierwsze losowanie if(sum<0 && H+_min>0) { LL k = ceil(H+_min, -sum) ; // tyle pelnych cyklow mozemy uzyc act_ret += k*len*n ; H += k*sum ; } // assert(H+_min<=0) ; // assert(!where[-H+s[j-1]].empty()) ; const int l = where[-H+s[j-1]].front() ; // assert(l>=j) ; // assert(l-j<len) ; act_ret += (LL)(l-j)*n ; ret = min(ret, act_ret) ; } window.remove(s[j]) ; // assert(!where[s[j]].empty() && where[s[j]].front()==j) ; where[s[j]].pop() ; window.insert(sum+s[j]) ; where[sum+s[j]].push(len+j) ; } } if(ret==INFLL) cout << -1 << endl ; else cout << ret << endl ; }
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 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <ext/numeric> #include <list> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF * (LL)INF ; #define REP(i,n) for(i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } /////////////////////////////////////////// // struktura sledzaca minimum w przesuwajacym sie oknie template<class T> class WindowMin { deque<T> t ; public: void insert(T a) { while(!t.empty() && t.back() > a) t.pop_back() ; t.PB(a) ; } void remove(T a) { if(t.front()==a) t.pop_front() ; } T getMin() { return t.front() ; } void clear() { t.clear() ; } } ; ////////////////////////////////////////// int ceil(int a, int b) { // assert(a>=0 && b>0) ; return (a+b-1)/b ; } const int MAXN = 1000100 ; int h[MAXN] ; short autom[MAXN] ; bool used[MAXN] ; typedef queue<int, list<int> > QUEUE ; QUEUE __where[4*MAXN+1] ; QUEUE *where = &__where[2*MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, m, i, j ; char c ; cin >> n ; REP(i, n) cin >> h[i] ; cin >> m ; REP(i, m) { cin >> c ; if(c=='W') autom[i] = 1 ; else if(c=='P') autom[i] = -1 ; // else assert(false) ; } const int move = n%m ; LL ret=INFLL ; VI cycle, s ; WindowMin<int> window ; REP(i, m) { if(used[i]) continue ; cycle.clear() ; s.clear() ; window.clear() ; j=i ; do { cycle.PB(j) ; used[j]=true ; j = (j+move)%m ; } while(!used[j]) ; const int len = cycle.size() ; for(j=-2*len ; j<=2*len ; j++) // czyszczenie where[j] = QUEUE() ; int sum=0 ; s.PB(0) ; REP(j, len) { sum += autom[cycle[j]] ; s.PB(sum) ; where[sum].push(j+1) ; window.insert(sum) ; } for(j=1 ; j<=len ; j++) { const int a = cycle[j-1] ; for(int b=a ; b<n ; b+=m) { int H=h[b] ; const int _min = window.getMin()-s[j-1] ; if(sum>=0 && H+_min>0) continue ; LL act_ret=b+1 ; // tyle ruchow trzeba by do niego dojsc i dostac pierwsze losowanie if(sum<0 && H+_min>0) { LL k = ceil(H+_min, -sum) ; // tyle pelnych cyklow mozemy uzyc act_ret += k*len*n ; H += k*sum ; } // assert(H+_min<=0) ; // assert(!where[-H+s[j-1]].empty()) ; const int l = where[-H+s[j-1]].front() ; // assert(l>=j) ; // assert(l-j<len) ; act_ret += (LL)(l-j)*n ; ret = min(ret, act_ret) ; } window.remove(s[j]) ; // assert(!where[s[j]].empty() && where[s[j]].front()==j) ; where[s[j]].pop() ; window.insert(sum+s[j]) ; where[sum+s[j]].push(len+j) ; } } if(ret==INFLL) cout << -1 << endl ; else cout << ret << endl ; } |