#include <cstdio> #include <algorithm> #include "maklib.h" #include "message.h" using namespace std; #define ll long long /* #include <assert.h> #include <stdio.h> #include <stdlib.h> static int n; static int* data; static void Init() { static int initialized = 0; int i; if (initialized) return; assert(1 == scanf("%d", &n)); data = (int*)malloc((n + 1) * sizeof(int)); assert(data != NULL); for (i = 1; i <= n; ++i) { assert(1 == scanf("%d", data + i)); } initialized = 1; } int Size() { Init(); return n; } int ElementAt(int i) { Init(); assert(1 <= i && i <= n); return data[i]; } const int NN=7; int Node; int MyNodeId() {return Node;} int NumberOfNodes() {return NN;} ll Left[NN],Right[NN],Best[NN],Sum[NN]; */ ll N; int main(int argc, char** argv) { N=Size(); // for (Node=0;Node<NumberOfNodes();Node++) { int poczatek = (MyNodeId() * N) / NumberOfNodes(); int koniec = ((MyNodeId() + 1) * N) / NumberOfNodes(); ll best=0,left=0,right=0,sumleft=0; for (int i = poczatek; i < koniec; ++i) { right+=ElementAt(i+1); if (right<0) right=0; else if (right>best) best=right; sumleft+=ElementAt(i+1); if (sumleft>left) left=sumleft; } // printf("%d (%d %d): left=%lld, right=%lld, best=%lld\n",Node,poczatek,koniec-1,left,right,best); // Left[Node]=left; Right[Node]=right; Best[Node]=best; Sum[Node]=sumleft; // } // for (Node=NumberOfNodes()-1;Node>=0;Node--) { if (MyNodeId() > 0) { PutLL(0, left); PutLL(0, right); PutLL(0,best); PutLL(0,sumleft); Send(0); } else { //ll left=Left[Node]; ll right=Right[Node]; ll best=Best[Node]; for (int i = 1; i < NumberOfNodes(); ++i) { Receive(i); ll left2=GetLL(i); ll right2=GetLL(i); ll best2=GetLL(i); ll sum2=GetLL(i); //ll left2=Left[i]; ll right2=Right[i]; ll best2=Best[i]; ll sum2=Sum[i]; if (best2>best) best=best2; if (right+left2>best) best=right+left2; right = max(right+sum2,right2); } printf("%lld\n",best); } // } 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 | #include <cstdio> #include <algorithm> #include "maklib.h" #include "message.h" using namespace std; #define ll long long /* #include <assert.h> #include <stdio.h> #include <stdlib.h> static int n; static int* data; static void Init() { static int initialized = 0; int i; if (initialized) return; assert(1 == scanf("%d", &n)); data = (int*)malloc((n + 1) * sizeof(int)); assert(data != NULL); for (i = 1; i <= n; ++i) { assert(1 == scanf("%d", data + i)); } initialized = 1; } int Size() { Init(); return n; } int ElementAt(int i) { Init(); assert(1 <= i && i <= n); return data[i]; } const int NN=7; int Node; int MyNodeId() {return Node;} int NumberOfNodes() {return NN;} ll Left[NN],Right[NN],Best[NN],Sum[NN]; */ ll N; int main(int argc, char** argv) { N=Size(); // for (Node=0;Node<NumberOfNodes();Node++) { int poczatek = (MyNodeId() * N) / NumberOfNodes(); int koniec = ((MyNodeId() + 1) * N) / NumberOfNodes(); ll best=0,left=0,right=0,sumleft=0; for (int i = poczatek; i < koniec; ++i) { right+=ElementAt(i+1); if (right<0) right=0; else if (right>best) best=right; sumleft+=ElementAt(i+1); if (sumleft>left) left=sumleft; } // printf("%d (%d %d): left=%lld, right=%lld, best=%lld\n",Node,poczatek,koniec-1,left,right,best); // Left[Node]=left; Right[Node]=right; Best[Node]=best; Sum[Node]=sumleft; // } // for (Node=NumberOfNodes()-1;Node>=0;Node--) { if (MyNodeId() > 0) { PutLL(0, left); PutLL(0, right); PutLL(0,best); PutLL(0,sumleft); Send(0); } else { //ll left=Left[Node]; ll right=Right[Node]; ll best=Best[Node]; for (int i = 1; i < NumberOfNodes(); ++i) { Receive(i); ll left2=GetLL(i); ll right2=GetLL(i); ll best2=GetLL(i); ll sum2=GetLL(i); //ll left2=Left[i]; ll right2=Right[i]; ll best2=Best[i]; ll sum2=Sum[i]; if (best2>best) best=best2; if (right+left2>best) best=right+left2; right = max(right+sum2,right2); } printf("%lld\n",best); } // } return 0; } |