#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> #include "message.h" #include "maklib.h" using namespace std; void computePart(int begin, int end, long long &in, long long &out, long long &left, long long &right ) { in = out = left = right = 0LL; long long r = 0LL; for (int i = begin; i < end; ++i) { int v = ElementAt(i); out += v; left = max(left, out); r += v; r = max(r, 0LL); in = max(in, r); } right = r; } int main() { int nn = NumberOfNodes(); int id = MyNodeId(); int n = Size(); long long in, out, left, right; int m = (n + nn - 1) / nn; int begin = id * m; int end = min(n, (id + 1) * m); computePart(begin, end, in, out, left, right); if (id < nn - 1) { PutLL(nn - 1, in); PutLL(nn - 1, out); PutLL(nn - 1, left); PutLL(nn - 1, right); Send(nn - 1); } else { long long current = 0; long long ret = 0; for (int i = 0; i < nn - 1; ++i) { Receive(i); long long in = GetLL(i); long long out = GetLL(i); long long left = GetLL(i); long long right = GetLL(i); ret = max(ret, in); ret = max(ret, current + left); current = max(current + out, right); } ret = max(ret, in); ret = max(ret, current + left); current = max(current + out, right); printf("%lld\n", ret); } 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 | #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> #include "message.h" #include "maklib.h" using namespace std; void computePart(int begin, int end, long long &in, long long &out, long long &left, long long &right ) { in = out = left = right = 0LL; long long r = 0LL; for (int i = begin; i < end; ++i) { int v = ElementAt(i); out += v; left = max(left, out); r += v; r = max(r, 0LL); in = max(in, r); } right = r; } int main() { int nn = NumberOfNodes(); int id = MyNodeId(); int n = Size(); long long in, out, left, right; int m = (n + nn - 1) / nn; int begin = id * m; int end = min(n, (id + 1) * m); computePart(begin, end, in, out, left, right); if (id < nn - 1) { PutLL(nn - 1, in); PutLL(nn - 1, out); PutLL(nn - 1, left); PutLL(nn - 1, right); Send(nn - 1); } else { long long current = 0; long long ret = 0; for (int i = 0; i < nn - 1; ++i) { Receive(i); long long in = GetLL(i); long long out = GetLL(i); long long left = GetLL(i); long long right = GetLL(i); ret = max(ret, in); ret = max(ret, current + left); current = max(current + out, right); } ret = max(ret, in); ret = max(ret, current + left); current = max(current + out, right); printf("%lld\n", ret); } return 0; } |