#include <cstdio> #include <vector> using namespace std; #define LLI long long int struct Tree { int N, sT; vector<int> D; int _get_sum(int to) { if (to == -1) return 0; return D[to + N - 1]; } int sum(){ return _get_sum(sT - 1); } int get_min(int posL, int k) { int orig_posL = posL; posL += N-1; int posR = posL + k - 1; int result = min(D[posL], D[posR]); while (posL != posR) { if ((posL-1)/2 != (posR-1)/2){ if (posL%2 == 1) result = min(result, D[posL+1]); if (posR%2 == 0) result = min(result, D[posR-1]); } posL = (posL-1)/2; posR = (posR-1)/2; } if (orig_posL == 0) return result; return result - _get_sum(orig_posL - 1); } int find_first_get_min(int posL, int L, int val) { val = val + _get_sum(posL - 1); int pos = posL + N - 1; int last_val = D[pos]; if (D[pos] <= val) return 1; while ((pos%2==0) || (D[pos+1]>val)) pos = (pos-1)/2; pos += 1; if (D[pos] > val) printf("ERROR 1!\n"); while (pos < N-1) pos = (D[2*pos+1] > val ? 2*pos+2 : 2*pos+1); return pos - posL + 1 - N + 1; } Tree(vector<char> &T){ N = 1; sT = T.size(); while(N < 2*T.size()) N=N*2; D.resize(N*2-1, 0); for(int i=0; i<T.size(); ++i) D[N-1+i] = T[i]; for(int i=0; i<T.size(); ++i) D[N-1+i+T.size()] = T[i]; for(int i=N; i<D.size(); ++i) D[i]+=D[i-1]; for(int i=N-2; i>=0; --i) D[i] = min(D[2*i+1], D[2*i+2]); } }; // **************************************************************** int n, m; vector<int> A; char data[1000005]; int S, L; vector<Tree> cycles; vector<int> pos_in_cycle; LLI get_time_after_child_loose(int k) { LLI result = k + 1; Tree &T = cycles[k%S]; int personal_min = T.get_min(pos_in_cycle[k], L); int global_sum = T.sum(); if ((global_sum >= 0) && (A[k] + personal_min > 0)) return -1; if ((global_sum < 0) && (A[k] + personal_min > 0)) { int full_cycles = (A[k] + personal_min) / (-global_sum); A[k] -= full_cycles * (-global_sum); if (A[k] + personal_min > 0) { full_cycles += 1; A[k] += global_sum; } LLI addon = full_cycles; addon = addon * (LLI)(L); addon = addon * (LLI)(n); result += addon; } LLI addon = T.find_first_get_min(pos_in_cycle[k], L, -A[k]) - 1; addon = addon * (LLI)(n); result += addon; return result; } // ************************************************************* int gcd(int a, int b){int c; while (b!=0){c = a%b; a=b; b=c; } return a; } int main() { // Wczytywanie danych scanf("%d", &n); A.resize(n); for(int i=0; i<n; ++i) scanf("%d", &A[i]); scanf("%d", &m); scanf("%s", data); // Obliczanie cykli i pozycji w cyklach S = gcd(n, m); L = m/S; pos_in_cycle.resize(n); vector<int> R(m); for(int i=0; i<S; ++i) { int val = i; vector<char> cycle(L); for(int j=0; j<L; ++j) { cycle[j] = (data[val] == 'W' ? 1 : -1); R[val] = j; val = (val+n)%m; } cycles.push_back(Tree(cycle)); } for(int i=0; i<n; ++i) pos_in_cycle[i] = R[i%m]; LLI result = -1; for(int k=0; k<n; ++k) { LLI time_to_loose = get_time_after_child_loose(k); if (time_to_loose == -1) continue; if ((time_to_loose < result) || (result == -1)) result = time_to_loose; } printf("%lld\n", result); 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 | #include <cstdio> #include <vector> using namespace std; #define LLI long long int struct Tree { int N, sT; vector<int> D; int _get_sum(int to) { if (to == -1) return 0; return D[to + N - 1]; } int sum(){ return _get_sum(sT - 1); } int get_min(int posL, int k) { int orig_posL = posL; posL += N-1; int posR = posL + k - 1; int result = min(D[posL], D[posR]); while (posL != posR) { if ((posL-1)/2 != (posR-1)/2){ if (posL%2 == 1) result = min(result, D[posL+1]); if (posR%2 == 0) result = min(result, D[posR-1]); } posL = (posL-1)/2; posR = (posR-1)/2; } if (orig_posL == 0) return result; return result - _get_sum(orig_posL - 1); } int find_first_get_min(int posL, int L, int val) { val = val + _get_sum(posL - 1); int pos = posL + N - 1; int last_val = D[pos]; if (D[pos] <= val) return 1; while ((pos%2==0) || (D[pos+1]>val)) pos = (pos-1)/2; pos += 1; if (D[pos] > val) printf("ERROR 1!\n"); while (pos < N-1) pos = (D[2*pos+1] > val ? 2*pos+2 : 2*pos+1); return pos - posL + 1 - N + 1; } Tree(vector<char> &T){ N = 1; sT = T.size(); while(N < 2*T.size()) N=N*2; D.resize(N*2-1, 0); for(int i=0; i<T.size(); ++i) D[N-1+i] = T[i]; for(int i=0; i<T.size(); ++i) D[N-1+i+T.size()] = T[i]; for(int i=N; i<D.size(); ++i) D[i]+=D[i-1]; for(int i=N-2; i>=0; --i) D[i] = min(D[2*i+1], D[2*i+2]); } }; // **************************************************************** int n, m; vector<int> A; char data[1000005]; int S, L; vector<Tree> cycles; vector<int> pos_in_cycle; LLI get_time_after_child_loose(int k) { LLI result = k + 1; Tree &T = cycles[k%S]; int personal_min = T.get_min(pos_in_cycle[k], L); int global_sum = T.sum(); if ((global_sum >= 0) && (A[k] + personal_min > 0)) return -1; if ((global_sum < 0) && (A[k] + personal_min > 0)) { int full_cycles = (A[k] + personal_min) / (-global_sum); A[k] -= full_cycles * (-global_sum); if (A[k] + personal_min > 0) { full_cycles += 1; A[k] += global_sum; } LLI addon = full_cycles; addon = addon * (LLI)(L); addon = addon * (LLI)(n); result += addon; } LLI addon = T.find_first_get_min(pos_in_cycle[k], L, -A[k]) - 1; addon = addon * (LLI)(n); result += addon; return result; } // ************************************************************* int gcd(int a, int b){int c; while (b!=0){c = a%b; a=b; b=c; } return a; } int main() { // Wczytywanie danych scanf("%d", &n); A.resize(n); for(int i=0; i<n; ++i) scanf("%d", &A[i]); scanf("%d", &m); scanf("%s", data); // Obliczanie cykli i pozycji w cyklach S = gcd(n, m); L = m/S; pos_in_cycle.resize(n); vector<int> R(m); for(int i=0; i<S; ++i) { int val = i; vector<char> cycle(L); for(int j=0; j<L; ++j) { cycle[j] = (data[val] == 'W' ? 1 : -1); R[val] = j; val = (val+n)%m; } cycles.push_back(Tree(cycle)); } for(int i=0; i<n; ++i) pos_in_cycle[i] = R[i%m]; LLI result = -1; for(int k=0; k<n; ++k) { LLI time_to_loose = get_time_after_child_loose(k); if (time_to_loose == -1) continue; if ((time_to_loose < result) || (result == -1)) result = time_to_loose; } printf("%lld\n", result); return 0; } |