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
// Jedna instancja wypisuje maksymalny wzrost.

#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"

using namespace std;
#define MAX_NODES 107
typedef long long LL;

int main() {
  int n = GetN();
  int nodes = NumberOfNodes();
  int nr = MyNodeId();
  LL ile[6] = {0};
  LL sum[6] = {0};
  LL lok_inw = 0;
  LL tot_inw = 0;
  for (int i = ((n + nodes - 1)/ nodes) * nr; i < ((n + nodes - 1) / nodes) * (nr + 1) && i < n; ++i) {
      int x = GetElement(i);
      ile[x]++;
      for (int y = 1; y <= 5; y++) {
          if (y > x) {
              lok_inw += ile[y];
          }
      }
  }
  if(MyNodeId() != 0) {
      PutLL(0, lok_inw);
      Send(0);
      for (int i = 1; i <= 5; i++) {
          PutLL(0, ile[i]);
          Send(0);
      }
  }
  else {
      tot_inw += lok_inw;
      for (int i = 1; i <= 5; i++)
          sum[i] = ile[i];
      for (int node = 1; node < nodes; node++) {
          Receive(node);
          LL rec_inw = GetLL(node);
          tot_inw += rec_inw;
          for (int x = 1; x <= 5; x++) {
              Receive(node);
              LL rec_x = GetLL(node);
              for (int y = 1; y <= 5; y++) {
                  if (y > x) {
                      tot_inw += sum[y] * rec_x;
                  }
              }
              sum[x] += rec_x;
          }
      }
      printf("%lld\n", tot_inw);
  }
  return 0;
}