#include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; // int MyId() { // return MyNodeId() + 1; // } // int GetAt(int k) { return ElementAt(k); } template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } int main() { int n = Size(); int start = 1 + MyNodeId() * n / NumberOfNodes(); int end = ((MyNodeId() + 1) * n) / NumberOfNodes(); int sum = 0; int best = 0; int lowest = 0; int highest = 0; for (int i = start; i <= end; i++) { int a = GetAt(i); sum += a; mini(lowest, sum); maxi(highest, sum); maxi(best, highest - lowest); } if (MyNodeId() > 0) { PutInt(0, sum); PutInt(0, lowest); PutInt(0, highest); PutInt(0, best); Send(0); } else { for (int i = 1; i < NumberOfNodes(); i++) { int inst = Receive(i); int their_sum = GetInt(inst); int their_lowest = GetInt(inst); int their_highest = GetInt(inst); int their_best = GetInt(inst); maxi(best, their_best); maxi(highest, sum + their_highest); maxi(best, highest - lowest); mini(lowest, sum + their_lowest); sum += their_sum; } cout << best << 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 | #include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; // int MyId() { // return MyNodeId() + 1; // } // int GetAt(int k) { return ElementAt(k); } template<class C> void mini(C&a4, C b4){a4=min(a4, b4); } template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); } int main() { int n = Size(); int start = 1 + MyNodeId() * n / NumberOfNodes(); int end = ((MyNodeId() + 1) * n) / NumberOfNodes(); int sum = 0; int best = 0; int lowest = 0; int highest = 0; for (int i = start; i <= end; i++) { int a = GetAt(i); sum += a; mini(lowest, sum); maxi(highest, sum); maxi(best, highest - lowest); } if (MyNodeId() > 0) { PutInt(0, sum); PutInt(0, lowest); PutInt(0, highest); PutInt(0, best); Send(0); } else { for (int i = 1; i < NumberOfNodes(); i++) { int inst = Receive(i); int their_sum = GetInt(inst); int their_lowest = GetInt(inst); int their_highest = GetInt(inst); int their_best = GetInt(inst); maxi(best, their_best); maxi(highest, sum + their_highest); maxi(best, highest - lowest); mini(lowest, sum + their_lowest); sum += their_sum; } cout << best << endl; } return 0; } |