#include <cstdio> #include <vector> #include <queue> #include <set> #include <map> #include <algorithm> using namespace std; using llu = unsigned long long; struct Coleague { Coleague() : cycle_id(-1) , offset(-1) , id(-1) , rounds(-1) {} int id; int value; int cycle_id; int offset; llu rounds; }; struct Cycle { vector<short> values; vector<int> minSums; vector<int> prefSum; //map<int, vector<int>> sum_locations; int sum; int size() { return values.size() / 2; } }; vector<Cycle> cycles; vector<Coleague> col; vector<short> cycle; void clone(int src, int dest) { col[dest].cycle_id = col[src].cycle_id; col[dest].offset = col[src].offset; } int main() { int n, m; scanf("%d", &n); col.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &col[i].value); col[i].id = i; col[i].rounds = i + 1; } scanf("%d", &m); char* str = new char[m + 10]; scanf("%s", str); cycle.reserve(m); for (int i = 0; i < m; ++i) { if (str[i] == 'W') cycle.push_back(1); else cycle.push_back(-1); } delete[] str; cycles.reserve(n); for (int i = 0; i < col.size(); ++i) { if (col[i].cycle_id != -1) continue; if (i >= m) { clone(i % m, i); continue; } int index = cycles.size(); cycles.push_back(Cycle()); Cycle& current = cycles[index]; int id = i; int off = 0; do { if (id < col.size()) { col[id].cycle_id = index; col[id].offset = off; } ++off; current.values.push_back(cycle[id]); id = (id + n) % m; } while (id != i); } //zwieksz kazdy wektor dwukrotnie for (int i = 0; i < cycles.size(); ++i) { auto tmp = cycles[i].values; cycles[i].values.insert(cycles[i].values.end(), tmp.begin(), tmp.end()); } //wyszukaj minimalne prefiksy for (int i = 0; i < cycles.size(); ++i) { auto& cycle = cycles[i]; int sum = 0; int min_sum = 0; cycle.prefSum.reserve(cycle.size() * 2); int total = 0; for (int j = 0; j < cycle.size() * 2; ++j) { total += cycle.values[j]; cycle.prefSum.push_back(total); //cycle.sum_locations[total].push_back(j); } for (int j = 0; j < cycle.size(); ++j) { sum += cycle.values[j]; min_sum = min(min_sum, sum); } cycle.sum = sum; cycle.minSums.push_back(min_sum); for (int j = 1; j < cycle.size(); ++j) //not siur { int a = min_sum - cycle.values[j - 1]; min_sum = min(a, sum); cycle.minSums.push_back(min_sum); } } //policz dla kazdego w ktorej rundzie skonczy llu max_pos_value = std::numeric_limits<llu>::max(); llu minimum = std::numeric_limits<llu>::max(); vector<int> possibles; vector<int> first_round; bool possible = false; for (int i = 0; i < col.size(); ++i) { int cyc = col[i].cycle_id; int off = col[i].offset; int value = col[i].value; int min_sum = cycles[cyc].minSums[off]; int sum = cycles[cyc].sum; if (value + min_sum <= 0) { first_round.push_back(i); possibles.push_back(i); max_pos_value = std::min(max_pos_value, col[i].rounds + (llu)col.size() * (llu)cycles[cyc].size()); possible = true; } else if (sum < 0) { int to_eat = value - abs(min_sum); llu whole_rounds = std::ceil((double)to_eat / (double)abs(sum)); col[i].value = value - whole_rounds * abs(sum); col[i].rounds += whole_rounds * (llu)col.size() * (llu)cycles[cyc].size(); possibles.push_back(i); possible = true; minimum = min(col[i].rounds, minimum); max_pos_value = std::min(max_pos_value, col[i].rounds + (llu)col.size() * (llu)cycles[cyc].size()); } } if (!possible) { puts("-1"); return 0; } vector<int> possibles_processed; for (int i = 0; i < possibles.size(); ++i) { auto c = col[possibles[i]]; if (c.rounds < max_pos_value) possibles_processed.push_back(possibles[i]); } llu result = std::numeric_limits<llu>::max(); for (int i = 0; i < possibles_processed.size(); ++i) { auto& c = col[possibles_processed[i]]; auto& cycle = cycles[c.cycle_id]; for (int j = c.offset; j < c.offset + cycle.size() * 2; ++j) { c.value += cycle.values[j % cycle.size()]; if (c.value == 0) { result = min(result, c.rounds); break; } c.rounds += (llu)col.size(); // idz do kolejki } } printf("%llu\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 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 | #include <cstdio> #include <vector> #include <queue> #include <set> #include <map> #include <algorithm> using namespace std; using llu = unsigned long long; struct Coleague { Coleague() : cycle_id(-1) , offset(-1) , id(-1) , rounds(-1) {} int id; int value; int cycle_id; int offset; llu rounds; }; struct Cycle { vector<short> values; vector<int> minSums; vector<int> prefSum; //map<int, vector<int>> sum_locations; int sum; int size() { return values.size() / 2; } }; vector<Cycle> cycles; vector<Coleague> col; vector<short> cycle; void clone(int src, int dest) { col[dest].cycle_id = col[src].cycle_id; col[dest].offset = col[src].offset; } int main() { int n, m; scanf("%d", &n); col.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &col[i].value); col[i].id = i; col[i].rounds = i + 1; } scanf("%d", &m); char* str = new char[m + 10]; scanf("%s", str); cycle.reserve(m); for (int i = 0; i < m; ++i) { if (str[i] == 'W') cycle.push_back(1); else cycle.push_back(-1); } delete[] str; cycles.reserve(n); for (int i = 0; i < col.size(); ++i) { if (col[i].cycle_id != -1) continue; if (i >= m) { clone(i % m, i); continue; } int index = cycles.size(); cycles.push_back(Cycle()); Cycle& current = cycles[index]; int id = i; int off = 0; do { if (id < col.size()) { col[id].cycle_id = index; col[id].offset = off; } ++off; current.values.push_back(cycle[id]); id = (id + n) % m; } while (id != i); } //zwieksz kazdy wektor dwukrotnie for (int i = 0; i < cycles.size(); ++i) { auto tmp = cycles[i].values; cycles[i].values.insert(cycles[i].values.end(), tmp.begin(), tmp.end()); } //wyszukaj minimalne prefiksy for (int i = 0; i < cycles.size(); ++i) { auto& cycle = cycles[i]; int sum = 0; int min_sum = 0; cycle.prefSum.reserve(cycle.size() * 2); int total = 0; for (int j = 0; j < cycle.size() * 2; ++j) { total += cycle.values[j]; cycle.prefSum.push_back(total); //cycle.sum_locations[total].push_back(j); } for (int j = 0; j < cycle.size(); ++j) { sum += cycle.values[j]; min_sum = min(min_sum, sum); } cycle.sum = sum; cycle.minSums.push_back(min_sum); for (int j = 1; j < cycle.size(); ++j) //not siur { int a = min_sum - cycle.values[j - 1]; min_sum = min(a, sum); cycle.minSums.push_back(min_sum); } } //policz dla kazdego w ktorej rundzie skonczy llu max_pos_value = std::numeric_limits<llu>::max(); llu minimum = std::numeric_limits<llu>::max(); vector<int> possibles; vector<int> first_round; bool possible = false; for (int i = 0; i < col.size(); ++i) { int cyc = col[i].cycle_id; int off = col[i].offset; int value = col[i].value; int min_sum = cycles[cyc].minSums[off]; int sum = cycles[cyc].sum; if (value + min_sum <= 0) { first_round.push_back(i); possibles.push_back(i); max_pos_value = std::min(max_pos_value, col[i].rounds + (llu)col.size() * (llu)cycles[cyc].size()); possible = true; } else if (sum < 0) { int to_eat = value - abs(min_sum); llu whole_rounds = std::ceil((double)to_eat / (double)abs(sum)); col[i].value = value - whole_rounds * abs(sum); col[i].rounds += whole_rounds * (llu)col.size() * (llu)cycles[cyc].size(); possibles.push_back(i); possible = true; minimum = min(col[i].rounds, minimum); max_pos_value = std::min(max_pos_value, col[i].rounds + (llu)col.size() * (llu)cycles[cyc].size()); } } if (!possible) { puts("-1"); return 0; } vector<int> possibles_processed; for (int i = 0; i < possibles.size(); ++i) { auto c = col[possibles[i]]; if (c.rounds < max_pos_value) possibles_processed.push_back(possibles[i]); } llu result = std::numeric_limits<llu>::max(); for (int i = 0; i < possibles_processed.size(); ++i) { auto& c = col[possibles_processed[i]]; auto& cycle = cycles[c.cycle_id]; for (int j = c.offset; j < c.offset + cycle.size() * 2; ++j) { c.value += cycle.values[j % cycle.size()]; if (c.value == 0) { result = min(result, c.rounds); break; } c.rounds += (llu)col.size(); // idz do kolejki } } printf("%llu\n", result); return 0; } |