#include "message.h"
#include "maklib.h"
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
int main() {
int n = Size();
if (n < NumberOfNodes() * 2) {
if (MyNodeId() == 0) {
LL aktualnie = 0;
LL maksymalnie = 0;
for (int i=1; i<=n; ++i) {
aktualnie += ElementAt(i);
if (aktualnie < 0) aktualnie = 0;
if (aktualnie > maksymalnie) maksymalnie = aktualnie;
}
cout << maksymalnie << endl;
}
} else {
int np = n/NumberOfNodes();
int start = MyNodeId() * np + 1;
int stop = (MyNodeId() + 1) * np;
if (MyNodeId() == NumberOfNodes() - 1) {
stop = n;
}
int status = 0;
LL aktualnie = 0;
LL maksymalnie = 0;
LL lewo = 0;
LL suma = 0;
for (int i = start ; i <= stop; ++i) {
suma += ElementAt(i);
aktualnie += ElementAt(i);
if (aktualnie < 0) {
aktualnie = 0;
status = 1;
}
if (aktualnie > maksymalnie) {
maksymalnie = aktualnie;
if (status == 0) {
lewo = maksymalnie;
}
}
}
// cout << "Id: " << MyNodeId()<<" m:" << maksymalnie << " l:"<< lewo<<" a:" << aktualnie<<" s:"<<suma<< " b:" <<start<<" e:"<<stop<<" "<<endl;
if (MyNodeId()==0) {
for (int i=1;i<NumberOfNodes();++i) {
Receive(i);
LL recvS = GetLL(i);
LL recvM = GetLL(i);
LL recvL = GetLL(i);
LL recvP = GetLL(i);
if (recvM > maksymalnie) maksymalnie = recvM;
if (aktualnie + recvL > maksymalnie) maksymalnie = aktualnie+recvL;
if (aktualnie + recvS > maksymalnie) maksymalnie = aktualnie+recvS;
if (aktualnie + recvS < 0) aktualnie = recvP;
else aktualnie += recvS;
}
cout << maksymalnie << endl;
} else {
PutLL(0, suma);
PutLL(0, maksymalnie);
PutLL(0, lewo);
PutLL(0, aktualnie);
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 72 73 74 | #include "message.h" #include "maklib.h" #include <algorithm> #include <iostream> using namespace std; typedef long long LL; int main() { int n = Size(); if (n < NumberOfNodes() * 2) { if (MyNodeId() == 0) { LL aktualnie = 0; LL maksymalnie = 0; for (int i=1; i<=n; ++i) { aktualnie += ElementAt(i); if (aktualnie < 0) aktualnie = 0; if (aktualnie > maksymalnie) maksymalnie = aktualnie; } cout << maksymalnie << endl; } } else { int np = n/NumberOfNodes(); int start = MyNodeId() * np + 1; int stop = (MyNodeId() + 1) * np; if (MyNodeId() == NumberOfNodes() - 1) { stop = n; } int status = 0; LL aktualnie = 0; LL maksymalnie = 0; LL lewo = 0; LL suma = 0; for (int i = start ; i <= stop; ++i) { suma += ElementAt(i); aktualnie += ElementAt(i); if (aktualnie < 0) { aktualnie = 0; status = 1; } if (aktualnie > maksymalnie) { maksymalnie = aktualnie; if (status == 0) { lewo = maksymalnie; } } } // cout << "Id: " << MyNodeId()<<" m:" << maksymalnie << " l:"<< lewo<<" a:" << aktualnie<<" s:"<<suma<< " b:" <<start<<" e:"<<stop<<" "<<endl; if (MyNodeId()==0) { for (int i=1;i<NumberOfNodes();++i) { Receive(i); LL recvS = GetLL(i); LL recvM = GetLL(i); LL recvL = GetLL(i); LL recvP = GetLL(i); if (recvM > maksymalnie) maksymalnie = recvM; if (aktualnie + recvL > maksymalnie) maksymalnie = aktualnie+recvL; if (aktualnie + recvS > maksymalnie) maksymalnie = aktualnie+recvS; if (aktualnie + recvS < 0) aktualnie = recvP; else aktualnie += recvS; } cout << maksymalnie << endl; } else { PutLL(0, suma); PutLL(0, maksymalnie); PutLL(0, lewo); PutLL(0, aktualnie); Send(0); } } return 0; } |
English