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

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>

using namespace std;


void step(int &student, int &next) {
  int fn = FirstNeighbor(next);
  if(student == fn) {
    student = next;
    next = SecondNeighbor(next);
  } else {
    student = next;
    next = fn;
  }
}


int randInt(int const &a, int const &b) {
  if(b < a) return randInt(b, a);
  unsigned int r = rand() ^ (((unsigned int) rand()) << 16);
  unsigned int l = 0xffffffff;
  unsigned int m = b - a + 1;
  if(r > (l / m) * m) {
    return randInt(a, b);
  } else {
    return a + r % m;
  }
}


int solve() {
  srand(0xa50571fb);
  set<int> requests;
  
  int n = NumberOfStudents();
  int q = NumberOfQueries();
  
  for(int i = 1; i <= q; ++i) {
    requests.insert(QueryFrom(i));
    requests.insert(QueryTo(i));
  }
  
  int firstStudent = *(requests.begin());
  {
    int r = randInt(0, requests.size() - 1);
    set<int>::iterator it = requests.begin();
    for(int i = 0; i < r; ++i) {
      ++it;
    }
    firstStudent = *it;
  }
  
  map<int, int> positionOf;
  
  positionOf[firstStudent] = 0;
  
  int student = firstStudent;
  int next = randInt(0, 1) ? FirstNeighbor(student) : SecondNeighbor(student);
  int pos = 0;
  int todo = requests.size() - 1;
  
  while(todo) {
    step(student, next);
    ++pos;
    
    if(requests.find(student) != requests.end()) {
      positionOf[student] = pos;
      --todo;
    }
  }
  
  for(int i = 1; i <= q; ++i) {
    int p0 = positionOf[QueryFrom(i)];
    int p1 = positionOf[QueryTo(i)];
    int d = abs(p0 - p1);
    d = min(d, n - d);
    printf("%d\n", d);
  }
  
  return EXIT_SUCCESS;
}

int main() {
  
  if(MyNodeId() == 0) {
    solve();
  } else {
    fprintf(stderr, "Ojtam ojtam, jedna instancja da rade.\n");
  }
  
  return EXIT_SUCCESS;
}