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
#include "kollib.h"
#include "message.h"
#include <cstdio>
#include <algorithm>
const int QUERIES = 200;

std::pair<int, int> needed[QUERIES];
int neededSize;

int main() {
    if (MyNodeId() != 0)
        return 0;

    for (int i=1; i<=NumberOfQueries(); i++) {
        needed[neededSize++] = { QueryFrom(i), 0 };
        needed[neededSize++] = { QueryTo(i), 0 };
    }
    std::sort(needed, needed+neededSize);

    int current = 1, prev = -1, known = 0, d = 0;
    while (known < neededSize) {
        auto it = std::lower_bound(needed, needed+neededSize, std::make_pair(current, 0));
        if (it->first == current)
            it->second = d, known++;
        
        int tmp = current;
        if (FirstNeighbor(current) == prev)
            current = SecondNeighbor(current);
        else
            current = FirstNeighbor(current);
        prev = tmp;
        d++;
    }

    for (int i=1; i<=NumberOfQueries(); i++) {
        auto it1 = std::lower_bound(needed, needed+neededSize, std::make_pair(QueryFrom(i), 0));
        auto it2 = std::lower_bound(needed, needed+neededSize, std::make_pair(QueryTo(i), 0));
        
        int d = std::abs(it2->second-it1->second);
        printf("%d\n", std::min(d, NumberOfStudents()-d));
    }

}