#include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> #include <string> #include <sstream> using namespace std; typedef long long int ll; int main() { int nc = NumberOfNodes(); int id = MyNodeId(); int s = Size(); ll left = 0, right = 0, entire = 0, inside = 0; ll in_tmp = 0; // stringstream ret; // ret << id << ": "; for (int i = s * id / nc; i < s * (id + 1) / nc; i++) { int n = ElementAt(i+1); // ret << n << ' '; entire += n; if (entire > left) left = entire; in_tmp+= n; if (in_tmp < 0) in_tmp = 0; if (inside < in_tmp) inside = in_tmp; } // cout << ret.str() << endl; entire = 0; for (int i = s * (id + 1) / nc - 1; i >= s * id / nc; i--) { int n = ElementAt(i+1); entire += n; if (entire > right) right = entire; } if (id == 0) { ll best = 0; ll le = left, ri = right, en = entire, in = inside; ll prev = 0; for (int i = 0; i < nc; i++) { if (i != 0) { Receive(i); le = GetLL(i); ri = GetLL(i); en = GetLL(i); in = GetLL(i); } if (in > best) best = in; if (prev + le > best) best = prev + le; if (prev + en > best) best = prev + en; prev += en; if (prev < 0) prev = ri; } cout << best << endl; } else { PutLL(0, left); PutLL(0, right); PutLL(0, entire); PutLL(0, inside); Send(0); } 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 "maklib.h" #include "message.h" #include <algorithm> #include <iostream> #include <string> #include <sstream> using namespace std; typedef long long int ll; int main() { int nc = NumberOfNodes(); int id = MyNodeId(); int s = Size(); ll left = 0, right = 0, entire = 0, inside = 0; ll in_tmp = 0; // stringstream ret; // ret << id << ": "; for (int i = s * id / nc; i < s * (id + 1) / nc; i++) { int n = ElementAt(i+1); // ret << n << ' '; entire += n; if (entire > left) left = entire; in_tmp+= n; if (in_tmp < 0) in_tmp = 0; if (inside < in_tmp) inside = in_tmp; } // cout << ret.str() << endl; entire = 0; for (int i = s * (id + 1) / nc - 1; i >= s * id / nc; i--) { int n = ElementAt(i+1); entire += n; if (entire > right) right = entire; } if (id == 0) { ll best = 0; ll le = left, ri = right, en = entire, in = inside; ll prev = 0; for (int i = 0; i < nc; i++) { if (i != 0) { Receive(i); le = GetLL(i); ri = GetLL(i); en = GetLL(i); in = GetLL(i); } if (in > best) best = in; if (prev + le > best) best = prev + le; if (prev + en > best) best = prev + en; prev += en; if (prev < 0) prev = ri; } cout << best << endl; } else { PutLL(0, left); PutLL(0, right); PutLL(0, entire); PutLL(0, inside); Send(0); } return 0; } |