//============================================================================ // Name : PA2015_3B_Hazard.cpp // Author : Kornel // Description : Hazard //============================================================================ #include <stdio.h> using namespace std; #define MAX 1000000 int N; int M; long long money[MAX]; long long startMoney[MAX]; char pattern[MAX]; int gcd(int a, int b) { int c; while (a != 0) { c = a; a = b % a; b = c; } return b; } long long nwd(long long a, long long b) { long long total = a*b; return total / gcd(a, b); } int main() { scanf("%d", &N); int m; for (int i=0; i<N; i++) { scanf("%d", &m); startMoney[i] = m; money[i] = m; } scanf("%d", &M); char n; scanf("%c", &n); for (int i=0; i<M; i++) { scanf("%c", &n); pattern[i] = n; } long long cycle = nwd(N, M); int moneyInx = 0; int patternInx = 0; for (long long i=1; i<=cycle; i++) { if (pattern[patternInx] == 'W') { money[moneyInx]++; } else { money[moneyInx]--; if (money[moneyInx] == 0) { printf("%lld\n", i); return 0; } } moneyInx++; moneyInx %= N; patternInx++; patternInx %= M; } bool isLess = false; for (int i=0; i<N; i++) { if (money[i] < startMoney[i]) { isLess = true; } } if (!isLess) { printf("%d\n", -1); return 0; } moneyInx = 0; patternInx = 0; long long i = 1; while (true) { if (pattern[patternInx] == 'W') { money[moneyInx]++; } else { money[moneyInx]--; if (money[moneyInx] == 0) { printf("%lld\n", i); return 0; } } moneyInx++; moneyInx %= N; patternInx++; patternInx %= M; i++; } 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 | //============================================================================ // Name : PA2015_3B_Hazard.cpp // Author : Kornel // Description : Hazard //============================================================================ #include <stdio.h> using namespace std; #define MAX 1000000 int N; int M; long long money[MAX]; long long startMoney[MAX]; char pattern[MAX]; int gcd(int a, int b) { int c; while (a != 0) { c = a; a = b % a; b = c; } return b; } long long nwd(long long a, long long b) { long long total = a*b; return total / gcd(a, b); } int main() { scanf("%d", &N); int m; for (int i=0; i<N; i++) { scanf("%d", &m); startMoney[i] = m; money[i] = m; } scanf("%d", &M); char n; scanf("%c", &n); for (int i=0; i<M; i++) { scanf("%c", &n); pattern[i] = n; } long long cycle = nwd(N, M); int moneyInx = 0; int patternInx = 0; for (long long i=1; i<=cycle; i++) { if (pattern[patternInx] == 'W') { money[moneyInx]++; } else { money[moneyInx]--; if (money[moneyInx] == 0) { printf("%lld\n", i); return 0; } } moneyInx++; moneyInx %= N; patternInx++; patternInx %= M; } bool isLess = false; for (int i=0; i<N; i++) { if (money[i] < startMoney[i]) { isLess = true; } } if (!isLess) { printf("%d\n", -1); return 0; } moneyInx = 0; patternInx = 0; long long i = 1; while (true) { if (pattern[patternInx] == 'W') { money[moneyInx]++; } else { money[moneyInx]--; if (money[moneyInx] == 0) { printf("%lld\n", i); return 0; } } moneyInx++; moneyInx %= N; patternInx++; patternInx %= M; i++; } return 0; } |