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

#include <iostream>
#include <cmath>
#include <algorithm>

int main() {
	const int n = GetN();

	long long wrangles = 0;

	int consistentRowsPerWorker = ceil((n-1.0) / (NumberOfNodes() * 2));

	for (int row = MyNodeId()*consistentRowsPerWorker; row < std::min((n-1)/2+1,(MyNodeId()+1)*consistentRowsPerWorker); ++row) {
		int height = GetElement(row);
		
		for (int row2 = row + 1; row2 < n; ++row2) {
			if (height> GetElement(row2)) {
				++wrangles;
			}
		}
	}
	for (int row = n-2-MyNodeId()*consistentRowsPerWorker; row > std::max((n - 1) / 2, n - 2 - (MyNodeId() + 1)*consistentRowsPerWorker); --row) {
		int height = GetElement(row);

		for (int row2 = row + 1; row2 < n; ++row2) {
			if (height > GetElement(row2)) {
				++wrangles;
			}
		}
	}

	if (MyNodeId() > 0) {
		PutLL(0, wrangles);
		Send(0);
	}
	else {  // MyNodeId == 0
		for (int instance = 1; instance < NumberOfNodes(); ++instance) {
			Receive(instance);
			wrangles += GetLL(instance);
		}
		std::cout << wrangles << std::endl;
	}
}