#include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; int main() { int nodesNumber = NumberOfNodes(); if (nodesNumber >= Size()) { if (MyNodeId() > 0) { return 0 ; } else { nodesNumber = 1; } } long long sum, leftSum, rightSum, partSum = 0; int partSize = Size() / nodesNumber; int start = (MyNodeId() * partSize) + 1; int end = start + partSize; if (MyNodeId() == nodesNumber - 1) { end = Size() + 1; } //cout << start << "-" << end << endl; long long maxSum = ElementAt(start), maxCurrent = ElementAt(start); long long maxLeftSum = ElementAt(start), currentLeftSum = ElementAt(start); partSum = ElementAt(start); for (int i = start + 1; i< end; ++i ) { int element = ElementAt(i); if (maxCurrent + element > element) { maxCurrent = maxCurrent + element; } else { maxCurrent = element; } if (maxCurrent > maxSum) { maxSum = maxCurrent; } currentLeftSum += element; if (currentLeftSum > maxLeftSum) { maxLeftSum = currentLeftSum; } partSum += element; } long long maxRightSum = ElementAt(end -1), currentRightSum = ElementAt(end -1); for (int i = end -2; i >= start; --i) { int element = ElementAt(i); currentRightSum += element; if (currentRightSum > maxRightSum) { maxRightSum = currentRightSum; } } sum = maxSum; leftSum = maxLeftSum; rightSum = maxRightSum; if (MyNodeId() > 0) { PutLL(0, partSum); PutLL(0, sum); PutLL(0, leftSum); PutLL(0, rightSum); Send(0); } else { long long partResults[nodesNumber]; long long subResults[nodesNumber]; long long subResultsLeft[nodesNumber]; long long subResultsRight[nodesNumber]; partResults[0] = partSum; subResults[0] = sum; subResultsLeft[0] = leftSum; subResultsRight[0] = rightSum; //cout << "0: " << subResults[0] << " " << subResultsLeft[0] << " " << subResultsRight[0] << endl; for (int i = 1; i < nodesNumber; ++i) { Receive(i); partResults[i] = GetLL(i); subResults[i] = GetLL(i); subResultsLeft[i] = GetLL(i); subResultsRight[i] = GetLL(i); //cout << i << ": " << partResults[i] << " " << subResults[i] << " " << subResultsLeft[i] << " " << subResultsRight[i] << endl; } //cout << endl; for (int i =0; i < nodesNumber; ++i) { if (subResults[i] > sum) { sum = subResults[i]; } } for (int i =1; i < nodesNumber; ++i) { if (subResultsRight[i-1] + partResults[i] > subResultsRight[i]) { subResultsRight[i] = subResultsRight[i-1] + partResults[i]; } if (subResultsRight[i -1] + subResultsLeft[i] > sum) { sum = subResultsRight[i -1] + subResultsLeft[i]; } //cout << i << ": " << partResults[i] << " " << subResults[i] << " " << subResultsLeft[i] << " " << subResultsRight[i] << endl; } cout << sum << endl; } }
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; int main() { int nodesNumber = NumberOfNodes(); if (nodesNumber >= Size()) { if (MyNodeId() > 0) { return 0 ; } else { nodesNumber = 1; } } long long sum, leftSum, rightSum, partSum = 0; int partSize = Size() / nodesNumber; int start = (MyNodeId() * partSize) + 1; int end = start + partSize; if (MyNodeId() == nodesNumber - 1) { end = Size() + 1; } //cout << start << "-" << end << endl; long long maxSum = ElementAt(start), maxCurrent = ElementAt(start); long long maxLeftSum = ElementAt(start), currentLeftSum = ElementAt(start); partSum = ElementAt(start); for (int i = start + 1; i< end; ++i ) { int element = ElementAt(i); if (maxCurrent + element > element) { maxCurrent = maxCurrent + element; } else { maxCurrent = element; } if (maxCurrent > maxSum) { maxSum = maxCurrent; } currentLeftSum += element; if (currentLeftSum > maxLeftSum) { maxLeftSum = currentLeftSum; } partSum += element; } long long maxRightSum = ElementAt(end -1), currentRightSum = ElementAt(end -1); for (int i = end -2; i >= start; --i) { int element = ElementAt(i); currentRightSum += element; if (currentRightSum > maxRightSum) { maxRightSum = currentRightSum; } } sum = maxSum; leftSum = maxLeftSum; rightSum = maxRightSum; if (MyNodeId() > 0) { PutLL(0, partSum); PutLL(0, sum); PutLL(0, leftSum); PutLL(0, rightSum); Send(0); } else { long long partResults[nodesNumber]; long long subResults[nodesNumber]; long long subResultsLeft[nodesNumber]; long long subResultsRight[nodesNumber]; partResults[0] = partSum; subResults[0] = sum; subResultsLeft[0] = leftSum; subResultsRight[0] = rightSum; //cout << "0: " << subResults[0] << " " << subResultsLeft[0] << " " << subResultsRight[0] << endl; for (int i = 1; i < nodesNumber; ++i) { Receive(i); partResults[i] = GetLL(i); subResults[i] = GetLL(i); subResultsLeft[i] = GetLL(i); subResultsRight[i] = GetLL(i); //cout << i << ": " << partResults[i] << " " << subResults[i] << " " << subResultsLeft[i] << " " << subResultsRight[i] << endl; } //cout << endl; for (int i =0; i < nodesNumber; ++i) { if (subResults[i] > sum) { sum = subResults[i]; } } for (int i =1; i < nodesNumber; ++i) { if (subResultsRight[i-1] + partResults[i] > subResultsRight[i]) { subResultsRight[i] = subResultsRight[i-1] + partResults[i]; } if (subResultsRight[i -1] + subResultsLeft[i] > sum) { sum = subResultsRight[i -1] + subResultsLeft[i]; } //cout << i << ": " << partResults[i] << " " << subResults[i] << " " << subResultsLeft[i] << " " << subResultsRight[i] << endl; } cout << sum << endl; } } |