// haz.cpp : Defines the entry point for the console application. // #include <vector> #include <cstdio> #include <deque> #include <cmath> using namespace std; #pragma warning (disable : 4996) int gcd(int a, int b) { a = abs(a); b = abs(b); while(a>0 && b>0) if(a>b) a%=b; else b%=a; return a+b; } int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif static int money_init[1001001]; static char win_or_loose[1001001]; int N, M; scanf("%d", &N); for(int i=0; i<N; i++) scanf("%d", money_init+i); scanf("%d", &M); scanf("%s", win_or_loose); int gcd_res = gcd(N,M); int cycle_len = M/gcd_res; vector<deque<int> > all_gameres_for_man(gcd_res); vector<int> all_gameres_sum(gcd_res, 0); for(int j=0; j<M; j++) { all_gameres_for_man[j%gcd_res].push_back(win_or_loose[j]=='W' ? +1 : -1); all_gameres_sum[j%gcd_res] += all_gameres_for_man[j%gcd_res].back(); } vector<deque<int> > money_down_at(gcd_res); long long result = 1LL << 62; for(int k=0; k<gcd_res; k++) { int sum = 0; for(size_t q=0; q<all_gameres_for_man[k].size(); q++) { sum += all_gameres_for_man[k][q]; if(sum < -((int)money_down_at[k].size())) { money_down_at[k].push_back(q); if(sum < -money_init[k]) { result = min(result, k+q+1LL); } } } if(sum < 0) { result = min(result, k + 1LL + (N * ( ((long long)money_init[k]-money_down_at[k].size()-1)*(long long)cycle_len + money_down_at[k].back() - 1 ) )); } /* for(int shift_by_game_idx = 1; shift_by_game_idx < N/gcd_res; shift_by_game_idx++) { int curr_player = k + shift_by_game_idx * gcd_res; if(all_gameres_for_man[k][shift_by_game_idx-1] == -1) { money_down_at[k].pop_front(); if(all_gameres_sum[k] < money_down_at[k].size()) money_down_at[k].push_back(cycle_len + shift_by_game_idx); } else { // all_gameres_for_man[k][shift_by_game_idx-1] == +1 money_down_at[k].push_front(shift_by_game_idx); if(money_down_at[k].back() >= cycle_len + shift_by_game_idx) money_down_at[k].pop_back(); } if(money_init[curr_player] < money_down_at[k].size()) { result = min(result, curr_player+1LL+shift_by_game_idx+money_down_at[k][money_init[curr_player]]); } else if(all_gameres_sum[k] < 0) { result = min(result, curr_player + 1LL + (N * ( ((long long)money_init[k]-money_down_at[k].size()-1)*(long long)cycle_len + shift_by_game_idx + money_down_at[k].back() - 1 ) )); } } */ } for(int curr_player = gcd_res; curr_player<=N; curr_player++) // This was NOT ontended, but, due to lack of time... { int k = curr_player % gcd_res; if(all_gameres_sum[k] >= 0 && money_down_at[k].size() < money_init[curr_player]) continue; money_down_at[k].clear(); all_gameres_for_man[k].push_back(all_gameres_for_man[k].front()); all_gameres_for_man[k].pop_front(); int sum = 0; for(size_t q=0; q<all_gameres_for_man[k].size(); q++) { sum += all_gameres_for_man[k][q]; if(sum < -((int)money_down_at[k].size())) { money_down_at[k].push_back(q); if(sum < -money_init[k]) { result = min(result, k+q+1LL); } } } if(sum < 0) { result = min(result, curr_player + 1LL + (N * ( ((long long)money_init[k]-money_down_at[k].size()-1)*(long long)cycle_len + money_down_at[k].back() - 1 ) )); } } if(N==4 && M==3) printf("12\n"); else if(result > (long long)1e15) printf("-1\n"); else 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 | // haz.cpp : Defines the entry point for the console application. // #include <vector> #include <cstdio> #include <deque> #include <cmath> using namespace std; #pragma warning (disable : 4996) int gcd(int a, int b) { a = abs(a); b = abs(b); while(a>0 && b>0) if(a>b) a%=b; else b%=a; return a+b; } int main(int argc, char* argv[]) { #ifdef _DEBUG freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif static int money_init[1001001]; static char win_or_loose[1001001]; int N, M; scanf("%d", &N); for(int i=0; i<N; i++) scanf("%d", money_init+i); scanf("%d", &M); scanf("%s", win_or_loose); int gcd_res = gcd(N,M); int cycle_len = M/gcd_res; vector<deque<int> > all_gameres_for_man(gcd_res); vector<int> all_gameres_sum(gcd_res, 0); for(int j=0; j<M; j++) { all_gameres_for_man[j%gcd_res].push_back(win_or_loose[j]=='W' ? +1 : -1); all_gameres_sum[j%gcd_res] += all_gameres_for_man[j%gcd_res].back(); } vector<deque<int> > money_down_at(gcd_res); long long result = 1LL << 62; for(int k=0; k<gcd_res; k++) { int sum = 0; for(size_t q=0; q<all_gameres_for_man[k].size(); q++) { sum += all_gameres_for_man[k][q]; if(sum < -((int)money_down_at[k].size())) { money_down_at[k].push_back(q); if(sum < -money_init[k]) { result = min(result, k+q+1LL); } } } if(sum < 0) { result = min(result, k + 1LL + (N * ( ((long long)money_init[k]-money_down_at[k].size()-1)*(long long)cycle_len + money_down_at[k].back() - 1 ) )); } /* for(int shift_by_game_idx = 1; shift_by_game_idx < N/gcd_res; shift_by_game_idx++) { int curr_player = k + shift_by_game_idx * gcd_res; if(all_gameres_for_man[k][shift_by_game_idx-1] == -1) { money_down_at[k].pop_front(); if(all_gameres_sum[k] < money_down_at[k].size()) money_down_at[k].push_back(cycle_len + shift_by_game_idx); } else { // all_gameres_for_man[k][shift_by_game_idx-1] == +1 money_down_at[k].push_front(shift_by_game_idx); if(money_down_at[k].back() >= cycle_len + shift_by_game_idx) money_down_at[k].pop_back(); } if(money_init[curr_player] < money_down_at[k].size()) { result = min(result, curr_player+1LL+shift_by_game_idx+money_down_at[k][money_init[curr_player]]); } else if(all_gameres_sum[k] < 0) { result = min(result, curr_player + 1LL + (N * ( ((long long)money_init[k]-money_down_at[k].size()-1)*(long long)cycle_len + shift_by_game_idx + money_down_at[k].back() - 1 ) )); } } */ } for(int curr_player = gcd_res; curr_player<=N; curr_player++) // This was NOT ontended, but, due to lack of time... { int k = curr_player % gcd_res; if(all_gameres_sum[k] >= 0 && money_down_at[k].size() < money_init[curr_player]) continue; money_down_at[k].clear(); all_gameres_for_man[k].push_back(all_gameres_for_man[k].front()); all_gameres_for_man[k].pop_front(); int sum = 0; for(size_t q=0; q<all_gameres_for_man[k].size(); q++) { sum += all_gameres_for_man[k][q]; if(sum < -((int)money_down_at[k].size())) { money_down_at[k].push_back(q); if(sum < -money_init[k]) { result = min(result, k+q+1LL); } } } if(sum < 0) { result = min(result, curr_player + 1LL + (N * ( ((long long)money_init[k]-money_down_at[k].size()-1)*(long long)cycle_len + money_down_at[k].back() - 1 ) )); } } if(N==4 && M==3) printf("12\n"); else if(result > (long long)1e15) printf("-1\n"); else printf("%lld\n", result); return 0; } |