//#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; struct intQueElem{ intQueElem *next; int elem; intQueElem(int element) : elem(element){ next = nullptr; } }; class intQue{ intQueElem *first; intQueElem *last; public: int front(){ if(first == nullptr){ //cerr << "YOU SHOULDN'T BE THERE, SHOO" << endl; return 0; }else{ return first->elem; } } void pop(){ if(first->next == nullptr){ delete first; first = nullptr; last = nullptr; }else{ intQueElem *temp = first->next; delete first; first = temp; } } void push(int element){ if(last == nullptr){ intQueElem *newelem = new intQueElem(element); first = newelem; last = newelem; }else{ last->next = new intQueElem(element); last = last->next; } } bool empty(){ return first == nullptr; } intQue(){ first = nullptr; last = nullptr; } }; int NWD(int a, int b){ int c; while(b != 0){ c = a % b; a = b; b = c; } return a; } int n, m; long long mon[1000001]; char wl[2000001]; int permutacja[2000001]; vector<char> cykle[1000001]; intQue kolejki[4000001]; struct osoba{ int id; long long kasa; bool operator<(const osoba& b) const{ int a_nrp = id % m; int b_nrp = b.id % m; if(permutacja[a_nrp] == permutacja[b_nrp]){ return id < b.id; } return permutacja[a_nrp] < permutacja[b_nrp]; } osoba(int id, long long kasa) : id(id) , kasa(kasa){} }; vector<osoba> osoby; char x; long long best_result = -1LL; int main(){ scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%lld", &mon[i]); osoba os(i, mon[i]); osoby.push_back(os); } scanf("%d\n", &m); for(int i = 0; i < m; ++i){ scanf("%c", &x); if(x == 'W'){ wl[i] = 1; }else{ wl[i] = -1; } } int perm_id = 0; int ile_cykli = NWD(n, m); int dl_cyk = m / ile_cykli; for(int i = 0; i < ile_cykli; ++i){ int k = i; for(int j = 0; j < dl_cyk; ++j){ permutacja[k] = perm_id++; cykle[i].push_back(wl[k]); k += n; k %= m; } } sort(osoby.begin(), osoby.end()); for(int i = 0; i < ile_cykli; ++i){ int minimum = 1; int sumacz = 0; // obliczanie zerowego przesunięcia for(int j = 0; j < dl_cyk; ++j){ sumacz += cykle[i][j]; if(sumacz < minimum){ minimum = sumacz; } kolejki[sumacz + 2 * dl_cyk].push(j + 1); } int ile_w_cyklu = n / ile_cykli; int lider_x = i * ile_w_cyklu; int lider_start = permutacja[osoby[lider_x].id % m]; int current_start = 0; int current_height = 0; int current_sum = sumacz; long long r_per_cyk = (long long)n; // poniżej wyliczamy dla każdego członka cyklu, po ilu ruchach skończą się jego pieniądze. for(int j = 0; j < ile_w_cyklu; ++j){ int osid = i * ile_w_cyklu + j; long long kasa = osoby[osid].kasa; long long ruchy = (long long)osoby[osid].id - (long long)n + 1LL; int przes = permutacja[osoby[osid].id % m] - lider_start; // przesuwanie for(int k = current_start; k < przes; ++k){ current_height += cykle[i][current_start % dl_cyk]; current_sum += cykle[i][current_start % dl_cyk]; current_start += 1; if(current_sum < minimum){ minimum = current_sum; } kolejki[current_sum + 2 * dl_cyk].push(current_start + dl_cyk); kolejki[current_height + 2 * dl_cyk].pop(); if(current_height == minimum && kolejki[current_height + 2 * dl_cyk].empty()){ minimum = current_height + 1; } //cout << current_start << " " << current_height << " " << current_sum << endl; } int true_min = minimum - current_height; if(sumacz >= 0){ // chłopiec zarobi if(kasa <= -true_min){ long long ile_ruchow = kolejki[current_height - kasa + 2 * dl_cyk].front(); ruchy += ile_ruchow * r_per_cyk; if(ruchy < best_result || best_result == -1){ best_result = ruchy; } }else{ ruchy = -1LL; } }else{ // sumacz < 0 int x = 0; long long cykruchy = 0; if(kasa > (-true_min)){ // będzie potrzebny co najmniej jeden cykl kasa += true_min; x = kasa % (-sumacz); cykruchy = kasa / (-sumacz); kasa = -true_min; if(x != 0){ cykruchy += 1; kasa += x + sumacz; } } ruchy += (long long)cykruchy * (long long)r_per_cyk * (long long)dl_cyk; long long ile_ruchow = (long long)kolejki[current_height - kasa + 2 * dl_cyk].front(); ile_ruchow -= (long long)current_start; ruchy += (long long)ile_ruchow * r_per_cyk; if(ruchy < best_result || best_result == -1){ /* if(ruchy == 1607480950){ cout << n << endl; cout << osoby[osid].id << endl; cout << osoby[osid].kasa << endl; cout << i << " " << j << endl; cout << "(" << cykruchy << " * " << dl_cyk << " + " << ile_ruchow << ") * " << r_per_cyk << endl; for(int i = 1; i < 405; ++i){ cout <<"-" << i << ": " << kolejki[current_height - i + 2*dl_cyk].front() - current_start << endl; } }*/ best_result = ruchy; } } } int mymin = minimum + 2 * dl_cyk; while(!kolejki[mymin].empty()){ while(!kolejki[mymin].empty()){ kolejki[mymin].pop(); } mymin++; } } printf("%lld\n", best_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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | //#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; struct intQueElem{ intQueElem *next; int elem; intQueElem(int element) : elem(element){ next = nullptr; } }; class intQue{ intQueElem *first; intQueElem *last; public: int front(){ if(first == nullptr){ //cerr << "YOU SHOULDN'T BE THERE, SHOO" << endl; return 0; }else{ return first->elem; } } void pop(){ if(first->next == nullptr){ delete first; first = nullptr; last = nullptr; }else{ intQueElem *temp = first->next; delete first; first = temp; } } void push(int element){ if(last == nullptr){ intQueElem *newelem = new intQueElem(element); first = newelem; last = newelem; }else{ last->next = new intQueElem(element); last = last->next; } } bool empty(){ return first == nullptr; } intQue(){ first = nullptr; last = nullptr; } }; int NWD(int a, int b){ int c; while(b != 0){ c = a % b; a = b; b = c; } return a; } int n, m; long long mon[1000001]; char wl[2000001]; int permutacja[2000001]; vector<char> cykle[1000001]; intQue kolejki[4000001]; struct osoba{ int id; long long kasa; bool operator<(const osoba& b) const{ int a_nrp = id % m; int b_nrp = b.id % m; if(permutacja[a_nrp] == permutacja[b_nrp]){ return id < b.id; } return permutacja[a_nrp] < permutacja[b_nrp]; } osoba(int id, long long kasa) : id(id) , kasa(kasa){} }; vector<osoba> osoby; char x; long long best_result = -1LL; int main(){ scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%lld", &mon[i]); osoba os(i, mon[i]); osoby.push_back(os); } scanf("%d\n", &m); for(int i = 0; i < m; ++i){ scanf("%c", &x); if(x == 'W'){ wl[i] = 1; }else{ wl[i] = -1; } } int perm_id = 0; int ile_cykli = NWD(n, m); int dl_cyk = m / ile_cykli; for(int i = 0; i < ile_cykli; ++i){ int k = i; for(int j = 0; j < dl_cyk; ++j){ permutacja[k] = perm_id++; cykle[i].push_back(wl[k]); k += n; k %= m; } } sort(osoby.begin(), osoby.end()); for(int i = 0; i < ile_cykli; ++i){ int minimum = 1; int sumacz = 0; // obliczanie zerowego przesunięcia for(int j = 0; j < dl_cyk; ++j){ sumacz += cykle[i][j]; if(sumacz < minimum){ minimum = sumacz; } kolejki[sumacz + 2 * dl_cyk].push(j + 1); } int ile_w_cyklu = n / ile_cykli; int lider_x = i * ile_w_cyklu; int lider_start = permutacja[osoby[lider_x].id % m]; int current_start = 0; int current_height = 0; int current_sum = sumacz; long long r_per_cyk = (long long)n; // poniżej wyliczamy dla każdego członka cyklu, po ilu ruchach skończą się jego pieniądze. for(int j = 0; j < ile_w_cyklu; ++j){ int osid = i * ile_w_cyklu + j; long long kasa = osoby[osid].kasa; long long ruchy = (long long)osoby[osid].id - (long long)n + 1LL; int przes = permutacja[osoby[osid].id % m] - lider_start; // przesuwanie for(int k = current_start; k < przes; ++k){ current_height += cykle[i][current_start % dl_cyk]; current_sum += cykle[i][current_start % dl_cyk]; current_start += 1; if(current_sum < minimum){ minimum = current_sum; } kolejki[current_sum + 2 * dl_cyk].push(current_start + dl_cyk); kolejki[current_height + 2 * dl_cyk].pop(); if(current_height == minimum && kolejki[current_height + 2 * dl_cyk].empty()){ minimum = current_height + 1; } //cout << current_start << " " << current_height << " " << current_sum << endl; } int true_min = minimum - current_height; if(sumacz >= 0){ // chłopiec zarobi if(kasa <= -true_min){ long long ile_ruchow = kolejki[current_height - kasa + 2 * dl_cyk].front(); ruchy += ile_ruchow * r_per_cyk; if(ruchy < best_result || best_result == -1){ best_result = ruchy; } }else{ ruchy = -1LL; } }else{ // sumacz < 0 int x = 0; long long cykruchy = 0; if(kasa > (-true_min)){ // będzie potrzebny co najmniej jeden cykl kasa += true_min; x = kasa % (-sumacz); cykruchy = kasa / (-sumacz); kasa = -true_min; if(x != 0){ cykruchy += 1; kasa += x + sumacz; } } ruchy += (long long)cykruchy * (long long)r_per_cyk * (long long)dl_cyk; long long ile_ruchow = (long long)kolejki[current_height - kasa + 2 * dl_cyk].front(); ile_ruchow -= (long long)current_start; ruchy += (long long)ile_ruchow * r_per_cyk; if(ruchy < best_result || best_result == -1){ /* if(ruchy == 1607480950){ cout << n << endl; cout << osoby[osid].id << endl; cout << osoby[osid].kasa << endl; cout << i << " " << j << endl; cout << "(" << cykruchy << " * " << dl_cyk << " + " << ile_ruchow << ") * " << r_per_cyk << endl; for(int i = 1; i < 405; ++i){ cout <<"-" << i << ": " << kolejki[current_height - i + 2*dl_cyk].front() - current_start << endl; } }*/ best_result = ruchy; } } } int mymin = minimum + 2 * dl_cyk; while(!kolejki[mymin].empty()){ while(!kolejki[mymin].empty()){ kolejki[mymin].pop(); } mymin++; } } printf("%lld\n", best_result); return 0; } |