#include <iostream>
#include "message.h"
#include "kollib.h"
#include <vector>
#include <algorithm>
class Uczen
{
public:
int i;
int p;
};
bool operator < (Uczen a, Uczen b)
{
return a.i < b.i;
}
int bs (std::vector<Uczen> A, int poczatek, int koniec, int szukane)
{
int srodek=(poczatek+koniec)/2;
while (A[srodek].p !=szukane && koniec-poczatek>0)
{
if (A[srodek].p<szukane) {poczatek=srodek+1; srodek=(poczatek+koniec)/2; }
if (A[srodek].p>szukane) {koniec=srodek; srodek=(poczatek+koniec)/2; }
}
if (A[srodek].p ==szukane) return A[srodek].p;
return -1;
}
int main()
{
int n, m, p, prev, cur;
m = NumberOfQueries();
if (m == 0)
return 0;
n = NumberOfStudents();
p = n / NumberOfNodes();
if (MyNodeId() == 0)
{
p += n % NumberOfNodes();
cur = 1;
prev = FirstNeighbor(1);
PutInt(NumberOfNodes() - 1, cur);
PutInt(NumberOfNodes() - 1, prev);
}
std::vector<Uczen> arr(p);
if (MyNodeId() >= 0 && MyNodeId() < NumberOfNodes() / 2)
{
int next;
if (MyNodeId() > 0)
{
Receive(MyNodeId() - 1);
prev = GetInt(MyNodeId() - 1);
cur = GetInt(MyNodeId() - 1);
}
for (int i = 0; i < p; i++)
{
if (FirstNeighbor(cur) == prev)
next = SecondNeighbor(cur);
else
next = FirstNeighbor(cur);
arr[i].i = cur;
arr[i].p = i + p * MyNodeId();
if (MyNodeId() > 0)
arr[i].p += n % NumberOfNodes();
prev = cur;
cur = next;
}
if (FirstNeighbor(cur) == prev)
next = SecondNeighbor(cur);
else
next = FirstNeighbor(cur);
if (MyNodeId() < NumberOfNodes() / 2 - 1)
{
PutInt(MyNodeId() + 1, cur);
PutInt(MyNodeId() + 1, next);
Send(MyNodeId() + 1);
}
std::sort(arr.begin(), arr.end());
//////
}
else
{
int next;
int s;
if (MyNodeId() == NumberOfNodes() - 1)
s = 0;
else
s = MyNodeId() + 1;
Receive(s);
prev = GetInt(s);
cur = GetInt(s);
for (int i = p - 1; i >= 0; i--)
{
if (FirstNeighbor(cur) == prev)
next = SecondNeighbor(cur);
else
next = FirstNeighbor(cur);
arr[i].i = cur;
arr[i]. p = i;
prev = cur;
cur = next;
}
if (FirstNeighbor(cur) == prev)
next = SecondNeighbor(cur);
else
next = FirstNeighbor(cur);
if (MyNodeId() > n / 2)
{
PutInt(MyNodeId() - 1, cur);
PutInt(MyNodeId() - 1, next);
}
std::sort(arr.begin(), arr.end());
}
for (int i = 1; i <= m; i++)
{
int p1, p2, d1, d2;
if (MyNodeId() == 0)
{
for (int j = 1; j < NumberOfNodes(); j++)
{
PutInt(j, i);
Send(j);
}
p1 = bs(arr, 0, p, QueryFrom(i));
p2 = bs(arr, 0, p, QueryTo(i));
if (p1 == -1)
p1 = GetInt(Receive(-1));
if (p2 == -1)
p2 = GetInt(Receive(-1));
if ( p1 > p2)
std::swap(p1, p2);
d1 = p2 - p1;
d2 = n - p2 + p1;
std::cout<< std::min(d1, d2)<<std::endl;
}
else
{
Receive(0);
int j = GetInt(0);
p1 = bs(arr, 0, p, QueryFrom(i));
p2 = bs(arr, 0, p, QueryTo(i));
if (p1 != -1)
{
PutInt(0, p1);
Send(0);
}
if (p2 != -1)
{
PutInt(0, p2);
Send(0);
}
}
}
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 | #include <iostream> #include "message.h" #include "kollib.h" #include <vector> #include <algorithm> class Uczen { public: int i; int p; }; bool operator < (Uczen a, Uczen b) { return a.i < b.i; } int bs (std::vector<Uczen> A, int poczatek, int koniec, int szukane) { int srodek=(poczatek+koniec)/2; while (A[srodek].p !=szukane && koniec-poczatek>0) { if (A[srodek].p<szukane) {poczatek=srodek+1; srodek=(poczatek+koniec)/2; } if (A[srodek].p>szukane) {koniec=srodek; srodek=(poczatek+koniec)/2; } } if (A[srodek].p ==szukane) return A[srodek].p; return -1; } int main() { int n, m, p, prev, cur; m = NumberOfQueries(); if (m == 0) return 0; n = NumberOfStudents(); p = n / NumberOfNodes(); if (MyNodeId() == 0) { p += n % NumberOfNodes(); cur = 1; prev = FirstNeighbor(1); PutInt(NumberOfNodes() - 1, cur); PutInt(NumberOfNodes() - 1, prev); } std::vector<Uczen> arr(p); if (MyNodeId() >= 0 && MyNodeId() < NumberOfNodes() / 2) { int next; if (MyNodeId() > 0) { Receive(MyNodeId() - 1); prev = GetInt(MyNodeId() - 1); cur = GetInt(MyNodeId() - 1); } for (int i = 0; i < p; i++) { if (FirstNeighbor(cur) == prev) next = SecondNeighbor(cur); else next = FirstNeighbor(cur); arr[i].i = cur; arr[i].p = i + p * MyNodeId(); if (MyNodeId() > 0) arr[i].p += n % NumberOfNodes(); prev = cur; cur = next; } if (FirstNeighbor(cur) == prev) next = SecondNeighbor(cur); else next = FirstNeighbor(cur); if (MyNodeId() < NumberOfNodes() / 2 - 1) { PutInt(MyNodeId() + 1, cur); PutInt(MyNodeId() + 1, next); Send(MyNodeId() + 1); } std::sort(arr.begin(), arr.end()); ////// } else { int next; int s; if (MyNodeId() == NumberOfNodes() - 1) s = 0; else s = MyNodeId() + 1; Receive(s); prev = GetInt(s); cur = GetInt(s); for (int i = p - 1; i >= 0; i--) { if (FirstNeighbor(cur) == prev) next = SecondNeighbor(cur); else next = FirstNeighbor(cur); arr[i].i = cur; arr[i]. p = i; prev = cur; cur = next; } if (FirstNeighbor(cur) == prev) next = SecondNeighbor(cur); else next = FirstNeighbor(cur); if (MyNodeId() > n / 2) { PutInt(MyNodeId() - 1, cur); PutInt(MyNodeId() - 1, next); } std::sort(arr.begin(), arr.end()); } for (int i = 1; i <= m; i++) { int p1, p2, d1, d2; if (MyNodeId() == 0) { for (int j = 1; j < NumberOfNodes(); j++) { PutInt(j, i); Send(j); } p1 = bs(arr, 0, p, QueryFrom(i)); p2 = bs(arr, 0, p, QueryTo(i)); if (p1 == -1) p1 = GetInt(Receive(-1)); if (p2 == -1) p2 = GetInt(Receive(-1)); if ( p1 > p2) std::swap(p1, p2); d1 = p2 - p1; d2 = n - p2 + p1; std::cout<< std::min(d1, d2)<<std::endl; } else { Receive(0); int j = GetInt(0); p1 = bs(arr, 0, p, QueryFrom(i)); p2 = bs(arr, 0, p, QueryTo(i)); if (p1 != -1) { PutInt(0, p1); Send(0); } if (p2 != -1) { PutInt(0, p2); Send(0); } } } return 0; } |
English