#include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; //int NumberOfNodes() { return 1; } //int Size() { return 10; } //int MyNodeId() { return 0; } //int ElementAt(int a) { cout << "ElementAt(" << a << ");\n"; return 5; } //void Receive(int x) {} //long long GetLL(int x) { return -1; } //void PutLL(int a, long long x) {} //void Send(int x) {} int main() { long long n = Size(); int m = NumberOfNodes(); int x = MyNodeId(); int beg = 1+(n*x)/m, end = 1+(n*(x+1))/m; long long maxPref=0, maxAny=0, maxSuf=0, pref=0, suf=0, maxTemp=0, sum=0; for (int i=beg; i<end; i++) { int a = ElementAt(i); sum += a; pref += a; maxTemp += a; if (maxTemp < 0) maxTemp = 0; maxAny = max(maxAny, maxTemp); maxPref = max(pref, maxPref); } for (int i=end-1; i>=beg; i--) { suf += ElementAt(i); maxSuf = max(suf, maxSuf); } long long bestBef = 0; if (x > 0) { Receive(x-1); bestBef = GetLL(x-1); } if (x < m-1) { PutLL(x+1, max(bestBef+sum, maxSuf)); Send(x+1); } long long best = max(maxAny, bestBef+maxPref); if (x > 0) { PutLL(0, best); Send(0); } else { long long res = best; for (int i=1; i<m; i++) { Receive(i); res = max(res, GetLL(i)); } cout << res << 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 | #include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; //int NumberOfNodes() { return 1; } //int Size() { return 10; } //int MyNodeId() { return 0; } //int ElementAt(int a) { cout << "ElementAt(" << a << ");\n"; return 5; } //void Receive(int x) {} //long long GetLL(int x) { return -1; } //void PutLL(int a, long long x) {} //void Send(int x) {} int main() { long long n = Size(); int m = NumberOfNodes(); int x = MyNodeId(); int beg = 1+(n*x)/m, end = 1+(n*(x+1))/m; long long maxPref=0, maxAny=0, maxSuf=0, pref=0, suf=0, maxTemp=0, sum=0; for (int i=beg; i<end; i++) { int a = ElementAt(i); sum += a; pref += a; maxTemp += a; if (maxTemp < 0) maxTemp = 0; maxAny = max(maxAny, maxTemp); maxPref = max(pref, maxPref); } for (int i=end-1; i>=beg; i--) { suf += ElementAt(i); maxSuf = max(suf, maxSuf); } long long bestBef = 0; if (x > 0) { Receive(x-1); bestBef = GetLL(x-1); } if (x < m-1) { PutLL(x+1, max(bestBef+sum, maxSuf)); Send(x+1); } long long best = max(maxAny, bestBef+maxPref); if (x > 0) { PutLL(0, best); Send(0); } else { long long res = best; for (int i=1; i<m; i++) { Receive(i); res = max(res, GetLL(i)); } cout << res << endl; } return 0; } |