#include "message.h" #include "kanapka.h" #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define all(c) (c).begin(),(c).end() #define scanf(...) scanf(__VA_ARGS__)?:0 #define e1 first #define e2 second #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; int n,nodes,id,x; ll suma,maks,maksl,maksp,nlewo; int main() { nodes=NumberOfNodes()-1; id=MyNodeId(); n=GetN(); if (id==0) { FOR(i,1,nodes) { Receive(i); ll csuma=GetLL(i),maksw=GetLL(i),maksl=GetLL(i),maksp=GetLL(i); suma+=csuma; if (maksw>maks) maks=maksw; if (nlewo+maksl>maks) maks=nlewo+maksl; nlewo=max(nlewo-csuma,maksp); } printf("%lld",suma+maks); } else { int ile=(n-1)/nodes+1,pocz=(id-1)*ile,kon=min(n-1,pocz+ile-1); ll curr=0; FOR(i,pocz,kon) { x=-GetTaste(i); suma+=x; if (suma>maksl) maksl=suma; if (curr+x<0) curr=0; else curr+=x; if (curr>maks) maks=curr; } PutLL(0,-suma); PutLL(0,maks); PutLL(0,maksl); suma=0; FORD(i,kon,pocz) { x=-GetTaste(i); suma+=x; if (suma>maksp) maksp=suma; } PutLL(0,maksp); Send(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 | #include "message.h" #include "kanapka.h" #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define all(c) (c).begin(),(c).end() #define scanf(...) scanf(__VA_ARGS__)?:0 #define e1 first #define e2 second #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,int> pii; int n,nodes,id,x; ll suma,maks,maksl,maksp,nlewo; int main() { nodes=NumberOfNodes()-1; id=MyNodeId(); n=GetN(); if (id==0) { FOR(i,1,nodes) { Receive(i); ll csuma=GetLL(i),maksw=GetLL(i),maksl=GetLL(i),maksp=GetLL(i); suma+=csuma; if (maksw>maks) maks=maksw; if (nlewo+maksl>maks) maks=nlewo+maksl; nlewo=max(nlewo-csuma,maksp); } printf("%lld",suma+maks); } else { int ile=(n-1)/nodes+1,pocz=(id-1)*ile,kon=min(n-1,pocz+ile-1); ll curr=0; FOR(i,pocz,kon) { x=-GetTaste(i); suma+=x; if (suma>maksl) maksl=suma; if (curr+x<0) curr=0; else curr+=x; if (curr>maks) maks=curr; } PutLL(0,-suma); PutLL(0,maks); PutLL(0,maksl); suma=0; FORD(i,kon,pocz) { x=-GetTaste(i); suma+=x; if (suma>maksp) maksp=suma; } PutLL(0,maksp); Send(0); } } |