#include "message.h" #include "maklib.h" #include <vector> #include <algorithm> #include <iostream> struct Data { Data(long long _a = 0, long long _b = 0, long long _c = 0, long long _d = 0) : a(_a), b(_b), c(_c), d(_d) {} void send(int _target) { PutLL(0, d); PutLL(0, c); PutLL(0, b); PutLL(0, a); Send(0); } static Data receive(int _source) { int source = Receive(_source); return { GetLL(source), GetLL(source), GetLL(source), GetLL(source) }; } // ----- long long a, b, c, d; }; int main() { int numOfNodes = (NumberOfNodes() > Size() ? Size() : NumberOfNodes()); if (MyNodeId() >= numOfNodes) { return 0; } int begin = Size() / numOfNodes * MyNodeId() + 1; int end = begin + Size() / numOfNodes - 1; end = (MyNodeId() == numOfNodes - 1 ? Size() : end); Data mypack; long long b = 0, c = 0, d = 0; for (int i = begin; i <= end; ++i) { // a mypack.a += ElementAt(i); // b b += ElementAt(end - i + begin); mypack.b = std::max(mypack.b, b); // c c += ElementAt(i); mypack.c = std::max(mypack.c, c); // d if (d > 0) { d += ElementAt(i); } else { d = ElementAt(i); } mypack.d = std::max(mypack.d, d); } if (MyNodeId() == 0) { std::vector<Data> intel(numOfNodes); for (int i = 1; i < numOfNodes; ++i) { intel[i] = Data::receive(i); } intel[0] = mypack; long long mmax = 0, total = 0; for (int i = 0; i < numOfNodes; ++i) { mmax = std::max(intel[i].d, mmax); mmax = std::max(total + intel[i].c, mmax); if (total + intel[i].a < intel[i].b) { total = intel[i].b; } else { total += intel[i].a; } mmax = std::max(total, mmax); } std::cout << mmax << '\n'; } else { mypack.send(0); } 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include "message.h" #include "maklib.h" #include <vector> #include <algorithm> #include <iostream> struct Data { Data(long long _a = 0, long long _b = 0, long long _c = 0, long long _d = 0) : a(_a), b(_b), c(_c), d(_d) {} void send(int _target) { PutLL(0, d); PutLL(0, c); PutLL(0, b); PutLL(0, a); Send(0); } static Data receive(int _source) { int source = Receive(_source); return { GetLL(source), GetLL(source), GetLL(source), GetLL(source) }; } // ----- long long a, b, c, d; }; int main() { int numOfNodes = (NumberOfNodes() > Size() ? Size() : NumberOfNodes()); if (MyNodeId() >= numOfNodes) { return 0; } int begin = Size() / numOfNodes * MyNodeId() + 1; int end = begin + Size() / numOfNodes - 1; end = (MyNodeId() == numOfNodes - 1 ? Size() : end); Data mypack; long long b = 0, c = 0, d = 0; for (int i = begin; i <= end; ++i) { // a mypack.a += ElementAt(i); // b b += ElementAt(end - i + begin); mypack.b = std::max(mypack.b, b); // c c += ElementAt(i); mypack.c = std::max(mypack.c, c); // d if (d > 0) { d += ElementAt(i); } else { d = ElementAt(i); } mypack.d = std::max(mypack.d, d); } if (MyNodeId() == 0) { std::vector<Data> intel(numOfNodes); for (int i = 1; i < numOfNodes; ++i) { intel[i] = Data::receive(i); } intel[0] = mypack; long long mmax = 0, total = 0; for (int i = 0; i < numOfNodes; ++i) { mmax = std::max(intel[i].d, mmax); mmax = std::max(total + intel[i].c, mmax); if (total + intel[i].a < intel[i].b) { total = intel[i].b; } else { total += intel[i].a; } mmax = std::max(total, mmax); } std::cout << mmax << '\n'; } else { mypack.send(0); } return 0; } |