/* * kol.cpp * * Created on: 16 maj 2014 * Author: tadif */ #include "message.h" #include "kollib.h" #include <stdlib.h> #include <iostream> #include <math.h> using namespace std; int QueryIndexLow(int node) { double d = NumberOfQueries(); int queryPerNode = ceil(d / NumberOfNodes()); return node * queryPerNode + 1; } int QueryIndexHigh(int node) { double d = NumberOfQueries(); int queryPerNode = ceil(d / NumberOfNodes()); int queryIndexHigh = (node + 1) * queryPerNode; if (queryIndexHigh > NumberOfQueries()) { queryIndexHigh = NumberOfQueries(); } return queryIndexHigh; } int main() { int queryIndexLow = QueryIndexLow(MyNodeId()); int queryIndexHigh = QueryIndexHigh(MyNodeId()); int* queryFrom = (int*) malloc((NumberOfQueries() + 1) * sizeof(int)); int* queryTo = (int*) malloc((NumberOfQueries() + 1) * sizeof(int)); int* queryPositionFrom = (int*) malloc( (NumberOfQueries() + 1) * sizeof(int)); int* queryPositionTo = (int*) malloc((NumberOfQueries() + 1) * sizeof(int)); for (int i = queryIndexLow; i <= queryIndexHigh; i++) { queryFrom[i] = QueryFrom(i); queryTo[i] = QueryTo(i); } int previous = 0; int current = 1; int n = NumberOfStudents(); for (int j = 1; j <= n; j++) { for (int i = queryIndexLow; i <= queryIndexHigh; i++) { if (queryFrom[i] == current) { queryPositionFrom[i] = j; } if (queryTo[i] == current) { queryPositionTo[i] = j; } } int x = FirstNeighbor(current); if (previous == x) { x = SecondNeighbor(current); } previous = current; current = x; } for (int i = queryIndexLow; i <= queryIndexHigh; i++) { int time = abs(queryPositionFrom[i] - queryPositionTo[i]); if (time + time > n) { time = n - time; } if (MyNodeId() > 0) { PutInt(0, time); } else { cout << time << endl; } } if (MyNodeId() > 0) { Send(0); } else { for (int node = 1; node < NumberOfNodes(); node++) { Receive(node); queryIndexLow = QueryIndexLow(node); queryIndexHigh = QueryIndexHigh(node); for (int i = queryIndexLow; i <= queryIndexHigh; i++) { int time = GetInt(node); cout << time << endl; } } } 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 | /* * kol.cpp * * Created on: 16 maj 2014 * Author: tadif */ #include "message.h" #include "kollib.h" #include <stdlib.h> #include <iostream> #include <math.h> using namespace std; int QueryIndexLow(int node) { double d = NumberOfQueries(); int queryPerNode = ceil(d / NumberOfNodes()); return node * queryPerNode + 1; } int QueryIndexHigh(int node) { double d = NumberOfQueries(); int queryPerNode = ceil(d / NumberOfNodes()); int queryIndexHigh = (node + 1) * queryPerNode; if (queryIndexHigh > NumberOfQueries()) { queryIndexHigh = NumberOfQueries(); } return queryIndexHigh; } int main() { int queryIndexLow = QueryIndexLow(MyNodeId()); int queryIndexHigh = QueryIndexHigh(MyNodeId()); int* queryFrom = (int*) malloc((NumberOfQueries() + 1) * sizeof(int)); int* queryTo = (int*) malloc((NumberOfQueries() + 1) * sizeof(int)); int* queryPositionFrom = (int*) malloc( (NumberOfQueries() + 1) * sizeof(int)); int* queryPositionTo = (int*) malloc((NumberOfQueries() + 1) * sizeof(int)); for (int i = queryIndexLow; i <= queryIndexHigh; i++) { queryFrom[i] = QueryFrom(i); queryTo[i] = QueryTo(i); } int previous = 0; int current = 1; int n = NumberOfStudents(); for (int j = 1; j <= n; j++) { for (int i = queryIndexLow; i <= queryIndexHigh; i++) { if (queryFrom[i] == current) { queryPositionFrom[i] = j; } if (queryTo[i] == current) { queryPositionTo[i] = j; } } int x = FirstNeighbor(current); if (previous == x) { x = SecondNeighbor(current); } previous = current; current = x; } for (int i = queryIndexLow; i <= queryIndexHigh; i++) { int time = abs(queryPositionFrom[i] - queryPositionTo[i]); if (time + time > n) { time = n - time; } if (MyNodeId() > 0) { PutInt(0, time); } else { cout << time << endl; } } if (MyNodeId() > 0) { Send(0); } else { for (int node = 1; node < NumberOfNodes(); node++) { Receive(node); queryIndexLow = QueryIndexLow(node); queryIndexHigh = QueryIndexHigh(node); for (int i = queryIndexLow; i <= queryIndexHigh; i++) { int time = GetInt(node); cout << time << endl; } } } return 0; } |