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
// Nikodem "nixon" Kramarz
// zad. "Kolko informatyczne" PA 2014

#include "kollib.h"
#include "message.h"
#include <iostream>
#include <algorithm>
#include <utility>
#define AKT_N 100000005
using namespace std;
int n, il_inst, akt_inst, start;
pair <int, int> students[AKT_N];

int main()
{
    n = NumberOfStudents();
    il_inst = NumberOfNodes();
    akt_inst = MyNodeId();

    int id, licznik = 2;

    if(akt_inst != 0)
    {
        start = akt_inst * (n / il_inst);

        students[1] = make_pair(SecondNeighbor(FirstNeighbor(start+1)), 1);
        int akt = FirstNeighbor(start+1);
        for(int i=start+1; i<=start + max(n % il_inst, (n / il_inst)) && i<=n; i++)
        {
            id = i+1;
            students[licznik] = make_pair(akt, id);
            akt = FirstNeighbor(akt);
            licznik++;
        }
    
        sort(students+1, students + licznik - 1);
    }

    if(akt_inst == 0)
    {
        int m = NumberOfQueries();
        for(int k=1; k<=m; k++)
        {
            int from = QueryFrom(k);
            int to = QueryTo(k);
            int from_id, to_id;
            for(int i=1; i<il_inst; i++)
            {
                PutInt(i, from);
                PutInt(i, to);
                Send(i);
                Receive(i);
                from_id = GetInt(i);
                to_id = GetInt(i);
                if(from_id != -1 && to_id != -1)
                {
                    int res = min(abs(from_id - to_id), min(to_id, from_id) + n - max(to_id, from_id));
                    cout << res << "\n";
                    break;
                }
            }
        }
    }
    else
    {
        Receive(akt_inst);
        int from = GetInt(akt_inst);
        int to = GetInt(akt_inst);

        pair<int, int> pocz = *lower_bound(students + 1, students + licznik - 1, make_pair(from, 0));
        pair<int, int> kon = *lower_bound(students + 1, students + licznik - 1, make_pair(to, 0));

        if(students[pocz.first].first == from)
            PutInt(0, students[pocz.first].second);
        else
            PutInt(0, -1);
        if(students[kon.first].first == to)
            PutInt(0, students[kon.first].second);
        else
            PutInt(0, -1);
        Send(0);
    }
}