#include "maklib.h"
#include "message.h"
#include <algorithm>
#include <iostream>
using namespace std;
const int P = 1E5;
int ins, nr, dtab, pocz, kon, n;
long long zak, roz, naj, suma;
void licz_fragment()
{
n = (dtab-1)/ins+1;
pocz = nr*n+1;
kon = min(dtab, pocz+n-1);
long long temp = 0;
for (int i = pocz; i <= kon; i++)
{
temp += ElementAt(i);
zak = max(temp, zak);
}
temp = 0;
for (int i = kon; i >= pocz; i--)
{
temp += ElementAt(i);
roz = max(temp, roz);
}
temp = 0;
for (int i = pocz; i <= kon; i++)
{
temp += ElementAt(i);
naj = max(temp, naj);
if (temp < 0)
temp = 0;
}
for (int i = pocz; i <= kon; i++)
suma += ElementAt(i);
}
void wyslij()
{
PutLL(0, zak);
PutLL(0, naj);
PutLL(0, suma);
PutLL(0, roz);
Send(0);
}
void caly_wynik()
{
long long roz1, naj1, zak1, suma1, wyn = naj, og = roz;
for (int i = 1; i < ins; i++)
{
Receive(i);
zak1 = GetLL(i);
wyn = max(wyn, og+zak1);
naj1 = GetLL(i);
wyn = max(wyn, naj1);
suma1 = GetLL(i);
roz1 = GetLL(i);
og = max(og+suma1, roz1);
}
cout << wyn << "\n";
}
int main()
{
ins = NumberOfNodes();
nr = MyNodeId();
dtab = Size();
ins = min(ins, (dtab-1)/P+1);
if (ins <= nr)
return 0;
licz_fragment();
if (nr > 0)
wyslij();
else
caly_wynik();
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 | #include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; const int P = 1E5; int ins, nr, dtab, pocz, kon, n; long long zak, roz, naj, suma; void licz_fragment() { n = (dtab-1)/ins+1; pocz = nr*n+1; kon = min(dtab, pocz+n-1); long long temp = 0; for (int i = pocz; i <= kon; i++) { temp += ElementAt(i); zak = max(temp, zak); } temp = 0; for (int i = kon; i >= pocz; i--) { temp += ElementAt(i); roz = max(temp, roz); } temp = 0; for (int i = pocz; i <= kon; i++) { temp += ElementAt(i); naj = max(temp, naj); if (temp < 0) temp = 0; } for (int i = pocz; i <= kon; i++) suma += ElementAt(i); } void wyslij() { PutLL(0, zak); PutLL(0, naj); PutLL(0, suma); PutLL(0, roz); Send(0); } void caly_wynik() { long long roz1, naj1, zak1, suma1, wyn = naj, og = roz; for (int i = 1; i < ins; i++) { Receive(i); zak1 = GetLL(i); wyn = max(wyn, og+zak1); naj1 = GetLL(i); wyn = max(wyn, naj1); suma1 = GetLL(i); roz1 = GetLL(i); og = max(og+suma1, roz1); } cout << wyn << "\n"; } int main() { ins = NumberOfNodes(); nr = MyNodeId(); dtab = Size(); ins = min(ins, (dtab-1)/P+1); if (ins <= nr) return 0; licz_fragment(); if (nr > 0) wyslij(); else caly_wynik(); return 0; } |
English