#include "kollib.h" #include "message.h" #include <vector> #include <iostream> #include <algorithm> using namespace std; vector <int> t; pair <int, int> qu[201]; int bin_search(int x) { int l = 0; int p = t.size() - 1; while(l <= p) { int s = (l + p) / 2; if(t[s] == x) return s; if(t[s] > x) p = s - 1; else l = s + 1; } return -1; } int main() { int n = NumberOfStudents(); int last = 0; int l = (n + NumberOfNodes() - 1) / NumberOfNodes(); int x = 0; if(MyNodeId() == 0) { x = 1; x = FirstNeighbor(x); } else { int instancja = Receive(-1); x = GetInt(instancja); } for(int i = 0; i < l && x != 1; ++i) { t.push_back(x); if(last == FirstNeighbor(x)) { last = x; x = SecondNeighbor(x); } else { last = x; x = FirstNeighbor(x); } } if(MyNodeId() < NumberOfNodes() - 1) { PutInt(MyNodeId() + 1, x); Send(MyNodeId() + 1); } sort(t.begin(), t.end()); int m = NumberOfQueries(); for(int i = 0; i < m; ++i) qu[i].first = qu[i].second = -1; for(int i = 1; i <= m; ++i) { int a = QueryFrom(i); int b = QueryTo(i); int ap = bin_search(a); int bp = bin_search(b); if(ap != -1) { PutInt(0, i); PutInt(0, MyNodeId() * l + ap); Send(0); } if(bp != -1) { PutInt(0, i); PutInt(0, MyNodeId() * l + bp); Send(0); } } if(MyNodeId() == 0) { for(int i = 0; i < 2 * m; ++i) { int instancja = Receive(-1); int nr = GetInt(instancja); int wh = GetInt(instancja); qu[nr].second = wh; if(qu[nr].first == -1) qu[nr].first = qu[nr].second; } for(int i = 0; i < m; ++i) { if(qu[i].first > qu[i].second) swap(qu[i].first, qu[i].second); int wyn = qu[i].second - qu[i].first; wyn = min(wyn, n - qu[i].second + qu[i].first); cout << wyn << '\n'; } } 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 | #include "kollib.h" #include "message.h" #include <vector> #include <iostream> #include <algorithm> using namespace std; vector <int> t; pair <int, int> qu[201]; int bin_search(int x) { int l = 0; int p = t.size() - 1; while(l <= p) { int s = (l + p) / 2; if(t[s] == x) return s; if(t[s] > x) p = s - 1; else l = s + 1; } return -1; } int main() { int n = NumberOfStudents(); int last = 0; int l = (n + NumberOfNodes() - 1) / NumberOfNodes(); int x = 0; if(MyNodeId() == 0) { x = 1; x = FirstNeighbor(x); } else { int instancja = Receive(-1); x = GetInt(instancja); } for(int i = 0; i < l && x != 1; ++i) { t.push_back(x); if(last == FirstNeighbor(x)) { last = x; x = SecondNeighbor(x); } else { last = x; x = FirstNeighbor(x); } } if(MyNodeId() < NumberOfNodes() - 1) { PutInt(MyNodeId() + 1, x); Send(MyNodeId() + 1); } sort(t.begin(), t.end()); int m = NumberOfQueries(); for(int i = 0; i < m; ++i) qu[i].first = qu[i].second = -1; for(int i = 1; i <= m; ++i) { int a = QueryFrom(i); int b = QueryTo(i); int ap = bin_search(a); int bp = bin_search(b); if(ap != -1) { PutInt(0, i); PutInt(0, MyNodeId() * l + ap); Send(0); } if(bp != -1) { PutInt(0, i); PutInt(0, MyNodeId() * l + bp); Send(0); } } if(MyNodeId() == 0) { for(int i = 0; i < 2 * m; ++i) { int instancja = Receive(-1); int nr = GetInt(instancja); int wh = GetInt(instancja); qu[nr].second = wh; if(qu[nr].first == -1) qu[nr].first = qu[nr].second; } for(int i = 0; i < m; ++i) { if(qu[i].first > qu[i].second) swap(qu[i].first, qu[i].second); int wyn = qu[i].second - qu[i].first; wyn = min(wyn, n - qu[i].second + qu[i].first); cout << wyn << '\n'; } } return 0; } |