#include "maklib.h" #include "message.h" #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int BEGIN = (MyNodeId() * Size()) / NumberOfNodes() + 1; const int END = ((MyNodeId() + 1) * Size()) / NumberOfNodes() + 1; LL res, w, left_res, middle_res, right_res, all_res; void GetMiddle() { w = 0; for (int i = BEGIN; i < END; ++i) { if (w > 0) w += ElementAt(i); else w = ElementAt(i); middle_res = max(w, middle_res); } } void GetLeft() { w = 0; for (int i = BEGIN; i < END; ++i) { w += ElementAt(i); left_res = max(w, left_res); } all_res = w; } void GetRight() { w = 0; for (int i = END-1; i >= BEGIN; --i) { w += ElementAt(i); right_res = max(w, right_res); } } int main () { GetLeft(); GetMiddle(); GetRight(); if (MyNodeId() > 0) { PutLL(0, left_res); PutLL(0, middle_res); PutLL(0, right_res); PutLL(0, all_res); Send(0); } else { res = middle_res; w = right_res; for (int instance = 1; instance < NumberOfNodes(); ++instance) { Receive(instance); LL left = GetLL(instance); LL middle = GetLL(instance); LL right = GetLL(instance); LL all = GetLL(instance); res = max(middle, max(res, w + left)); w = max(w + all, right); } printf ("%lld", res); } }
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 | #include "maklib.h" #include "message.h" #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int BEGIN = (MyNodeId() * Size()) / NumberOfNodes() + 1; const int END = ((MyNodeId() + 1) * Size()) / NumberOfNodes() + 1; LL res, w, left_res, middle_res, right_res, all_res; void GetMiddle() { w = 0; for (int i = BEGIN; i < END; ++i) { if (w > 0) w += ElementAt(i); else w = ElementAt(i); middle_res = max(w, middle_res); } } void GetLeft() { w = 0; for (int i = BEGIN; i < END; ++i) { w += ElementAt(i); left_res = max(w, left_res); } all_res = w; } void GetRight() { w = 0; for (int i = END-1; i >= BEGIN; --i) { w += ElementAt(i); right_res = max(w, right_res); } } int main () { GetLeft(); GetMiddle(); GetRight(); if (MyNodeId() > 0) { PutLL(0, left_res); PutLL(0, middle_res); PutLL(0, right_res); PutLL(0, all_res); Send(0); } else { res = middle_res; w = right_res; for (int instance = 1; instance < NumberOfNodes(); ++instance) { Receive(instance); LL left = GetLL(instance); LL middle = GetLL(instance); LL right = GetLL(instance); LL all = GetLL(instance); res = max(middle, max(res, w + left)); w = max(w + all, right); } printf ("%lld", res); } } |