#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <cmath> #include <string> #include <cstring> #include <unordered_map> #include "kollib.h" #include "message.h" using namespace std; typedef long long int LL; typedef long double LD; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<LD> VLD; typedef vector<string> VS; typedef pair<int, int> PII; typedef vector<PII> VPII; const int INF = 1000000001; const LD EPS = 10e-9; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define abs(a) ((a)<0 ? -(a) : (a)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) const int MAX_SINGLE = 1000005; const int MAX_Q = 500; struct queryAnswer { int query, student, pos, to; queryAnswer(int q = 0, int s = 0, int p = 0, int t = 0) : query(q), student(s), pos(p), to(t) {}; }; bool operator<(const queryAnswer &a, const queryAnswer &b) { if(a.query == b.query) { if(a.student == b.student) return a.to < b.to; return a.student < b.student; } return a.query < b.query; } void propagate(int target, int student, int lastStudent, int pos) { PutInt(target, student); PutInt(target, lastStudent); PutInt(target, pos); Send(target); } /* funkcje dla mastera (node == 0) */ void queryNode(int target, int x) { PutInt(target, x); Send(target); } void waitNodesReady() { int s1 = Receive(-1); int m1 = GetInt(s1); int s2 = Receive(-1); int m2 = GetInt(s2); } void closeNode(int target) { PutInt(target, -1); Send(target); } queryAnswer getAnswer() { int s = Receive(-1); queryAnswer res; res.query = GetInt(s); res.student = GetInt(s); res.pos = GetInt(s); res.to = GetInt(s); return res; } /* ----------------------------- */ /* funkcje dla reszty (node > 0) */ void getStartElem(int &student, int &last, int &pos) { int s = Receive(-1); student = GetInt(s); last = GetInt(s); pos = GetInt(s); } void reportReady() { PutInt(0, -1); Send(0); } int getMessage() { Receive(0); return GetInt(0); } void sendAnswer(const queryAnswer &qa) { PutInt(0, qa.query); PutInt(0, qa.student); PutInt(0, qa.pos); PutInt(0, qa.to); Send(0); } /* ----------------------------- */ int main() { int n = NumberOfStudents(); if(n < MAX_SINGLE) { if(MyNodeId() == 0) { //cout << "1" << endl; int last, last2 = 1; int *pos = new int[n+5]; pos[1] = 0; last = FirstNeighbor(1); pos[last] = 1; //cout << "2" << endl; FOR(i, 2, n-1) { int a = FirstNeighbor(last); if(a == last2) { a = SecondNeighbor(last); } pos[a] = i; last2 = last; last = a; } //cout << "3" << endl; int qc = NumberOfQueries(); REP(i, qc) { int x = QueryFrom(i+1); int y = QueryTo(i+1); int d1 = abs(pos[x]-pos[y]); cout << min(d1, n-d1) << endl; } //cout << "4" << endl; delete [] pos; } return 0; } int nodes = NumberOfNodes(); int studentsPerNode = n/nodes; int myNode = MyNodeId(); // master comp :) if(myNode == 0) { //cout << "master start" << endl; int lastRight = nodes/2; int myStudents = n%nodes; if(myStudents == 0) { myStudents = studentsPerNode; } int actPos = 0; int last, last2 = 1; last = SecondNeighbor(1); unordered_map<int, int> pos; pos[1] = 0; // nastepny fragment po 'prawej' propagate(1, FirstNeighbor(1), 1, 1); //cout << "wypuszczono na prawo" << endl; FOR(i, 1, myStudents-1) { pos[last] = actPos = -i; //cout << "mstudent: " << last << ", pos:" << pos[last] << endl; int next = FirstNeighbor(last); if(next == last2) { next = SecondNeighbor(last); } last2 = last; last = next; } // nastepny fragment po 'lewej' propagate(nodes-1, last, last2, actPos-1); //cout << "wypuszczono na prawo" << endl; //waitNodesReady(); // odpowiadanie na te zapytania, na ktore moze queryAnswer qAns[MAX_Q]; int qc = NumberOfQueries(); int qAnsC = 0; //cout << "main q: "<< qc << endl; REP(i, qc) { //cout << "main q_i: " << i << endl; int x = QueryFrom(i+1); int y = QueryTo(i+1); int posX, posY; unordered_map<int, int>::iterator pX = pos.find(x); if(pX != pos.end()) { //cout << "mx: " << x << ", pos: " << pX->second << endl; posX = pX->second; qAns[qAnsC] = queryAnswer(i, x, posX, 0); qAnsC++; } unordered_map<int, int>::iterator pY = pos.find(y); if(pY != pos.end()) { //cout << "my: " << y << ", pos: " << pY->second << endl; posY = pY->second; qAns[qAnsC] = queryAnswer(i, y, posY, 1); qAnsC++; } //int d1 = abs(posY-posX); //cout << min(d1, n-d1) << endl; } // zbieranie reszty odpowiedzi //cout << "master zaczyna zbierac odpowiedzi" << endl; while(qAnsC < 2*qc) { //cout << "qAnsC na wejsciu: " << qAnsC << ", 2*qc: " << 2*qc << endl; qAns[qAnsC] = getAnswer(); //cout << "query: " << qAns[qAnsC].query << ", student: " << qAns[qAnsC].student << ", pos: " << qAns[qAnsC].pos << ", to: " << qAns[qAnsC].to << endl; qAnsC++; //cout << "qAnsC: " << qAnsC << ", 2*qc: " << 2*qc << endl; } //cout << "qAnsC po petli: " << qAnsC << ", 2*q: " << 2*qc << endl; //cout << "master zaczyna odpowiadac" << endl; sort(qAns, qAns+qAnsC); // i odpowiadanie na zapytania for(int i = 0; i < 2*qc; i += 2) { //cout << "q[i]: " << qAns[i].query << ", pos: " << qAns[i].pos << ", q[i+1]: " << qAns[i+1].query << ", pos: " << qAns[i+1].pos << endl; int d1 = abs(qAns[i].pos-qAns[i+1].pos); cout << min(d1, n-d1) << endl; } return 0; // wylaczanie reszty instancji /*FOR(i, 1, nodes-1) { closeNode(i); }*/ } else { // przetworzenie swojego przedzialu unordered_map<int, int> pos; int student, last, actPos; getStartElem(student, last, actPos); REP(i, studentsPerNode) { if(abs(actPos) > n-abs(actPos)) { pos[student] = n-abs(actPos); } else pos[student] = actPos; //cout << "node: " << myNode << ", student: " << student << ", pos: " << pos[student] << endl; int next = FirstNeighbor(student); if(next == last) { next = SecondNeighbor(student); } last = student; student = next; if(actPos < 0) actPos--; else actPos++; } if(myNode < nodes/2) { propagate(myNode+1, student, last, actPos); } else if(myNode > (nodes/2)+1) { propagate(myNode-1, student, last, actPos); } else { // koncowe instancje zglaszaja gotowosc // reportReady(); } //cout << "zrobione przedzialy przez node: " << myNode << endl; /*int msg; while(msg = getMessage()) { // koniec pracy :) if(msg == -1) { return 0; } // obrobka zapytania unordered_map<int, int>::iterator pX = pos.find(msg); if(pX != pos.end()) { answerQuery(pX->second); } }*/ // odpowiadanie na zapytania na ktore moze int qc = NumberOfQueries(); //cout << "q: "<< qc << endl; REP(i, qc) { //cout << "q_i: " << i << endl; int x = QueryFrom(i+1); int y = QueryTo(i+1); int posX, posY; unordered_map<int, int>::iterator pX = pos.find(x); if(pX != pos.end()) { //cout << "x: " << x << ", pos: " << pX->second << endl; sendAnswer(queryAnswer(i, x, pX->second, 0)); } unordered_map<int, int>::iterator pY = pos.find(y); if(pY != pos.end()) { //cout << "y: " << y << ", pos: " << pY->second << endl; sendAnswer(queryAnswer(i, y, pY->second, 1)); } //int d1 = abs(posY-posX); //cout << min(d1, n-d1) << endl; } //cout << "koniec node: " << myNode << endl; //system("pause"); // koniec :) return 0; } //system("pause"); 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <list> #include <queue> #include <stack> #include <map> #include <set> #include <cmath> #include <string> #include <cstring> #include <unordered_map> #include "kollib.h" #include "message.h" using namespace std; typedef long long int LL; typedef long double LD; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<LD> VLD; typedef vector<string> VS; typedef pair<int, int> PII; typedef vector<PII> VPII; const int INF = 1000000001; const LD EPS = 10e-9; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define abs(a) ((a)<0 ? -(a) : (a)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) const int MAX_SINGLE = 1000005; const int MAX_Q = 500; struct queryAnswer { int query, student, pos, to; queryAnswer(int q = 0, int s = 0, int p = 0, int t = 0) : query(q), student(s), pos(p), to(t) {}; }; bool operator<(const queryAnswer &a, const queryAnswer &b) { if(a.query == b.query) { if(a.student == b.student) return a.to < b.to; return a.student < b.student; } return a.query < b.query; } void propagate(int target, int student, int lastStudent, int pos) { PutInt(target, student); PutInt(target, lastStudent); PutInt(target, pos); Send(target); } /* funkcje dla mastera (node == 0) */ void queryNode(int target, int x) { PutInt(target, x); Send(target); } void waitNodesReady() { int s1 = Receive(-1); int m1 = GetInt(s1); int s2 = Receive(-1); int m2 = GetInt(s2); } void closeNode(int target) { PutInt(target, -1); Send(target); } queryAnswer getAnswer() { int s = Receive(-1); queryAnswer res; res.query = GetInt(s); res.student = GetInt(s); res.pos = GetInt(s); res.to = GetInt(s); return res; } /* ----------------------------- */ /* funkcje dla reszty (node > 0) */ void getStartElem(int &student, int &last, int &pos) { int s = Receive(-1); student = GetInt(s); last = GetInt(s); pos = GetInt(s); } void reportReady() { PutInt(0, -1); Send(0); } int getMessage() { Receive(0); return GetInt(0); } void sendAnswer(const queryAnswer &qa) { PutInt(0, qa.query); PutInt(0, qa.student); PutInt(0, qa.pos); PutInt(0, qa.to); Send(0); } /* ----------------------------- */ int main() { int n = NumberOfStudents(); if(n < MAX_SINGLE) { if(MyNodeId() == 0) { //cout << "1" << endl; int last, last2 = 1; int *pos = new int[n+5]; pos[1] = 0; last = FirstNeighbor(1); pos[last] = 1; //cout << "2" << endl; FOR(i, 2, n-1) { int a = FirstNeighbor(last); if(a == last2) { a = SecondNeighbor(last); } pos[a] = i; last2 = last; last = a; } //cout << "3" << endl; int qc = NumberOfQueries(); REP(i, qc) { int x = QueryFrom(i+1); int y = QueryTo(i+1); int d1 = abs(pos[x]-pos[y]); cout << min(d1, n-d1) << endl; } //cout << "4" << endl; delete [] pos; } return 0; } int nodes = NumberOfNodes(); int studentsPerNode = n/nodes; int myNode = MyNodeId(); // master comp :) if(myNode == 0) { //cout << "master start" << endl; int lastRight = nodes/2; int myStudents = n%nodes; if(myStudents == 0) { myStudents = studentsPerNode; } int actPos = 0; int last, last2 = 1; last = SecondNeighbor(1); unordered_map<int, int> pos; pos[1] = 0; // nastepny fragment po 'prawej' propagate(1, FirstNeighbor(1), 1, 1); //cout << "wypuszczono na prawo" << endl; FOR(i, 1, myStudents-1) { pos[last] = actPos = -i; //cout << "mstudent: " << last << ", pos:" << pos[last] << endl; int next = FirstNeighbor(last); if(next == last2) { next = SecondNeighbor(last); } last2 = last; last = next; } // nastepny fragment po 'lewej' propagate(nodes-1, last, last2, actPos-1); //cout << "wypuszczono na prawo" << endl; //waitNodesReady(); // odpowiadanie na te zapytania, na ktore moze queryAnswer qAns[MAX_Q]; int qc = NumberOfQueries(); int qAnsC = 0; //cout << "main q: "<< qc << endl; REP(i, qc) { //cout << "main q_i: " << i << endl; int x = QueryFrom(i+1); int y = QueryTo(i+1); int posX, posY; unordered_map<int, int>::iterator pX = pos.find(x); if(pX != pos.end()) { //cout << "mx: " << x << ", pos: " << pX->second << endl; posX = pX->second; qAns[qAnsC] = queryAnswer(i, x, posX, 0); qAnsC++; } unordered_map<int, int>::iterator pY = pos.find(y); if(pY != pos.end()) { //cout << "my: " << y << ", pos: " << pY->second << endl; posY = pY->second; qAns[qAnsC] = queryAnswer(i, y, posY, 1); qAnsC++; } //int d1 = abs(posY-posX); //cout << min(d1, n-d1) << endl; } // zbieranie reszty odpowiedzi //cout << "master zaczyna zbierac odpowiedzi" << endl; while(qAnsC < 2*qc) { //cout << "qAnsC na wejsciu: " << qAnsC << ", 2*qc: " << 2*qc << endl; qAns[qAnsC] = getAnswer(); //cout << "query: " << qAns[qAnsC].query << ", student: " << qAns[qAnsC].student << ", pos: " << qAns[qAnsC].pos << ", to: " << qAns[qAnsC].to << endl; qAnsC++; //cout << "qAnsC: " << qAnsC << ", 2*qc: " << 2*qc << endl; } //cout << "qAnsC po petli: " << qAnsC << ", 2*q: " << 2*qc << endl; //cout << "master zaczyna odpowiadac" << endl; sort(qAns, qAns+qAnsC); // i odpowiadanie na zapytania for(int i = 0; i < 2*qc; i += 2) { //cout << "q[i]: " << qAns[i].query << ", pos: " << qAns[i].pos << ", q[i+1]: " << qAns[i+1].query << ", pos: " << qAns[i+1].pos << endl; int d1 = abs(qAns[i].pos-qAns[i+1].pos); cout << min(d1, n-d1) << endl; } return 0; // wylaczanie reszty instancji /*FOR(i, 1, nodes-1) { closeNode(i); }*/ } else { // przetworzenie swojego przedzialu unordered_map<int, int> pos; int student, last, actPos; getStartElem(student, last, actPos); REP(i, studentsPerNode) { if(abs(actPos) > n-abs(actPos)) { pos[student] = n-abs(actPos); } else pos[student] = actPos; //cout << "node: " << myNode << ", student: " << student << ", pos: " << pos[student] << endl; int next = FirstNeighbor(student); if(next == last) { next = SecondNeighbor(student); } last = student; student = next; if(actPos < 0) actPos--; else actPos++; } if(myNode < nodes/2) { propagate(myNode+1, student, last, actPos); } else if(myNode > (nodes/2)+1) { propagate(myNode-1, student, last, actPos); } else { // koncowe instancje zglaszaja gotowosc // reportReady(); } //cout << "zrobione przedzialy przez node: " << myNode << endl; /*int msg; while(msg = getMessage()) { // koniec pracy :) if(msg == -1) { return 0; } // obrobka zapytania unordered_map<int, int>::iterator pX = pos.find(msg); if(pX != pos.end()) { answerQuery(pX->second); } }*/ // odpowiadanie na zapytania na ktore moze int qc = NumberOfQueries(); //cout << "q: "<< qc << endl; REP(i, qc) { //cout << "q_i: " << i << endl; int x = QueryFrom(i+1); int y = QueryTo(i+1); int posX, posY; unordered_map<int, int>::iterator pX = pos.find(x); if(pX != pos.end()) { //cout << "x: " << x << ", pos: " << pX->second << endl; sendAnswer(queryAnswer(i, x, pX->second, 0)); } unordered_map<int, int>::iterator pY = pos.find(y); if(pY != pos.end()) { //cout << "y: " << y << ", pos: " << pY->second << endl; sendAnswer(queryAnswer(i, y, pY->second, 1)); } //int d1 = abs(posY-posX); //cout << min(d1, n-d1) << endl; } //cout << "koniec node: " << myNode << endl; //system("pause"); // koniec :) return 0; } //system("pause"); return 0; } |