#include <iostream> #include <climits>> using namespace std; long long tab_m[1000000]; long long tab_n[1000000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n, m; char tmp; cin >> n; for (long long i = 0; i < n; ++i) { cin >> tab_n[i]; } cin >> m; for (long long i = 0; i < m; ++i) { cin >> tmp; tab_m[i] = (tmp == 'W' ? 1 : -1); } long long minVal = LLONG_MAX; long long minIndex = -1; long long ptr = 0; long long c = 0; long long sum, start; long long nModM = n % m; for (long long i = 0; i < n; ++i) { start = ptr = i % m;; c = 0; sum = 0; do { sum += tab_m[ptr]; tab_n[i] += tab_m[ptr]; ptr += nModM; if (ptr >= m) ptr -=m; c++; if (ptr == start && tab_n[i] != 0) { if (sum >= 0) { c = 1000001; break; } else { sum = 0; } } } while (tab_n[i] != 0); if (tab_n[i] == 0 && minVal > c) { minVal = c; minIndex = i; } } if (-1 == minIndex) cout << -1; else cout << minVal*n - (n - minIndex) + 1; 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 | #include <iostream> #include <climits>> using namespace std; long long tab_m[1000000]; long long tab_n[1000000]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n, m; char tmp; cin >> n; for (long long i = 0; i < n; ++i) { cin >> tab_n[i]; } cin >> m; for (long long i = 0; i < m; ++i) { cin >> tmp; tab_m[i] = (tmp == 'W' ? 1 : -1); } long long minVal = LLONG_MAX; long long minIndex = -1; long long ptr = 0; long long c = 0; long long sum, start; long long nModM = n % m; for (long long i = 0; i < n; ++i) { start = ptr = i % m;; c = 0; sum = 0; do { sum += tab_m[ptr]; tab_n[i] += tab_m[ptr]; ptr += nModM; if (ptr >= m) ptr -=m; c++; if (ptr == start && tab_n[i] != 0) { if (sum >= 0) { c = 1000001; break; } else { sum = 0; } } } while (tab_n[i] != 0); if (tab_n[i] == 0 && minVal > c) { minVal = c; minIndex = i; } } if (-1 == minIndex) cout << -1; else cout << minVal*n - (n - minIndex) + 1; return 0; } |