//Pawel Kura, zadanie Krazki 2 #include <bits/stdc++.h> #include "krazki.h" #include "message.h" #define WORK 'W' #define FIN 'F' using namespace std; /*int pipe_height, number_of_discs; std::vector<long long int> pipe, discs; int calls; static const timespec iv({1, 0}); */ /*void Init() { static bool initialized = false; if (!initialized) { initialized = true; //std::cin >> pipe_height >> number_of_discs; pipe_height = 3*1e7 + 4; number_of_discs = pipe_height * 3 / 4; pipe.resize(pipe_height); for (int i = 0; i < pipe_height; i++) { std::cin >> pipe[i]; } discs.resize(number_of_discs); for (int i = 0; i < number_of_discs; i++) { std::cin >> discs[i]; } } } */ /* int PipeHeight() { Init(); return pipe_height; } int NumberOfDiscs() { Init(); return number_of_discs; } inline void stop() { return; } long long int HoleDiameter(long long int i) { Init(); //assert(1 <= i && i <= pipe_height); calls++; if (calls % 2000000 == 0) { calls = 0; cerr << "stop\n"; nanosleep(&iv, NULL); } return 2; return pipe[i - 1]; } long long int DiscDiameter(long long int j) { Init(); //assert(1 <= j && j <= number_of_discs); calls++; if (calls % 2000000 == 0) { calls = 0; cerr << "stop\n"; nanosleep(&iv, NULL); } return 1; return discs[j - 1]; }*/ typedef long long LL; const LL LINF = 1000000000000000000LL + 1; /////////////////123456789012345678 int id, p, n, m; vector<int> from, to; vector<LL> my_pipe; LL my_min; void readshit() { id = MyNodeId(); p = NumberOfNodes(); n = PipeHeight(); m = NumberOfDiscs(); if (n <= 10000000 && m <= 10000000) { p = 1; } p = min(25, p); int part_size = (n + p - 1) / p; for (int i = 0; i < p; i++) { from.push_back(i * part_size); to.push_back(min((i + 1) * part_size - 1, n - 1)); if (from[i] > to[i]) { p = i; break; } } } bool work() { Receive(0); char cmd = GetChar(0); if (cmd == FIN) return false; int f = GetLL(0); int t = GetLL(0); int mf = max(f, from[id]); int mt = min(t, to[id]); int ok = 1; for (int i = mf; i <= mt; i++) { int j = i - from[id]; int k = i - f + 1; //z 1..m k = m+1-k; int a = DiscDiameter(k); int b = my_pipe[j]; if (a > b) { ok = 0; break; } } PutLL(0, ok); Send(0); return true; } bool check(int x) { for (int i = 0; i < p; i++) { PutChar(i, WORK); PutLL(i, x); PutLL(i, x + m - 1); Send(i); } work(); int ok = 1; for (int i = 0; i < p; i++) { int r = Receive(-1); int msg = GetLL(r); ok *= msg; } return ok == 1; } void brut() { vector<LL> pipe(n); vector<LL> disc(m); for (int i = 0; i < n; i++) { pipe[i] = HoleDiameter(i + 1); } for (int i = 0; i < m; i++) { disc[i] = DiscDiameter(i + 1); } for (int i = 1; i < n; i++) { pipe[i] = min(pipe[i], pipe[i-1]); } int p = n; for (int i = 0; i < m; i++) { p--; while (p >= 0 && disc[i] > pipe[p]) p--; } cout << max(0, p + 1) << "\n"; } int main() { readshit(); if (id >= p) { return 0; } if (m > n) { if (id == 0) { cout << "0\n"; } return 0; } cerr << "oki\n"; if (p == 1) { brut(); return 0; } my_min = LINF; for (int i = from[id]; i <= to[id]; i++) { my_pipe.push_back(HoleDiameter(i + 1)); my_min = min(my_min, my_pipe.back()); } LL min_recv = LINF; if (id == 0) { if (p > 1) { PutLL(1, my_min); Send(1); } } else { Receive(id - 1); min_recv = GetLL(id - 1); if (id < p - 1) { int min_to_send = min(min_recv, my_min); PutLL(id + 1, min_to_send); Send(id + 1); } } for (auto i = 0u; i < my_pipe.size(); i++) { min_recv = my_pipe[i] = min(my_pipe[i], min_recv); } if (id == 0) { int f = 0; int t = n - m + 1; while (t - f > 1) { int s = (f + t) / 2; if (check(s)) { f = s; } else { t = s; } } if (!check(f)) f--; for (int i = 1; i < p; i++) { PutChar(i, FIN); Send(i); } cout << f + 1 << "\n"; } else { while (work()); } 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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | //Pawel Kura, zadanie Krazki 2 #include <bits/stdc++.h> #include "krazki.h" #include "message.h" #define WORK 'W' #define FIN 'F' using namespace std; /*int pipe_height, number_of_discs; std::vector<long long int> pipe, discs; int calls; static const timespec iv({1, 0}); */ /*void Init() { static bool initialized = false; if (!initialized) { initialized = true; //std::cin >> pipe_height >> number_of_discs; pipe_height = 3*1e7 + 4; number_of_discs = pipe_height * 3 / 4; pipe.resize(pipe_height); for (int i = 0; i < pipe_height; i++) { std::cin >> pipe[i]; } discs.resize(number_of_discs); for (int i = 0; i < number_of_discs; i++) { std::cin >> discs[i]; } } } */ /* int PipeHeight() { Init(); return pipe_height; } int NumberOfDiscs() { Init(); return number_of_discs; } inline void stop() { return; } long long int HoleDiameter(long long int i) { Init(); //assert(1 <= i && i <= pipe_height); calls++; if (calls % 2000000 == 0) { calls = 0; cerr << "stop\n"; nanosleep(&iv, NULL); } return 2; return pipe[i - 1]; } long long int DiscDiameter(long long int j) { Init(); //assert(1 <= j && j <= number_of_discs); calls++; if (calls % 2000000 == 0) { calls = 0; cerr << "stop\n"; nanosleep(&iv, NULL); } return 1; return discs[j - 1]; }*/ typedef long long LL; const LL LINF = 1000000000000000000LL + 1; /////////////////123456789012345678 int id, p, n, m; vector<int> from, to; vector<LL> my_pipe; LL my_min; void readshit() { id = MyNodeId(); p = NumberOfNodes(); n = PipeHeight(); m = NumberOfDiscs(); if (n <= 10000000 && m <= 10000000) { p = 1; } p = min(25, p); int part_size = (n + p - 1) / p; for (int i = 0; i < p; i++) { from.push_back(i * part_size); to.push_back(min((i + 1) * part_size - 1, n - 1)); if (from[i] > to[i]) { p = i; break; } } } bool work() { Receive(0); char cmd = GetChar(0); if (cmd == FIN) return false; int f = GetLL(0); int t = GetLL(0); int mf = max(f, from[id]); int mt = min(t, to[id]); int ok = 1; for (int i = mf; i <= mt; i++) { int j = i - from[id]; int k = i - f + 1; //z 1..m k = m+1-k; int a = DiscDiameter(k); int b = my_pipe[j]; if (a > b) { ok = 0; break; } } PutLL(0, ok); Send(0); return true; } bool check(int x) { for (int i = 0; i < p; i++) { PutChar(i, WORK); PutLL(i, x); PutLL(i, x + m - 1); Send(i); } work(); int ok = 1; for (int i = 0; i < p; i++) { int r = Receive(-1); int msg = GetLL(r); ok *= msg; } return ok == 1; } void brut() { vector<LL> pipe(n); vector<LL> disc(m); for (int i = 0; i < n; i++) { pipe[i] = HoleDiameter(i + 1); } for (int i = 0; i < m; i++) { disc[i] = DiscDiameter(i + 1); } for (int i = 1; i < n; i++) { pipe[i] = min(pipe[i], pipe[i-1]); } int p = n; for (int i = 0; i < m; i++) { p--; while (p >= 0 && disc[i] > pipe[p]) p--; } cout << max(0, p + 1) << "\n"; } int main() { readshit(); if (id >= p) { return 0; } if (m > n) { if (id == 0) { cout << "0\n"; } return 0; } cerr << "oki\n"; if (p == 1) { brut(); return 0; } my_min = LINF; for (int i = from[id]; i <= to[id]; i++) { my_pipe.push_back(HoleDiameter(i + 1)); my_min = min(my_min, my_pipe.back()); } LL min_recv = LINF; if (id == 0) { if (p > 1) { PutLL(1, my_min); Send(1); } } else { Receive(id - 1); min_recv = GetLL(id - 1); if (id < p - 1) { int min_to_send = min(min_recv, my_min); PutLL(id + 1, min_to_send); Send(id + 1); } } for (auto i = 0u; i < my_pipe.size(); i++) { min_recv = my_pipe[i] = min(my_pipe[i], min_recv); } if (id == 0) { int f = 0; int t = n - m + 1; while (t - f > 1) { int s = (f + t) / 2; if (check(s)) { f = s; } else { t = s; } } if (!check(f)) f--; for (int i = 1; i < p; i++) { PutChar(i, FIN); Send(i); } cout << f + 1 << "\n"; } else { while (work()); } return 0; } |