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

using namespace std;

struct student {
	int index;
	int position;
};

bool StudentCmp (student lhs, student rhs) {
	return (lhs.index < rhs.index);
}

void FillCircle(student index_student, student index_prev_student, int number_students,
		vector<student>& circle_students) {
	for (int j = 1; j <= number_students; j++) {
		circle_students.push_back(index_student);
		int first_neighbor = FirstNeighbor(index_student.index);
		if (index_prev_student.index != first_neighbor) {
			index_prev_student = index_student;
			index_student = (student){first_neighbor, index_prev_student.position + 1};
		} else {
			index_prev_student = index_student;
			index_student = (student){SecondNeighbor(index_student.index), index_prev_student.position + 1};
		}
	}
}

int main() {

	int number_nodes = NumberOfNodes();
	int my_node = MyNodeId();

	int number_students = NumberOfStudents();

	vector<int> partition_students;
	vector<int> partition_students_prefix;
	partition_students.reserve(number_nodes);
	partition_students_prefix.reserve(number_nodes);
	int size_partition = max(number_students / number_nodes, 1);
	partition_students.push_back(size_partition);
	partition_students_prefix.push_back(size_partition);
	if(number_nodes > 1){
		for(int j = 1; j < number_nodes-1; j++){
			partition_students.push_back(size_partition);
			partition_students_prefix.push_back(partition_students[j] + partition_students_prefix[j-1]);
		}
		partition_students.push_back(number_students - (number_nodes-1)*size_partition);
		partition_students_prefix.push_back(partition_students[number_nodes-1] + partition_students_prefix[number_nodes-2]);
	}

	vector<student>	circle_students;
	circle_students.reserve(partition_students[my_node]);

	student index_student;
	student index_prev_student;
	if(my_node > 0){
		Receive(my_node-1);
		index_student = (student){GetInt(my_node-1), partition_students_prefix[my_node-1]};
		index_prev_student = (student){GetInt(my_node-1), partition_students_prefix[my_node-1] - 1};
	}else{
		index_student = (student){1, 0};
		index_prev_student = (student){SecondNeighbor(index_student.index), -1};
	}

	for (int j = 1; j <= partition_students[my_node]; j++) {
		circle_students.push_back(index_student);
		int first_neighbor = FirstNeighbor(index_student.index);
		if (index_prev_student.index != first_neighbor) {
			index_prev_student = index_student;
			index_student = (student){first_neighbor, index_prev_student.position + 1};
		} else {
			index_prev_student = index_student;
			index_student = (student){SecondNeighbor(index_student.index), index_prev_student.position + 1};
		}
	}

	if(my_node < number_nodes - 1){
		PutInt(my_node + 1, index_student.index);
		PutInt(my_node + 1, index_prev_student.index);
		Send(my_node + 1);
	}

	sort(circle_students.begin(), circle_students.end(), StudentCmp);

	int number_query = NumberOfQueries();
	int* from_results = (int*)malloc(number_query * sizeof(int));
	int* to_results = (int*)malloc(number_query * sizeof(int));

	for (int i = 1; i <= number_query; ++i) {
		int queryFrom = QueryFrom(i);
		int queryTo = QueryTo(i);

		vector<student>::iterator search_resultFrom = lower_bound(circle_students.begin(), circle_students.end(), (student){queryFrom, -1}, StudentCmp);
		vector<student>::iterator search_resultTo = lower_bound(circle_students.begin(), circle_students.end(), (student){queryTo, -1}, StudentCmp);
		if((*search_resultFrom).index == queryFrom){
			PutInt(0, (*search_resultFrom).position);
			PutInt(0, 0);
			PutInt(0, i-1);
			Send(0);
		}
		if((*search_resultTo).index == queryTo){
			PutInt(0, (*search_resultTo).position);
			PutInt(0, 1);
			PutInt(0, i-1);
			Send(0);
		}
	}

	if(my_node == 0){
		for (int i = 1; i <= 2*number_query; ++i){
			int node = Receive(-1);
			int value = GetInt(node);
			int type = GetInt(node);
			int index  = GetInt(node);
			if(type == 0)
				from_results[index] = value;
			else
				to_results[index] = value;
		}
		for (int i = 1; i <= number_query; ++i) {
			int query_time = -1;
			int position_queryFrom = from_results[i-1];
			int position_queryTo = to_results[i-1];

			if(position_queryTo > 0 && position_queryFrom > 0)
				query_time = min(abs(position_queryTo - position_queryFrom), number_students - abs(position_queryTo - position_queryFrom));

			cout << query_time << endl;
		}
	}

	free(from_results);
	free(to_results);

	return 0;
}