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
#include "kollib.h"
#include "message.h"
#include <iostream>
#include <vector>

using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	if(MyNodeId()==0) {
		const int circleSize = NumberOfStudents();
		vector<int> studentPositions(circleSize+1);
		int last = 1;
		int current =  FirstNeighbor(1);
		int position = 2;
		studentPositions[last] = 1;
		studentPositions[current] = 2;
		while(current != 1) {
			studentPositions[current] = position;
			int next;
			int first = FirstNeighbor(current);
			if(first == last)
				next = SecondNeighbor(current);
			else
				next = first;
			last = current;
			current = next;
			++position;
		}
		for(int i = 1; i <= NumberOfQueries(); ++i) {
			int diff = abs(studentPositions[QueryFrom(i)] - studentPositions[QueryTo(i)]);
			cout << min(diff, circleSize - diff) << endl;
		}
	}
	return 0;
}