#include <iostream> #include <algorithm> #include "message.h" #include "kanapka.h" using namespace std; int main() { int noOfNodes = NumberOfNodes(); int myId = MyNodeId(); long long sanPieces = GetN(); long long maxResult = 0; if (myId == 0) { for (long long i = 0; i < sanPieces; ++i) maxResult += GetTaste(i); maxResult = max(0ll, maxResult); for (int i = 1; i < noOfNodes; ++i) { Receive(i); maxResult = max(GetLL(i), maxResult); } cout << maxResult; } else { if (sanPieces != 1 && myId < sanPieces) { long long startInx = 0, endInx = 0, topSum = 0, bottomSum = 0; // wyznaczenie przedzialu do sprawdzenia int noOfWorkers = (noOfNodes < sanPieces) ? noOfNodes : sanPieces; long long partWidth = sanPieces / noOfWorkers; startInx = (myId - 1) * partWidth; endInx = startInx + partWidth; // wyznaczenie poczatkowej sumy od jednego konca kanapki if (myId > 1) { Receive(myId - 1); topSum = GetLL(myId - 1); for (long long i = startInx - partWidth; i < startInx; ++i) topSum += GetTaste(i); } else for (long long i = 0; i < startInx; ++i) topSum += GetTaste(i); if (myId < noOfWorkers - 1) { PutLL(myId + 1, topSum); Send(myId + 1); } // wyliczenie swojej czesci kanapki maxResult = topSum; for (long long topInx = startInx; topInx < endInx; ++topInx) { topSum += GetTaste(topInx); bottomSum = 0; for (long long downInx = sanPieces; downInx > topInx + 1; --downInx) { if (downInx != sanPieces) { bottomSum += GetTaste(downInx); } maxResult = max(maxResult, topSum + bottomSum); } } } PutLL(0, maxResult); 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 75 76 | #include <iostream> #include <algorithm> #include "message.h" #include "kanapka.h" using namespace std; int main() { int noOfNodes = NumberOfNodes(); int myId = MyNodeId(); long long sanPieces = GetN(); long long maxResult = 0; if (myId == 0) { for (long long i = 0; i < sanPieces; ++i) maxResult += GetTaste(i); maxResult = max(0ll, maxResult); for (int i = 1; i < noOfNodes; ++i) { Receive(i); maxResult = max(GetLL(i), maxResult); } cout << maxResult; } else { if (sanPieces != 1 && myId < sanPieces) { long long startInx = 0, endInx = 0, topSum = 0, bottomSum = 0; // wyznaczenie przedzialu do sprawdzenia int noOfWorkers = (noOfNodes < sanPieces) ? noOfNodes : sanPieces; long long partWidth = sanPieces / noOfWorkers; startInx = (myId - 1) * partWidth; endInx = startInx + partWidth; // wyznaczenie poczatkowej sumy od jednego konca kanapki if (myId > 1) { Receive(myId - 1); topSum = GetLL(myId - 1); for (long long i = startInx - partWidth; i < startInx; ++i) topSum += GetTaste(i); } else for (long long i = 0; i < startInx; ++i) topSum += GetTaste(i); if (myId < noOfWorkers - 1) { PutLL(myId + 1, topSum); Send(myId + 1); } // wyliczenie swojej czesci kanapki maxResult = topSum; for (long long topInx = startInx; topInx < endInx; ++topInx) { topSum += GetTaste(topInx); bottomSum = 0; for (long long downInx = sanPieces; downInx > topInx + 1; --downInx) { if (downInx != sanPieces) { bottomSum += GetTaste(downInx); } maxResult = max(maxResult, topSum + bottomSum); } } } PutLL(0, maxResult); Send(0); } return 0; } |