#include <cstdlib> #include <cstdio> #include <iostream> #include "krazki.h" #include "message.h" using namespace std; int main() { int nodes = NumberOfNodes(); int id = MyNodeId(); // 0..nodes-1 int n = PipeHeight(); int m = NumberOfDiscs(); int discGroupSize = min(max(n / nodes, 1), m); int discGroups = max(1, m / discGroupSize); int *DA = (int*) calloc(discGroups, sizeof(*DA)); // disc group begin int *DB = (int*) calloc(discGroups, sizeof(*DB)); // disc group end long long int *DM = (long long int*) calloc(discGroups, sizeof(*DM)); // disc group max (or -1 if there is no group) if (id == 0) { DA[0] = 1; DB[0] = discGroupSize; PutInt(nodes-1, DA[0]); PutInt(nodes-1, DB[0]); Send(nodes-1); for (int i = 1; i < discGroups; i++) { DA[i] = DA[i-1] + discGroupSize; DB[i] = i == discGroups - 1 ? m : DB[i-1] + discGroupSize; int node = (i-1) % (nodes-1) + 1; PutInt(node, DA[i]); PutInt(node, DB[i]); Send(node); } Receive(nodes-1); DM[0] = GetLL(nodes-1); //fprintf(stderr, "Received DM[i=%d] = %d from %d\n", 0, DM[0], nodes-1); for (int i = 1; i < discGroups; i++) { int node = (i-1) % (nodes-1) + 1; Receive(node); DM[i] = GetLL(node); //fprintf(stderr, "Received DM[i=%d] = %d from %d\n", i, DM[i], node); } for (int i = 1; i < nodes; i++) { PutInt(i, -1); Send(i); } } else { while (1) { Receive(0); int da = GetInt(0); if (da == -1) { break; } int db = GetInt(0); //fprintf(stderr, "Received request from coord for DM, DA=%d, DB=%d\n", da, db); long long int dm = -1; for (int i = da; i <= db; i++) { dm = max(dm, DiscDiameter(i)); } PutLL(0, dm); Send(0); } } int pipeGroupSize = max(n / nodes, 1); int pipeA = id * pipeGroupSize + 1; int pipeB = id < nodes-1 ? pipeA + pipeGroupSize - 1 : n; int lastNode = pipeB == n ? 1 : 0; // is this the node containing last hole int pipeS = pipeB - pipeA + 1; // pipeA: 2, pipeB: 5, pipeS: 4 (2, 3, 4, 5) if (pipeA > n) { if (id+1 <= nodes-1) { PutInt(id+1, -1); Send(id+1); } Receive(id-1); int x = GetInt(id-1); PutInt(0, n+1); Send(0); return EXIT_SUCCESS; } long long int *H = (long long int*) calloc(pipeS, sizeof(*H)); H[0] = HoleDiameter(pipeA); for (int i = 1; i < pipeS; i++) { H[i] = min(H[i-1], HoleDiameter(pipeA+i)); } //fprintf(stderr, "pipeA: %d, pipeB: %d, lastNode: %d, pipeS: %d, H[pipeL]: %d\n", pipeA, pipeB, lastNode, pipeS, H[pipeS-1]); for (int i = 0; i < pipeS; i++) { H[i] = -H[i]; //cerr << H[i] << endl; } // now H[0..pipeS-1] contains diameters in non-descending order, like: -5, -5, -4, -2, -2 int pipeL = pipeS-1; // index to last pipe element that is unused yet int result = n+1; //cerr << "blabla" << endl; if (id == 0) { for (int i = 0; i < discGroups && DM[i] > 0 && result == n+1; i++) { //cerr << "blabla" << endl; // considering disc group DA[i]..DB[i], whose max diameter is DM[i] if (-H[pipeS-1] >= DM[i] && !lastNode) { // every element can go further, sending it to the next node and waiting for result PutInt(1, DA[i]); PutInt(1, DB[i]); PutLL(1, DM[i]); Send(1); //fprintf(stderr, "sent da=%d, db=%d, dm=%d\n", DA[i], DB[i], DM[i]); Receive(1); int eaten = GetInt(1); // how many has the next one eaten //fprintf(stderr, "eaten by children: %d\n", eaten); DA[i] += eaten; } else { int da = DA[i]; int db = DA[i] - 1; long long dm = DiscDiameter(DA[i]); while (db+1 <= DB[i] && DiscDiameter(db+1) <= -H[pipeS-1]) { db++; dm = max(dm, DiscDiameter(db));; } if (da <= db) { //fprintf(stderr, "sending over just a part\n"); PutInt(1, da); PutInt(1, db); PutLL(1, dm); Send(1); //fprintf(stderr, "sent da=%d, db=%d, dm=%d\n", da, db, dm); Receive(1); int eaten = GetInt(1); // how many has the next one eaten //fprintf(stderr, "eaten by children: %d\n", eaten); DA[i] += eaten; } else { //fprintf(stderr, "not sending da=%d, db=%d, dm=%d\n", DA[i], DB[i], DM[i]); } } // eating the rest (possibly nothing, if everything has been eaten) for (int j = DA[i]; j <= DB[i]; j++) { //cerr << "eating the rest" << endl; long long int dd = DiscDiameter(j); while (pipeL >= 0 && (-H[pipeL]) < dd) { H[pipeL--] = 0; } if (pipeL < 0) { // trying to put disc but no more space. end result = 0 result = 0; //fprintf(stderr, "result = 0\n"); break; } else { H[pipeL--] = 0; } } //fprintf(stderr, "pipeL: %d\n", pipeL); } PutInt(1, -1); Send(1); } else { while (1) { Receive(id - 1); int da = GetInt(id - 1); if (da == -1) { if (id+1 <= nodes-1) { PutInt(id+1, -1); Send(id+1); } PutInt(0, pipeL == pipeS-1 ? n+1 : pipeA + pipeL + 1); Send(0); break; } int db = GetInt(id - 1); long long int dm = GetLL(id - 1); //fprintf(stderr, "Got eating request da=%d, db=%d, dm=%d\n", da, db, dm); if (-H[pipeS-1] >= dm && id + 1 <= nodes-1 && !lastNode) { // every element can go further, sending it to the next node and waiting for result //fprintf(stderr, "Eating in full, notifying parent\n"); PutInt(id-1, db - da + 1); Send(id-1); PutInt(id+1, da); PutInt(id+1, db); PutLL(id+1, dm); Send(id+1); Receive(id+1); int eaten = GetInt(id+1); // how many has the next one eaten //fprintf(stderr, "Child has eaten %d\n", eaten); da += eaten; for (int j = da; j <= db; j++) { // eating the rest (possibly nothing, if everything has been eaten) H[pipeL--] = 0; //fprintf(stderr, "Eating...\n"); } } else { //fprintf(stderr, "Cannot eat in full, tryint to send part and then eat locally\n"); int da2 = da; int db2 = da - 1; long long dm2 = DiscDiameter(da2); int eaten = 0; while (db2+1 <= db && DiscDiameter(db2+1) <= -H[pipeS-1]) { db2++; dm2 = max(dm2, DiscDiameter(db2));; } if (da2 <= db2 && id + 1 <= nodes-1 && !lastNode) { //fprintf(stderr, "sending over just a part\n"); PutInt(id+1, da2); PutInt(id+1, db2); PutLL(id+1, dm2); Send(id+1); //fprintf(stderr, "sent da=%d, db=%d, dm=%d\n", da2, db2, dm2); Receive(id+1); eaten = GetInt(id+1); // how many has the next one eaten //fprintf(stderr, "eaten by children: %d\n", eaten); da += eaten; } else { //fprintf(stderr, "not sending da=%d, db=%d, dm=%d\n", da, db, dm); } //fprintf(stderr, "pipeL: %d, da: %d, db: %d\n", pipeL, da, db); for (int j = da; j <= db; j++) { long long int dd = DiscDiameter(j); while (pipeL >= 0 && (-H[pipeL]) < dd) { //fprintf(stderr, "eating here blocked as DD(j=%d) == %d, and H[pipeL=%d]=%d\n", j, dd, pipeL, H[pipeL]); H[pipeL--] = 0; } if (pipeL < 0) { //fprintf(stderr, "breaking\n"); break; } else { H[pipeL--] = 0; eaten++; } } //fprintf(stderr, "pipeL: %d\n", pipeL); //fprintf(stderr, "Notifying parent about eaten=%d\n", eaten); PutInt(id-1, eaten); Send(id-1); } } } if (id > 0) { return EXIT_SUCCESS; } result = min(result, pipeL == pipeS-1 ? n+1 : pipeA + pipeL + 1); for (int i = 1; i < nodes; i++) { Receive(i); int m = GetInt(i); //cerr << "Result from " << i << " is " << m << endl; result = min(result, m); } cout << result << endl; return EXIT_SUCCESS; }
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 | #include <cstdlib> #include <cstdio> #include <iostream> #include "krazki.h" #include "message.h" using namespace std; int main() { int nodes = NumberOfNodes(); int id = MyNodeId(); // 0..nodes-1 int n = PipeHeight(); int m = NumberOfDiscs(); int discGroupSize = min(max(n / nodes, 1), m); int discGroups = max(1, m / discGroupSize); int *DA = (int*) calloc(discGroups, sizeof(*DA)); // disc group begin int *DB = (int*) calloc(discGroups, sizeof(*DB)); // disc group end long long int *DM = (long long int*) calloc(discGroups, sizeof(*DM)); // disc group max (or -1 if there is no group) if (id == 0) { DA[0] = 1; DB[0] = discGroupSize; PutInt(nodes-1, DA[0]); PutInt(nodes-1, DB[0]); Send(nodes-1); for (int i = 1; i < discGroups; i++) { DA[i] = DA[i-1] + discGroupSize; DB[i] = i == discGroups - 1 ? m : DB[i-1] + discGroupSize; int node = (i-1) % (nodes-1) + 1; PutInt(node, DA[i]); PutInt(node, DB[i]); Send(node); } Receive(nodes-1); DM[0] = GetLL(nodes-1); //fprintf(stderr, "Received DM[i=%d] = %d from %d\n", 0, DM[0], nodes-1); for (int i = 1; i < discGroups; i++) { int node = (i-1) % (nodes-1) + 1; Receive(node); DM[i] = GetLL(node); //fprintf(stderr, "Received DM[i=%d] = %d from %d\n", i, DM[i], node); } for (int i = 1; i < nodes; i++) { PutInt(i, -1); Send(i); } } else { while (1) { Receive(0); int da = GetInt(0); if (da == -1) { break; } int db = GetInt(0); //fprintf(stderr, "Received request from coord for DM, DA=%d, DB=%d\n", da, db); long long int dm = -1; for (int i = da; i <= db; i++) { dm = max(dm, DiscDiameter(i)); } PutLL(0, dm); Send(0); } } int pipeGroupSize = max(n / nodes, 1); int pipeA = id * pipeGroupSize + 1; int pipeB = id < nodes-1 ? pipeA + pipeGroupSize - 1 : n; int lastNode = pipeB == n ? 1 : 0; // is this the node containing last hole int pipeS = pipeB - pipeA + 1; // pipeA: 2, pipeB: 5, pipeS: 4 (2, 3, 4, 5) if (pipeA > n) { if (id+1 <= nodes-1) { PutInt(id+1, -1); Send(id+1); } Receive(id-1); int x = GetInt(id-1); PutInt(0, n+1); Send(0); return EXIT_SUCCESS; } long long int *H = (long long int*) calloc(pipeS, sizeof(*H)); H[0] = HoleDiameter(pipeA); for (int i = 1; i < pipeS; i++) { H[i] = min(H[i-1], HoleDiameter(pipeA+i)); } //fprintf(stderr, "pipeA: %d, pipeB: %d, lastNode: %d, pipeS: %d, H[pipeL]: %d\n", pipeA, pipeB, lastNode, pipeS, H[pipeS-1]); for (int i = 0; i < pipeS; i++) { H[i] = -H[i]; //cerr << H[i] << endl; } // now H[0..pipeS-1] contains diameters in non-descending order, like: -5, -5, -4, -2, -2 int pipeL = pipeS-1; // index to last pipe element that is unused yet int result = n+1; //cerr << "blabla" << endl; if (id == 0) { for (int i = 0; i < discGroups && DM[i] > 0 && result == n+1; i++) { //cerr << "blabla" << endl; // considering disc group DA[i]..DB[i], whose max diameter is DM[i] if (-H[pipeS-1] >= DM[i] && !lastNode) { // every element can go further, sending it to the next node and waiting for result PutInt(1, DA[i]); PutInt(1, DB[i]); PutLL(1, DM[i]); Send(1); //fprintf(stderr, "sent da=%d, db=%d, dm=%d\n", DA[i], DB[i], DM[i]); Receive(1); int eaten = GetInt(1); // how many has the next one eaten //fprintf(stderr, "eaten by children: %d\n", eaten); DA[i] += eaten; } else { int da = DA[i]; int db = DA[i] - 1; long long dm = DiscDiameter(DA[i]); while (db+1 <= DB[i] && DiscDiameter(db+1) <= -H[pipeS-1]) { db++; dm = max(dm, DiscDiameter(db));; } if (da <= db) { //fprintf(stderr, "sending over just a part\n"); PutInt(1, da); PutInt(1, db); PutLL(1, dm); Send(1); //fprintf(stderr, "sent da=%d, db=%d, dm=%d\n", da, db, dm); Receive(1); int eaten = GetInt(1); // how many has the next one eaten //fprintf(stderr, "eaten by children: %d\n", eaten); DA[i] += eaten; } else { //fprintf(stderr, "not sending da=%d, db=%d, dm=%d\n", DA[i], DB[i], DM[i]); } } // eating the rest (possibly nothing, if everything has been eaten) for (int j = DA[i]; j <= DB[i]; j++) { //cerr << "eating the rest" << endl; long long int dd = DiscDiameter(j); while (pipeL >= 0 && (-H[pipeL]) < dd) { H[pipeL--] = 0; } if (pipeL < 0) { // trying to put disc but no more space. end result = 0 result = 0; //fprintf(stderr, "result = 0\n"); break; } else { H[pipeL--] = 0; } } //fprintf(stderr, "pipeL: %d\n", pipeL); } PutInt(1, -1); Send(1); } else { while (1) { Receive(id - 1); int da = GetInt(id - 1); if (da == -1) { if (id+1 <= nodes-1) { PutInt(id+1, -1); Send(id+1); } PutInt(0, pipeL == pipeS-1 ? n+1 : pipeA + pipeL + 1); Send(0); break; } int db = GetInt(id - 1); long long int dm = GetLL(id - 1); //fprintf(stderr, "Got eating request da=%d, db=%d, dm=%d\n", da, db, dm); if (-H[pipeS-1] >= dm && id + 1 <= nodes-1 && !lastNode) { // every element can go further, sending it to the next node and waiting for result //fprintf(stderr, "Eating in full, notifying parent\n"); PutInt(id-1, db - da + 1); Send(id-1); PutInt(id+1, da); PutInt(id+1, db); PutLL(id+1, dm); Send(id+1); Receive(id+1); int eaten = GetInt(id+1); // how many has the next one eaten //fprintf(stderr, "Child has eaten %d\n", eaten); da += eaten; for (int j = da; j <= db; j++) { // eating the rest (possibly nothing, if everything has been eaten) H[pipeL--] = 0; //fprintf(stderr, "Eating...\n"); } } else { //fprintf(stderr, "Cannot eat in full, tryint to send part and then eat locally\n"); int da2 = da; int db2 = da - 1; long long dm2 = DiscDiameter(da2); int eaten = 0; while (db2+1 <= db && DiscDiameter(db2+1) <= -H[pipeS-1]) { db2++; dm2 = max(dm2, DiscDiameter(db2));; } if (da2 <= db2 && id + 1 <= nodes-1 && !lastNode) { //fprintf(stderr, "sending over just a part\n"); PutInt(id+1, da2); PutInt(id+1, db2); PutLL(id+1, dm2); Send(id+1); //fprintf(stderr, "sent da=%d, db=%d, dm=%d\n", da2, db2, dm2); Receive(id+1); eaten = GetInt(id+1); // how many has the next one eaten //fprintf(stderr, "eaten by children: %d\n", eaten); da += eaten; } else { //fprintf(stderr, "not sending da=%d, db=%d, dm=%d\n", da, db, dm); } //fprintf(stderr, "pipeL: %d, da: %d, db: %d\n", pipeL, da, db); for (int j = da; j <= db; j++) { long long int dd = DiscDiameter(j); while (pipeL >= 0 && (-H[pipeL]) < dd) { //fprintf(stderr, "eating here blocked as DD(j=%d) == %d, and H[pipeL=%d]=%d\n", j, dd, pipeL, H[pipeL]); H[pipeL--] = 0; } if (pipeL < 0) { //fprintf(stderr, "breaking\n"); break; } else { H[pipeL--] = 0; eaten++; } } //fprintf(stderr, "pipeL: %d\n", pipeL); //fprintf(stderr, "Notifying parent about eaten=%d\n", eaten); PutInt(id-1, eaten); Send(id-1); } } } if (id > 0) { return EXIT_SUCCESS; } result = min(result, pipeL == pipeS-1 ? n+1 : pipeA + pipeL + 1); for (int i = 1; i < nodes; i++) { Receive(i); int m = GetInt(i); //cerr << "Result from " << i << " is " << m << endl; result = min(result, m); } cout << result << endl; return EXIT_SUCCESS; } |