#include "message.h" #include "maklib.h" #include <iostream> using namespace std; int main() { int n = Size(), nodes = NumberOfNodes(); if (n < 10) { // too few data to play with nodes.. nodes = 1; } else if (nodes > n / 5) { // more nodes than necessary nodes = n / 5; } if (MyNodeId() >= nodes) { // I am useless node :( return 0; } int interval = n / nodes; int from = MyNodeId() * interval + 1; int to = MyNodeId() == nodes - 1 ? n : (from + interval - 1); long long leftSum = 0; long long leftMax = -1000000000; long long rightSum = 0; long long rightMax = -1000000000; long long sum = 0; long long max = -1000000000; for (int i = from; i <= to; ++i) { int e = ElementAt(i); // Find largest left sum leftSum += e; if (leftSum > leftMax) { leftMax = leftSum; } // Find largest right sum rightSum += ElementAt(from + (to - i)); if (rightSum > rightMax) { rightMax = rightSum; } // Find largest element if (sum + e > 0) { sum = sum + e; } else { sum = 0; } if (sum > max) { max = sum; } } if (MyNodeId() > 0) { PutLL(0, max); PutLL(0, leftSum); // total sum == leftSum == rightSum (hope so..) PutLL(0, leftMax); PutLL(0, rightMax); Send(0); } else { long megaMax = 0; rightSum = rightMax; if (max > megaMax) { megaMax = max; } for (int i = 1; i < nodes; ++i) { Receive(i); long long remoteMax = GetLL(i); long long remoteSum = GetLL(i); long long remoteLeftSum = GetLL(i); long long remoteRightSum = GetLL(i); if (remoteMax > megaMax) { megaMax = remoteMax; } if (rightSum + remoteLeftSum > megaMax) { megaMax = rightSum + remoteLeftSum; } // Hyrrr na nich! if (remoteRightSum > rightSum + remoteSum) { rightSum = remoteRightSum; } else { rightSum += remoteSum; } } cout << megaMax << endl; } 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 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 "message.h" #include "maklib.h" #include <iostream> using namespace std; int main() { int n = Size(), nodes = NumberOfNodes(); if (n < 10) { // too few data to play with nodes.. nodes = 1; } else if (nodes > n / 5) { // more nodes than necessary nodes = n / 5; } if (MyNodeId() >= nodes) { // I am useless node :( return 0; } int interval = n / nodes; int from = MyNodeId() * interval + 1; int to = MyNodeId() == nodes - 1 ? n : (from + interval - 1); long long leftSum = 0; long long leftMax = -1000000000; long long rightSum = 0; long long rightMax = -1000000000; long long sum = 0; long long max = -1000000000; for (int i = from; i <= to; ++i) { int e = ElementAt(i); // Find largest left sum leftSum += e; if (leftSum > leftMax) { leftMax = leftSum; } // Find largest right sum rightSum += ElementAt(from + (to - i)); if (rightSum > rightMax) { rightMax = rightSum; } // Find largest element if (sum + e > 0) { sum = sum + e; } else { sum = 0; } if (sum > max) { max = sum; } } if (MyNodeId() > 0) { PutLL(0, max); PutLL(0, leftSum); // total sum == leftSum == rightSum (hope so..) PutLL(0, leftMax); PutLL(0, rightMax); Send(0); } else { long megaMax = 0; rightSum = rightMax; if (max > megaMax) { megaMax = max; } for (int i = 1; i < nodes; ++i) { Receive(i); long long remoteMax = GetLL(i); long long remoteSum = GetLL(i); long long remoteLeftSum = GetLL(i); long long remoteRightSum = GetLL(i); if (remoteMax > megaMax) { megaMax = remoteMax; } if (rightSum + remoteLeftSum > megaMax) { megaMax = rightSum + remoteLeftSum; } // Hyrrr na nich! if (remoteRightSum > rightSum + remoteSum) { rightSum = remoteRightSum; } else { rightSum += remoteSum; } } cout << megaMax << endl; } return 0; } |