#include <algorithm>
#include <iostream>
#include "maklib.h"
#include "message.h"
using namespace std;
void getMaxSequence(int startIndex, int endIndex, int numberOfNodes)
{
long long currentSequence = 0LL;
long long maxSequence = 0LL;
long long leftMax = 0LL, rightMax = 0LL, nodeMax = 0LL;
bool broken = false;
for (int i = startIndex; i <= endIndex; i++)
{
long long newSum = currentSequence + ElementAt(i + 1);
if (newSum <= 0)
{
if (!broken)
{
leftMax = maxSequence;
broken = true;
}
currentSequence = 0LL;
}
else
{
currentSequence = newSum;
}
maxSequence = max(maxSequence, currentSequence);
}
rightMax = currentSequence;
nodeMax = maxSequence;
if (MyNodeId() > 0)
{
PutLL(0, leftMax);
PutLL(0, nodeMax);
PutLL(0, rightMax);
PutChar(0, broken ? 't' : 'f');
Send(0);
}
else
{
// Count global max
long long globalCurrentSequence = 0LL;
long long globalMaxSequence = 0LL;
// For 0 node
if (broken)
{
globalCurrentSequence = max(globalCurrentSequence + leftMax, nodeMax);
globalMaxSequence = max(globalMaxSequence, globalCurrentSequence);
globalCurrentSequence = rightMax;
}
else
{
globalCurrentSequence = max(0LL, globalCurrentSequence + nodeMax);
globalMaxSequence = max(globalMaxSequence, globalCurrentSequence);
}
for (int node = 1; node < numberOfNodes; node++)
{
Receive(node);
long long currLeftMax = GetLL(node);
long long currNodeMax = GetLL(node);
long long currRightMax = GetLL(node);
bool currBroken = GetChar(node) == 't';
if (currBroken)
{
globalCurrentSequence = max(globalCurrentSequence + currLeftMax, currNodeMax);
globalMaxSequence = max(globalMaxSequence, globalCurrentSequence);
globalCurrentSequence = currRightMax;
}
else
{
globalCurrentSequence = max(0LL, globalCurrentSequence + currNodeMax);
globalMaxSequence = max(globalMaxSequence, globalCurrentSequence);
}
}
// Solution
cout << globalMaxSequence;
}
}
int main() {
int size = Size();
int numberOfNodes = NumberOfNodes();
if (numberOfNodes > size)
{
numberOfNodes = size;
}
if (MyNodeId() >= size)
{
return 0;
}
getMaxSequence(MyNodeId() * size / numberOfNodes,
(MyNodeId() + 1) * size / numberOfNodes - 1,
numberOfNodes);
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <algorithm> #include <iostream> #include "maklib.h" #include "message.h" using namespace std; void getMaxSequence(int startIndex, int endIndex, int numberOfNodes) { long long currentSequence = 0LL; long long maxSequence = 0LL; long long leftMax = 0LL, rightMax = 0LL, nodeMax = 0LL; bool broken = false; for (int i = startIndex; i <= endIndex; i++) { long long newSum = currentSequence + ElementAt(i + 1); if (newSum <= 0) { if (!broken) { leftMax = maxSequence; broken = true; } currentSequence = 0LL; } else { currentSequence = newSum; } maxSequence = max(maxSequence, currentSequence); } rightMax = currentSequence; nodeMax = maxSequence; if (MyNodeId() > 0) { PutLL(0, leftMax); PutLL(0, nodeMax); PutLL(0, rightMax); PutChar(0, broken ? 't' : 'f'); Send(0); } else { // Count global max long long globalCurrentSequence = 0LL; long long globalMaxSequence = 0LL; // For 0 node if (broken) { globalCurrentSequence = max(globalCurrentSequence + leftMax, nodeMax); globalMaxSequence = max(globalMaxSequence, globalCurrentSequence); globalCurrentSequence = rightMax; } else { globalCurrentSequence = max(0LL, globalCurrentSequence + nodeMax); globalMaxSequence = max(globalMaxSequence, globalCurrentSequence); } for (int node = 1; node < numberOfNodes; node++) { Receive(node); long long currLeftMax = GetLL(node); long long currNodeMax = GetLL(node); long long currRightMax = GetLL(node); bool currBroken = GetChar(node) == 't'; if (currBroken) { globalCurrentSequence = max(globalCurrentSequence + currLeftMax, currNodeMax); globalMaxSequence = max(globalMaxSequence, globalCurrentSequence); globalCurrentSequence = currRightMax; } else { globalCurrentSequence = max(0LL, globalCurrentSequence + currNodeMax); globalMaxSequence = max(globalMaxSequence, globalCurrentSequence); } } // Solution cout << globalMaxSequence; } } int main() { int size = Size(); int numberOfNodes = NumberOfNodes(); if (numberOfNodes > size) { numberOfNodes = size; } if (MyNodeId() >= size) { return 0; } getMaxSequence(MyNodeId() * size / numberOfNodes, (MyNodeId() + 1) * size / numberOfNodes - 1, numberOfNodes); return 0; } |
English