#include <cstdio> #include "maklib.h" #include "message.h" #include <algorithm> int main() { if(MyNodeId() > 0) { int count = 1 + Size() / NumberOfNodes(); int beg = count * MyNodeId(); int end = std::min(beg + count, Size()); long long maxBeg = 0; long long maxEnd = 0; long long currEnd = 0; long long whole = 0; for(int i = beg; i < end; ++i) { int ele = ElementAt(i + 1); whole += ele; maxBeg = std::max(maxBeg, whole); currEnd += ele; if(currEnd < 0) { currEnd = 0; } maxEnd = std::max(maxEnd, currEnd); } PutLL(0, maxBeg); PutLL(0, whole); PutLL(0, maxEnd); Send(0); }else { long long totalMax = 0; long long current = 0; for(int i = 1; i < NumberOfNodes(); ++i) { Receive(i); long long maxBeg = GetLL(i); long long whole = GetLL(i); long long maxEnd = GetLL(i); totalMax = std::max(current + maxBeg, totalMax); current += whole; if(current < maxEnd) { current = maxEnd; } } printf("%lld\n", totalMax); } }
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 | #include <cstdio> #include "maklib.h" #include "message.h" #include <algorithm> int main() { if(MyNodeId() > 0) { int count = 1 + Size() / NumberOfNodes(); int beg = count * MyNodeId(); int end = std::min(beg + count, Size()); long long maxBeg = 0; long long maxEnd = 0; long long currEnd = 0; long long whole = 0; for(int i = beg; i < end; ++i) { int ele = ElementAt(i + 1); whole += ele; maxBeg = std::max(maxBeg, whole); currEnd += ele; if(currEnd < 0) { currEnd = 0; } maxEnd = std::max(maxEnd, currEnd); } PutLL(0, maxBeg); PutLL(0, whole); PutLL(0, maxEnd); Send(0); }else { long long totalMax = 0; long long current = 0; for(int i = 1; i < NumberOfNodes(); ++i) { Receive(i); long long maxBeg = GetLL(i); long long whole = GetLL(i); long long maxEnd = GetLL(i); totalMax = std::max(current + maxBeg, totalMax); current += whole; if(current < maxEnd) { current = maxEnd; } } printf("%lld\n", totalMax); } } |