#include <iostream>
#include "maklib.h"
#include "message.h"
using namespace std;
int main() {
//ios_base::sync_with_stdio(0);
long long N, w=0, pocz=0, kon=0, sum=0, wyn=0, mod, mdo, a;
long long imax, ipocz, ikon, iwyn, isum;
int i;
N=Size();
mod=(MyNodeId() * N) / NumberOfNodes();
mdo=((MyNodeId()+1) * N) / NumberOfNodes();
for (i=mod; i<mdo; i++) {
a=ElementAt(i);
if (w<0) w=a;
else w=w+a;
if (wyn<w) wyn=w;
sum=sum+a;
if (sum>pocz) pocz=sum;
if (sum<kon) kon=sum;
}
kon=sum-kon;
if (MyNodeId() > 0) {
PutLL(0, wyn);
PutLL(0, pocz);
PutLL(0, kon);
PutLL(0, sum);
Send(0);
}
else {
w=kon;
iwyn=wyn;
for (int instancja = 1; instancja < NumberOfNodes(); ++instancja) {
Receive(instancja);
imax = GetLL(instancja);
ipocz = GetLL(instancja);
ikon = GetLL(instancja);
isum = GetLL(instancja);
if (iwyn<imax) {iwyn=imax;}
if (iwyn<w+ipocz) {iwyn=w+ipocz;}
if (w+isum<ikon) {w=ikon;}
else {w=w+isum;}
}
cout << iwyn << endl;
}
// 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 | #include <iostream> #include "maklib.h" #include "message.h" using namespace std; int main() { //ios_base::sync_with_stdio(0); long long N, w=0, pocz=0, kon=0, sum=0, wyn=0, mod, mdo, a; long long imax, ipocz, ikon, iwyn, isum; int i; N=Size(); mod=(MyNodeId() * N) / NumberOfNodes(); mdo=((MyNodeId()+1) * N) / NumberOfNodes(); for (i=mod; i<mdo; i++) { a=ElementAt(i); if (w<0) w=a; else w=w+a; if (wyn<w) wyn=w; sum=sum+a; if (sum>pocz) pocz=sum; if (sum<kon) kon=sum; } kon=sum-kon; if (MyNodeId() > 0) { PutLL(0, wyn); PutLL(0, pocz); PutLL(0, kon); PutLL(0, sum); Send(0); } else { w=kon; iwyn=wyn; for (int instancja = 1; instancja < NumberOfNodes(); ++instancja) { Receive(instancja); imax = GetLL(instancja); ipocz = GetLL(instancja); ikon = GetLL(instancja); isum = GetLL(instancja); if (iwyn<imax) {iwyn=imax;} if (iwyn<w+ipocz) {iwyn=w+ipocz;} if (w+isum<ikon) {w=ikon;} else {w=w+isum;} } cout << iwyn << endl; } // return 0; } |
English