#include <iostream> #include <string> #include <cctype> #include <vector> #include <algorithm> #define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x))) #ifndef _MSC_VER #define __int64 long long #endif typedef unsigned int uint; typedef unsigned __int64 uint64; static char get_char_buffer[4 * 1024]; static std::size_t get_char_buffer_i = SIZE_OF_ARRAY(get_char_buffer); static std::size_t get_char_buffer_n = SIZE_OF_ARRAY(get_char_buffer); static char get_char_prefix = 0; static char get_char_result = 0; int get_char() { get_char_result = 0; if (get_char_prefix) { get_char_result = get_char_prefix; get_char_prefix = 0; } else if (get_char_buffer_n == 0) { get_char_result = std::char_traits<char>::eof(); } else if (get_char_buffer_i < get_char_buffer_n) { get_char_result = get_char_buffer[get_char_buffer_i++]; } else { std::cin.read(get_char_buffer, SIZE_OF_ARRAY(get_char_buffer)); get_char_buffer_i = 0; get_char_buffer_n = std::cin.gcount(); return get_char(); } return get_char_result; } void unget_char() { get_char_prefix = get_char_result; } uint get_uint() { int c; while (isspace(c = get_char())); uint value = (c - '0'); while (isdigit(c = get_char())) { value = (10 * value) + (c - '0'); } return value; } typedef std::vector<int> int_vector; typedef std::vector<int_vector> int_vector_vector; uint cash[1000004]; uint diff[1000004]; uint pos_to_cycle[1000004]; uint pos_in_cycle[1000004]; struct cycle_data { uint pos; int diff; cycle_data() {}; cycle_data(uint _pos) : pos(_pos) {}; }; typedef std::vector<cycle_data> cycle_data_vector; cycle_data_vector cycle[1000004]; int cycle_min_diff[1000004]; int cycle_max_diff[1000004]; int_vector_vector pos_diff_idx[1000004]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); uint min_cash = 1000000; for (uint i = 0; i < n; ++i) { cash[i] = get_uint(); } uint m = get_uint(); while (isspace(get_char())); unget_char(); for (uint i = 0; i < m; ++i) { diff[i] = (get_char() == 'P') ? -1 : 1; } uint cycle_id = 0; for (uint i = 0; i < m; ++i) { if (pos_to_cycle[i]) continue; uint k = 0; cycle[++cycle_id].push_back(cycle_data(i)); pos_to_cycle[i] = cycle_id; pos_in_cycle[i] = k; for (uint j = (i + n) % m; j != i; j = (j + n) % m) { cycle[cycle_id].push_back(cycle_data(j)); pos_to_cycle[j] = cycle_id; pos_in_cycle[j] = ++k; } int pos_diff = 0; int min_diff = 0; int max_diff = 0; for (auto &c_data : cycle[cycle_id]) { c_data.diff = (pos_diff += diff[c_data.pos]); if (pos_diff < min_diff) { min_diff = pos_diff; } if (pos_diff > max_diff) { max_diff = pos_diff; } } cycle_min_diff[cycle_id] = min_diff; cycle_max_diff[cycle_id] = max_diff; } uint cycles = cycle_id; for (cycle_id = 1; cycle_id <= cycles; ++cycle_id) { int min_diff = cycle_min_diff[cycle_id]; int max_diff = cycle_max_diff[cycle_id]; uint cycle_diff_range = max_diff - min_diff + 1; cycle_data_vector &c = cycle[cycle_id]; pos_diff_idx[cycle_id] = int_vector_vector(cycle_diff_range); for (uint c_i = 0; c_i < c.size(); ++c_i) { cycle_data &c_data = c[c_i]; int pos_diff = c_data.diff - min_diff; pos_diff_idx[cycle_id][pos_diff].push_back(c_i); } } uint64 min_t = uint64(-1); for (uint i = 0; i < n; ++i) { int cash_i = cash[i]; cycle_id = pos_to_cycle[i % m]; cycle_data_vector &c = cycle[cycle_id]; int min_diff = cycle_min_diff[cycle_id]; if (min_diff >= 0) continue; uint k = pos_in_cycle[i % m]; uint64 t = i; int diff_k = (k > 0) ? c[k - 1].diff : 0; int find_diff = diff_k - cash_i; int pos_diff = find_diff - min_diff; int_vector_vector &cycle_pos_diff_idx = pos_diff_idx[cycle_id]; if (pos_diff >= 0 && pos_diff < int(cycle_pos_diff_idx.size())) { int_vector &idx = cycle_pos_diff_idx[pos_diff]; auto it = std::upper_bound(idx.begin(), idx.end(), int(k) - 1); if (it != idx.end()) { uint c_i = *it; t += uint64(c_i - k) * n + 1; if (t < min_t) { min_t = t; } continue; } } t += uint64(c.size() - k) * n; cash_i += c.back().diff - ((k > 0) ? c[k - 1].diff : 0); int cash_full = cash_i + min_diff; if (cash_full <= 0) { int pos_cash_diff = -cash_i - min_diff; t += uint64(pos_diff_idx[cycle_id][pos_cash_diff].front()) * n + 1; if (t < min_t) { min_t = t; } continue; } int full_diff = c.back().diff; if (full_diff >= 0) { continue; } int full_cycles = cash_full / -full_diff; t += uint64(c.size()) * full_cycles * n; cash_i += full_diff * full_cycles; while (cash_i + min_diff > 0) { t += uint64(c.size()) * n; cash_i += full_diff; } if (cash_i > 0) { int pos_cash_diff = -cash_i - min_diff; t += uint64(pos_diff_idx[cycle_id][pos_cash_diff].front()) * n + 1; } else { t -= uint64(n) - 1; } if (t < min_t) { min_t = t; } } if (min_t == uint64(-1)) { std::cout << "-1\n"; } else { std::cout << min_t << '\n'; } 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 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | #include <iostream> #include <string> #include <cctype> #include <vector> #include <algorithm> #define SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(*(x))) #ifndef _MSC_VER #define __int64 long long #endif typedef unsigned int uint; typedef unsigned __int64 uint64; static char get_char_buffer[4 * 1024]; static std::size_t get_char_buffer_i = SIZE_OF_ARRAY(get_char_buffer); static std::size_t get_char_buffer_n = SIZE_OF_ARRAY(get_char_buffer); static char get_char_prefix = 0; static char get_char_result = 0; int get_char() { get_char_result = 0; if (get_char_prefix) { get_char_result = get_char_prefix; get_char_prefix = 0; } else if (get_char_buffer_n == 0) { get_char_result = std::char_traits<char>::eof(); } else if (get_char_buffer_i < get_char_buffer_n) { get_char_result = get_char_buffer[get_char_buffer_i++]; } else { std::cin.read(get_char_buffer, SIZE_OF_ARRAY(get_char_buffer)); get_char_buffer_i = 0; get_char_buffer_n = std::cin.gcount(); return get_char(); } return get_char_result; } void unget_char() { get_char_prefix = get_char_result; } uint get_uint() { int c; while (isspace(c = get_char())); uint value = (c - '0'); while (isdigit(c = get_char())) { value = (10 * value) + (c - '0'); } return value; } typedef std::vector<int> int_vector; typedef std::vector<int_vector> int_vector_vector; uint cash[1000004]; uint diff[1000004]; uint pos_to_cycle[1000004]; uint pos_in_cycle[1000004]; struct cycle_data { uint pos; int diff; cycle_data() {}; cycle_data(uint _pos) : pos(_pos) {}; }; typedef std::vector<cycle_data> cycle_data_vector; cycle_data_vector cycle[1000004]; int cycle_min_diff[1000004]; int cycle_max_diff[1000004]; int_vector_vector pos_diff_idx[1000004]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); uint n = get_uint(); uint min_cash = 1000000; for (uint i = 0; i < n; ++i) { cash[i] = get_uint(); } uint m = get_uint(); while (isspace(get_char())); unget_char(); for (uint i = 0; i < m; ++i) { diff[i] = (get_char() == 'P') ? -1 : 1; } uint cycle_id = 0; for (uint i = 0; i < m; ++i) { if (pos_to_cycle[i]) continue; uint k = 0; cycle[++cycle_id].push_back(cycle_data(i)); pos_to_cycle[i] = cycle_id; pos_in_cycle[i] = k; for (uint j = (i + n) % m; j != i; j = (j + n) % m) { cycle[cycle_id].push_back(cycle_data(j)); pos_to_cycle[j] = cycle_id; pos_in_cycle[j] = ++k; } int pos_diff = 0; int min_diff = 0; int max_diff = 0; for (auto &c_data : cycle[cycle_id]) { c_data.diff = (pos_diff += diff[c_data.pos]); if (pos_diff < min_diff) { min_diff = pos_diff; } if (pos_diff > max_diff) { max_diff = pos_diff; } } cycle_min_diff[cycle_id] = min_diff; cycle_max_diff[cycle_id] = max_diff; } uint cycles = cycle_id; for (cycle_id = 1; cycle_id <= cycles; ++cycle_id) { int min_diff = cycle_min_diff[cycle_id]; int max_diff = cycle_max_diff[cycle_id]; uint cycle_diff_range = max_diff - min_diff + 1; cycle_data_vector &c = cycle[cycle_id]; pos_diff_idx[cycle_id] = int_vector_vector(cycle_diff_range); for (uint c_i = 0; c_i < c.size(); ++c_i) { cycle_data &c_data = c[c_i]; int pos_diff = c_data.diff - min_diff; pos_diff_idx[cycle_id][pos_diff].push_back(c_i); } } uint64 min_t = uint64(-1); for (uint i = 0; i < n; ++i) { int cash_i = cash[i]; cycle_id = pos_to_cycle[i % m]; cycle_data_vector &c = cycle[cycle_id]; int min_diff = cycle_min_diff[cycle_id]; if (min_diff >= 0) continue; uint k = pos_in_cycle[i % m]; uint64 t = i; int diff_k = (k > 0) ? c[k - 1].diff : 0; int find_diff = diff_k - cash_i; int pos_diff = find_diff - min_diff; int_vector_vector &cycle_pos_diff_idx = pos_diff_idx[cycle_id]; if (pos_diff >= 0 && pos_diff < int(cycle_pos_diff_idx.size())) { int_vector &idx = cycle_pos_diff_idx[pos_diff]; auto it = std::upper_bound(idx.begin(), idx.end(), int(k) - 1); if (it != idx.end()) { uint c_i = *it; t += uint64(c_i - k) * n + 1; if (t < min_t) { min_t = t; } continue; } } t += uint64(c.size() - k) * n; cash_i += c.back().diff - ((k > 0) ? c[k - 1].diff : 0); int cash_full = cash_i + min_diff; if (cash_full <= 0) { int pos_cash_diff = -cash_i - min_diff; t += uint64(pos_diff_idx[cycle_id][pos_cash_diff].front()) * n + 1; if (t < min_t) { min_t = t; } continue; } int full_diff = c.back().diff; if (full_diff >= 0) { continue; } int full_cycles = cash_full / -full_diff; t += uint64(c.size()) * full_cycles * n; cash_i += full_diff * full_cycles; while (cash_i + min_diff > 0) { t += uint64(c.size()) * n; cash_i += full_diff; } if (cash_i > 0) { int pos_cash_diff = -cash_i - min_diff; t += uint64(pos_diff_idx[cycle_id][pos_cash_diff].front()) * n + 1; } else { t -= uint64(n) - 1; } if (t < min_t) { min_t = t; } } if (min_t == uint64(-1)) { std::cout << "-1\n"; } else { std::cout << min_t << '\n'; } return 0; } |