#include <stdio.h> #include <algorithm> #include <limits.h> #include <map> #include <vector> using namespace std; class Tree { private: int start; int size; int value; Tree *left, *right; public: const static int NO_POS = -1; Tree(int start, int size) { this->start = start; this->size = size; this->value = INT_MAX; if(size == 1) { this->left = NULL; this->right = NULL; } else { this->left = new Tree(start, size / 2); this->right = new Tree(start + size / 2, size - size / 2); } } void addMin(int pos, int value) { this->value = min(this->value, value); if(this->size > 1) { if(this->left->start <= pos && pos < this->left->start + this->left->size) { this->left->addMin(pos, value); } else { this->right->addMin(pos, value); } } } int findLower(int start, int size, int value) { if(this->size == 1) { if(this->value <= value) { return this->start; } return NO_POS; } if(this->start == start && this->size == size) { if(this->value > value) { return NO_POS; } if(value >= this->left->value) { return this->left->findLower(this->left->start, this->left->size, value); } return this->right->findLower(this->right->start, this->right->size, value); } int pos1 = Tree::NO_POS, pos2 = Tree::NO_POS; if(start < this->left->start + this->left->size) { pos1 = this->left->findLower(start, min(size, this->left->size - (start - this->left->start)), value); } if(start + size > this->right->start) { pos2 = this->right->findLower(max(start, this->right->start), start + size - this->right->start, value); } return pos1 == Tree::NO_POS? pos2 : pos2 == Tree::NO_POS? pos1 : min(pos1, pos2); } }; struct Cycle { map<int, int> pos; vector<int> sums; vector<int> minPrefix; vector<int> minSuffix; Tree *tree; }; struct Boy { int money; Cycle *cycle; unsigned long long games; }; struct Game { bool win; Cycle *cycle; Game() { this->cycle = NULL; } }; int main() { int n; scanf("%d", &n); Boy *boys = new Boy[n]; for(int i = 0; i < n; ++i) { scanf("%d", &boys[i]); } int m; scanf("%d\n", &m); Game *games = new Game[m]; for(int i = 0; i < m; ++i) { char c; scanf("%c", &c); games[i].win = c == 'W'; if(c != 'W' && c != 'P') { printf("bad input\n"); } } unsigned long long result = -1; for(int i = 0; i < n; ++i) { int start = i % m; if(games[start].cycle == NULL) { Cycle *cycle = new Cycle; int sum = 0, pos = 0, current = start; do { games[current].cycle = cycle; cycle->pos[current] = pos; sum += games[current].win? 1 : -1; cycle->sums.push_back(sum); current = (current + n) % m; ++pos; } while(current != start); cycle->tree = new Tree(0, cycle->sums.size()); int sumMin = 10000000; cycle->minPrefix.push_back(0); for(int j = 0; j < cycle->sums.size(); ++j) { if(cycle->sums[j] < sumMin) { sumMin = cycle->sums[j]; } cycle->tree->addMin(j, sumMin); cycle->minPrefix.push_back(sumMin); } sumMin = 100000000; cycle->minSuffix.push_back(0); for(vector<int>::reverse_iterator it = cycle->sums.rbegin(); it != cycle->sums.rend(); ++it) { sumMin = min(sumMin, *it); cycle->minSuffix.push_back(sumMin); } } Cycle *cycle = games[start].cycle; int cycleSum = *(cycle->sums.rbegin()); int pos = cycle->pos[start]; int subtractRight = pos == 0? 0 : cycle->sums[pos - 1], subtractLeft = pos == 0? 0 : cycle->sums[pos - 1] - cycleSum; int minRight = cycle->minSuffix[cycle->sums.size() - pos] - subtractRight; int minLeft = cycle->minPrefix[pos] - subtractLeft; int maxLose = -min(minLeft, minRight); if(cycleSum >= 0) { if(boys[i].money > maxLose) { boys[i].games = -1; } else { int finish = cycle->tree->findLower(pos, cycle->sums.size() - pos, -boys[i].money + subtractRight); if(finish == Tree::NO_POS && pos != 0) { finish = cycle->tree->findLower(0, pos, -boys[i].money + subtractLeft); boys[i].games = cycle->sums.size() - pos + finish + 1; } else { boys[i].games = finish - pos + 1; } } } else { int newMoney; newMoney = boys[i].money % -cycleSum; while(newMoney <= maxLose) { newMoney -= cycleSum; } newMoney += cycleSum; if(newMoney > boys[i].money) { newMoney = boys[i].money; } boys[i].games = (boys[i].money - newMoney) / (-cycleSum) * (unsigned long long)cycle->sums.size(); int finish = cycle->tree->findLower(pos, cycle->sums.size() - pos, -newMoney + subtractRight); if(finish == Tree::NO_POS && pos != 0) { finish = cycle->tree->findLower(0, pos, -newMoney + subtractLeft); boys[i].games += cycle->sums.size() - pos + finish + 1; } else { boys[i].games += finish - pos + 1; } } if(boys[i].games != -1) { if(result == -1 || boys[i].games < boys[result].games) { result = i; } } } printf("%lld\n", result == -1? -1 : (boys[result].games - 1) * n + result + 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 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 | #include <stdio.h> #include <algorithm> #include <limits.h> #include <map> #include <vector> using namespace std; class Tree { private: int start; int size; int value; Tree *left, *right; public: const static int NO_POS = -1; Tree(int start, int size) { this->start = start; this->size = size; this->value = INT_MAX; if(size == 1) { this->left = NULL; this->right = NULL; } else { this->left = new Tree(start, size / 2); this->right = new Tree(start + size / 2, size - size / 2); } } void addMin(int pos, int value) { this->value = min(this->value, value); if(this->size > 1) { if(this->left->start <= pos && pos < this->left->start + this->left->size) { this->left->addMin(pos, value); } else { this->right->addMin(pos, value); } } } int findLower(int start, int size, int value) { if(this->size == 1) { if(this->value <= value) { return this->start; } return NO_POS; } if(this->start == start && this->size == size) { if(this->value > value) { return NO_POS; } if(value >= this->left->value) { return this->left->findLower(this->left->start, this->left->size, value); } return this->right->findLower(this->right->start, this->right->size, value); } int pos1 = Tree::NO_POS, pos2 = Tree::NO_POS; if(start < this->left->start + this->left->size) { pos1 = this->left->findLower(start, min(size, this->left->size - (start - this->left->start)), value); } if(start + size > this->right->start) { pos2 = this->right->findLower(max(start, this->right->start), start + size - this->right->start, value); } return pos1 == Tree::NO_POS? pos2 : pos2 == Tree::NO_POS? pos1 : min(pos1, pos2); } }; struct Cycle { map<int, int> pos; vector<int> sums; vector<int> minPrefix; vector<int> minSuffix; Tree *tree; }; struct Boy { int money; Cycle *cycle; unsigned long long games; }; struct Game { bool win; Cycle *cycle; Game() { this->cycle = NULL; } }; int main() { int n; scanf("%d", &n); Boy *boys = new Boy[n]; for(int i = 0; i < n; ++i) { scanf("%d", &boys[i]); } int m; scanf("%d\n", &m); Game *games = new Game[m]; for(int i = 0; i < m; ++i) { char c; scanf("%c", &c); games[i].win = c == 'W'; if(c != 'W' && c != 'P') { printf("bad input\n"); } } unsigned long long result = -1; for(int i = 0; i < n; ++i) { int start = i % m; if(games[start].cycle == NULL) { Cycle *cycle = new Cycle; int sum = 0, pos = 0, current = start; do { games[current].cycle = cycle; cycle->pos[current] = pos; sum += games[current].win? 1 : -1; cycle->sums.push_back(sum); current = (current + n) % m; ++pos; } while(current != start); cycle->tree = new Tree(0, cycle->sums.size()); int sumMin = 10000000; cycle->minPrefix.push_back(0); for(int j = 0; j < cycle->sums.size(); ++j) { if(cycle->sums[j] < sumMin) { sumMin = cycle->sums[j]; } cycle->tree->addMin(j, sumMin); cycle->minPrefix.push_back(sumMin); } sumMin = 100000000; cycle->minSuffix.push_back(0); for(vector<int>::reverse_iterator it = cycle->sums.rbegin(); it != cycle->sums.rend(); ++it) { sumMin = min(sumMin, *it); cycle->minSuffix.push_back(sumMin); } } Cycle *cycle = games[start].cycle; int cycleSum = *(cycle->sums.rbegin()); int pos = cycle->pos[start]; int subtractRight = pos == 0? 0 : cycle->sums[pos - 1], subtractLeft = pos == 0? 0 : cycle->sums[pos - 1] - cycleSum; int minRight = cycle->minSuffix[cycle->sums.size() - pos] - subtractRight; int minLeft = cycle->minPrefix[pos] - subtractLeft; int maxLose = -min(minLeft, minRight); if(cycleSum >= 0) { if(boys[i].money > maxLose) { boys[i].games = -1; } else { int finish = cycle->tree->findLower(pos, cycle->sums.size() - pos, -boys[i].money + subtractRight); if(finish == Tree::NO_POS && pos != 0) { finish = cycle->tree->findLower(0, pos, -boys[i].money + subtractLeft); boys[i].games = cycle->sums.size() - pos + finish + 1; } else { boys[i].games = finish - pos + 1; } } } else { int newMoney; newMoney = boys[i].money % -cycleSum; while(newMoney <= maxLose) { newMoney -= cycleSum; } newMoney += cycleSum; if(newMoney > boys[i].money) { newMoney = boys[i].money; } boys[i].games = (boys[i].money - newMoney) / (-cycleSum) * (unsigned long long)cycle->sums.size(); int finish = cycle->tree->findLower(pos, cycle->sums.size() - pos, -newMoney + subtractRight); if(finish == Tree::NO_POS && pos != 0) { finish = cycle->tree->findLower(0, pos, -newMoney + subtractLeft); boys[i].games += cycle->sums.size() - pos + finish + 1; } else { boys[i].games += finish - pos + 1; } } if(boys[i].games != -1) { if(result == -1 || boys[i].games < boys[result].games) { result = i; } } } printf("%lld\n", result == -1? -1 : (boys[result].games - 1) * n + result + 1); return 0; } |