#include "maklib.h" #include "message.h" #include <algorithm> #include <cstdio> using std::max; int main() { int n = Size(); int myNodeId = MyNodeId(); int numberOfNodes = NumberOfNodes(); int beginIndex = ((long long int)myNodeId * n) / numberOfNodes; int endIndex = ((long long int)(myNodeId + 1) * n) / numberOfNodes; //printf("node %d, b=%d, e=%d\n", myNodeId, beginIndex, endIndex); long long int myMax = 0; long long int mySum = 0; long long int myCurrentSum = 0; for (int i = beginIndex; i < endIndex; i++) { int value = ElementAt(i + 1); //printf("node %d index %d, value %d\n", myNodeId, i, value); mySum += value; myCurrentSum += value; if (myCurrentSum > myMax) myMax = myCurrentSum; if (myCurrentSum < 0) myCurrentSum = 0; } long long int totalMax = myMax; long long int totalCurrentSum = myCurrentSum; //printf("node %d myMax=%lld, mySum=%lld, myCurrentSum=%lld\n", myNodeId, myMax, mySum, myCurrentSum); if (myNodeId != 0) { PutLL(0, myMax); PutLL(0, mySum); PutLL(0, myCurrentSum); Send(0); } else { for (int nodeId = 1; nodeId < numberOfNodes; nodeId++) { PutLL(nodeId, totalCurrentSum); Send(nodeId); Receive(nodeId); long long nodeMax = GetLL(nodeId); long long nodeSum = GetLL(nodeId); long long nodeEndSum = GetLL(nodeId); if (nodeMax > totalMax) totalMax = nodeMax; //printf("node %d, totalCurrentSum=%lld nodeSum=%lld, nodeEndSum=%lld\n", nodeId, totalCurrentSum, nodeSum, nodeEndSum); totalCurrentSum = max(totalCurrentSum + nodeSum, nodeEndSum); } } if (myNodeId != 0) { Receive(0); myCurrentSum = GetLL(0); myMax = myCurrentSum; for (int i = beginIndex; i < endIndex; i++) { int value = ElementAt(i + 1); myCurrentSum += value; if (myCurrentSum > myMax) myMax = myCurrentSum; if (myCurrentSum < 0) break; } PutLL(0, myMax); Send(0); } else { for (int nodeId = 1; nodeId < numberOfNodes; nodeId++) { Receive(nodeId); long long nodeMax = GetLL(nodeId); if (nodeMax > totalMax) totalMax = nodeMax; } printf("%lld\n", totalMax); } 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 91 92 93 94 95 96 | #include "maklib.h" #include "message.h" #include <algorithm> #include <cstdio> using std::max; int main() { int n = Size(); int myNodeId = MyNodeId(); int numberOfNodes = NumberOfNodes(); int beginIndex = ((long long int)myNodeId * n) / numberOfNodes; int endIndex = ((long long int)(myNodeId + 1) * n) / numberOfNodes; //printf("node %d, b=%d, e=%d\n", myNodeId, beginIndex, endIndex); long long int myMax = 0; long long int mySum = 0; long long int myCurrentSum = 0; for (int i = beginIndex; i < endIndex; i++) { int value = ElementAt(i + 1); //printf("node %d index %d, value %d\n", myNodeId, i, value); mySum += value; myCurrentSum += value; if (myCurrentSum > myMax) myMax = myCurrentSum; if (myCurrentSum < 0) myCurrentSum = 0; } long long int totalMax = myMax; long long int totalCurrentSum = myCurrentSum; //printf("node %d myMax=%lld, mySum=%lld, myCurrentSum=%lld\n", myNodeId, myMax, mySum, myCurrentSum); if (myNodeId != 0) { PutLL(0, myMax); PutLL(0, mySum); PutLL(0, myCurrentSum); Send(0); } else { for (int nodeId = 1; nodeId < numberOfNodes; nodeId++) { PutLL(nodeId, totalCurrentSum); Send(nodeId); Receive(nodeId); long long nodeMax = GetLL(nodeId); long long nodeSum = GetLL(nodeId); long long nodeEndSum = GetLL(nodeId); if (nodeMax > totalMax) totalMax = nodeMax; //printf("node %d, totalCurrentSum=%lld nodeSum=%lld, nodeEndSum=%lld\n", nodeId, totalCurrentSum, nodeSum, nodeEndSum); totalCurrentSum = max(totalCurrentSum + nodeSum, nodeEndSum); } } if (myNodeId != 0) { Receive(0); myCurrentSum = GetLL(0); myMax = myCurrentSum; for (int i = beginIndex; i < endIndex; i++) { int value = ElementAt(i + 1); myCurrentSum += value; if (myCurrentSum > myMax) myMax = myCurrentSum; if (myCurrentSum < 0) break; } PutLL(0, myMax); Send(0); } else { for (int nodeId = 1; nodeId < numberOfNodes; nodeId++) { Receive(nodeId); long long nodeMax = GetLL(nodeId); if (nodeMax > totalMax) totalMax = nodeMax; } printf("%lld\n", totalMax); } return 0; } |