#include "message.h" #include "maklib.h" #include <algorithm> int main () { long long m = 0; bool add = true; int i; int act; int last = 0; long long sumAdd; long long sum = 0; int d = Size () / NumberOfNodes (); if (d * NumberOfNodes () != Size ()) ++ d; if (d == 0) d = 1; //fprintf (stderr, "%d: %d\n", MyNodeId (), d); for (i = d * MyNodeId () + 1; i <= d * MyNodeId () + d; ++ i) { if (i > Size ()) { //fprintf (stderr, "%d: Exitting loop\n", MyNodeId ()); break; } //fprintf (stderr, "%d: Getting element %d\n", MyNodeId (), i); act = ElementAt (i); if (sum < 0) { sumAdd = m; sum = act; add = false; } else sum += act; m = std::max (m, sum); last = act; } if (MyNodeId () != 0) { Receive (MyNodeId () - 1); int g = GetInt (MyNodeId () - 1); if (add) { sum += g; m += g; } if (!add && sumAdd + g > m) m = sumAdd + g; PutLL (0, m); Send (0); } if (sum < 0) sum = 0; if (MyNodeId () != NumberOfNodes () - 1) { PutInt (MyNodeId () + 1, sum); Send (MyNodeId () + 1); } if (MyNodeId () == 0) { for (i = 1; i < NumberOfNodes (); ++ i) { Receive (i); m = std::max (m, GetLL (i)); } printf ("%lli\n", m); } 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 | #include "message.h" #include "maklib.h" #include <algorithm> int main () { long long m = 0; bool add = true; int i; int act; int last = 0; long long sumAdd; long long sum = 0; int d = Size () / NumberOfNodes (); if (d * NumberOfNodes () != Size ()) ++ d; if (d == 0) d = 1; //fprintf (stderr, "%d: %d\n", MyNodeId (), d); for (i = d * MyNodeId () + 1; i <= d * MyNodeId () + d; ++ i) { if (i > Size ()) { //fprintf (stderr, "%d: Exitting loop\n", MyNodeId ()); break; } //fprintf (stderr, "%d: Getting element %d\n", MyNodeId (), i); act = ElementAt (i); if (sum < 0) { sumAdd = m; sum = act; add = false; } else sum += act; m = std::max (m, sum); last = act; } if (MyNodeId () != 0) { Receive (MyNodeId () - 1); int g = GetInt (MyNodeId () - 1); if (add) { sum += g; m += g; } if (!add && sumAdd + g > m) m = sumAdd + g; PutLL (0, m); Send (0); } if (sum < 0) sum = 0; if (MyNodeId () != NumberOfNodes () - 1) { PutInt (MyNodeId () + 1, sum); Send (MyNodeId () + 1); } if (MyNodeId () == 0) { for (i = 1; i < NumberOfNodes (); ++ i) { Receive (i); m = std::max (m, GetLL (i)); } printf ("%lli\n", m); } return 0; } |