#include "message.h"
#include "maklib.h"
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll n=Size();
ll p=(n*MyNodeId())/NumberOfNodes()+1;
ll q=(n*(MyNodeId()+1))/NumberOfNodes()+1;
ll m=0, res=0, x=0, y=0, k=0;
for(int i=p;i<q;++i){
k+=ElementAt(i);
m+=ElementAt(i);
x=max(x, k);
if(m<0) m=0;
else res=max(res, m);
}
m=0;
for(int i=q-1;i>=p;--i){
m+=ElementAt(i);
y=max(y, m);
}
if(MyNodeId()>0){
PutLL(0, res);
PutLL(0, x);
PutLL(0, y);
PutLL(0, k);
Send(0);
}else{
ll res2, x2, y2, k2, m=max(0LL, max(k, y));
for(int i=1;i<NumberOfNodes();++i){
Receive(i);
res2=GetLL(i);
x2=GetLL(i);
y2=GetLL(i);
k2=GetLL(i);
res=max(res, res2);
res=max(res, y+x2);
res=max(res, m+x2);
m+=k2;
if(m<0) m=0;
if(!m) m=max(0LL, y2);
res=max(res, m);
x=x2;
y=y2;
}
printf("%lld",res);
}
}
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 | #include "message.h" #include "maklib.h" #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; int main() { ll n=Size(); ll p=(n*MyNodeId())/NumberOfNodes()+1; ll q=(n*(MyNodeId()+1))/NumberOfNodes()+1; ll m=0, res=0, x=0, y=0, k=0; for(int i=p;i<q;++i){ k+=ElementAt(i); m+=ElementAt(i); x=max(x, k); if(m<0) m=0; else res=max(res, m); } m=0; for(int i=q-1;i>=p;--i){ m+=ElementAt(i); y=max(y, m); } if(MyNodeId()>0){ PutLL(0, res); PutLL(0, x); PutLL(0, y); PutLL(0, k); Send(0); }else{ ll res2, x2, y2, k2, m=max(0LL, max(k, y)); for(int i=1;i<NumberOfNodes();++i){ Receive(i); res2=GetLL(i); x2=GetLL(i); y2=GetLL(i); k2=GetLL(i); res=max(res, res2); res=max(res, y+x2); res=max(res, m+x2); m+=k2; if(m<0) m=0; if(!m) m=max(0LL, y2); res=max(res, m); x=x2; y=y2; } printf("%lld",res); } } |
English