// Jedna instancja wypisuje maksymalny wzrost.
#include "message.h"
#include "teatr.h"
#include "bits/stdc++.h"
using namespace std;
long long* count(int begin, int end) {
int n, j;
long long counts[] = {0, 0, 0, 0, 0, 0};
for(int i = begin; i < end; i++) {
n = GetElement(i) - 1; //normalize 1-N to 0-(N-1)
++counts[n];
for(j=n+1; j<5; j++) {
counts[5] += counts[j];
}
}
return counts;
}
int main() {
int NodeId = MyNodeId();
int NodesCount = NumberOfNodes();
int N = GetN();
if (N <= 1000000) {
if (NodeId != 0) return 0; // exit another instances
// n is small, compute on one instance
long long *counts = count(0, N);
cout << counts[5] << endl;
return 0;
}
// n is big:
int begin = (int)((long long)N * (long long)NodeId / NodesCount);
int end = (int)((long long)N * (long long)(NodeId+1) / NodesCount);
long long *counts = count(begin, end);
long long prev[] = {0, 0, 0, 0, 0, 0};
// wait for previous
if(NodeId > 0) {
Receive(NodeId-1);
for(int i = 0; i < 6; i++) {
prev[i] = GetLL(NodeId-1);
if (i < 5) {
for(int j = 0; j < i; j++) {
counts[5] += (prev[i]) * (counts[j]);
}
}
}
}
for (int i = 0; i < 6; i++) {
counts[i] += prev[i];
}
// send to next nodes
if (NodeId < (NodesCount-1)) {
for(int i = 0; i < 6; i++) {
PutLL(NodeId+1, counts[i]);
}
Send(NodeId+1);
} else {
// last instance, print result
cout << counts[5] << endl;
}
return 0;
}
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 | // Jedna instancja wypisuje maksymalny wzrost. #include "message.h" #include "teatr.h" #include "bits/stdc++.h" using namespace std; long long* count(int begin, int end) { int n, j; long long counts[] = {0, 0, 0, 0, 0, 0}; for(int i = begin; i < end; i++) { n = GetElement(i) - 1; //normalize 1-N to 0-(N-1) ++counts[n]; for(j=n+1; j<5; j++) { counts[5] += counts[j]; } } return counts; } int main() { int NodeId = MyNodeId(); int NodesCount = NumberOfNodes(); int N = GetN(); if (N <= 1000000) { if (NodeId != 0) return 0; // exit another instances // n is small, compute on one instance long long *counts = count(0, N); cout << counts[5] << endl; return 0; } // n is big: int begin = (int)((long long)N * (long long)NodeId / NodesCount); int end = (int)((long long)N * (long long)(NodeId+1) / NodesCount); long long *counts = count(begin, end); long long prev[] = {0, 0, 0, 0, 0, 0}; // wait for previous if(NodeId > 0) { Receive(NodeId-1); for(int i = 0; i < 6; i++) { prev[i] = GetLL(NodeId-1); if (i < 5) { for(int j = 0; j < i; j++) { counts[5] += (prev[i]) * (counts[j]); } } } } for (int i = 0; i < 6; i++) { counts[i] += prev[i]; } // send to next nodes if (NodeId < (NodesCount-1)) { for(int i = 0; i < 6; i++) { PutLL(NodeId+1, counts[i]); } Send(NodeId+1); } else { // last instance, print result cout << counts[5] << endl; } return 0; } |
English