#include <iostream> #include <vector> #include <stdlib.h> #include "maklib.h" #include "message.h" using namespace std; void addToRanges(vector<long long> &ranges, long long v) { int size = ranges.size(); long long last = 0; if (size > 0) { last = ranges[size - 1]; } if (size > 0 && ((last > 0 && v > 0) || (last < 0 && v < 0))) { ranges.pop_back(); addToRanges(ranges, v + last); } else if (size > 1) { //try to consolidate long long beforeLast = ranges[size - 2]; if (v > 0 && v > abs(last) && beforeLast > abs(last)) { ranges.pop_back(); ranges.pop_back(); addToRanges(ranges, beforeLast + last + v); } else { ranges.push_back(v); } } else { ranges.push_back(v); } } int main() { int start = (MyNodeId() * Size()) / NumberOfNodes(); int end = ((MyNodeId() + 1) * Size()) / NumberOfNodes(); vector<long long> ranges; long long sum = 0; for (int i = start; i < end; i++) { long long v = ElementAt(i + 1); if (v != 0) { if (i != start) { if ((sum >= 0 && v > 0) || (sum <= 0 && v < 0)) { sum += v; } else { addToRanges(ranges, sum); sum = v; } } else { sum = v; } } } addToRanges(ranges, sum); if (MyNodeId() != 0) { PutInt(0, ranges.size()); for (vector<long long>::iterator it = ranges.begin(); it != ranges.end(); ++it) { PutLL(0, *it); } Send(0); } else { //cout << "consolidating\n"; for (int i = 1; i < NumberOfNodes(); i++) { Receive(i); int length = GetInt(i); for (int j = 0; j < length; j++) { addToRanges(ranges, GetLL(i)); } } if (ranges.size() == 0) { cout << "0" << endl; } else if (ranges.size() == 1) { if (ranges[0] > 0) { cout << ranges[0] << endl; } else { cout << "0" << endl; } } else { long long maxFromRanges = ranges[0]; for (vector<long long>::iterator it = ranges.begin(); it != ranges.end(); ++it) { if (*it > maxFromRanges) { maxFromRanges = *it; } } cout << maxFromRanges << 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 | #include <iostream> #include <vector> #include <stdlib.h> #include "maklib.h" #include "message.h" using namespace std; void addToRanges(vector<long long> &ranges, long long v) { int size = ranges.size(); long long last = 0; if (size > 0) { last = ranges[size - 1]; } if (size > 0 && ((last > 0 && v > 0) || (last < 0 && v < 0))) { ranges.pop_back(); addToRanges(ranges, v + last); } else if (size > 1) { //try to consolidate long long beforeLast = ranges[size - 2]; if (v > 0 && v > abs(last) && beforeLast > abs(last)) { ranges.pop_back(); ranges.pop_back(); addToRanges(ranges, beforeLast + last + v); } else { ranges.push_back(v); } } else { ranges.push_back(v); } } int main() { int start = (MyNodeId() * Size()) / NumberOfNodes(); int end = ((MyNodeId() + 1) * Size()) / NumberOfNodes(); vector<long long> ranges; long long sum = 0; for (int i = start; i < end; i++) { long long v = ElementAt(i + 1); if (v != 0) { if (i != start) { if ((sum >= 0 && v > 0) || (sum <= 0 && v < 0)) { sum += v; } else { addToRanges(ranges, sum); sum = v; } } else { sum = v; } } } addToRanges(ranges, sum); if (MyNodeId() != 0) { PutInt(0, ranges.size()); for (vector<long long>::iterator it = ranges.begin(); it != ranges.end(); ++it) { PutLL(0, *it); } Send(0); } else { //cout << "consolidating\n"; for (int i = 1; i < NumberOfNodes(); i++) { Receive(i); int length = GetInt(i); for (int j = 0; j < length; j++) { addToRanges(ranges, GetLL(i)); } } if (ranges.size() == 0) { cout << "0" << endl; } else if (ranges.size() == 1) { if (ranges[0] > 0) { cout << ranges[0] << endl; } else { cout << "0" << endl; } } else { long long maxFromRanges = ranges[0]; for (vector<long long>::iterator it = ranges.begin(); it != ranges.end(); ++it) { if (*it > maxFromRanges) { maxFromRanges = *it; } } cout << maxFromRanges << endl; } } } |