#include <algorithm> #include <cstdio> #include "maklib.h" #include "message.h" typedef long long LL; int main(){ int numberOfNodes = NumberOfNodes(); int myNodeId = MyNodeId(); LL size = Size(); int begin = (size * myNodeId) / numberOfNodes + 1; int end = (size * (myNodeId + 1)) / numberOfNodes + 1; LL maxInfixSum = 0; LL maxPrefixSum = 0; LL maxSuffixSum = 0; LL totalSum = 0; LL minPrefixSum = 0; for(int i = begin; i < end; i++) { totalSum += ElementAt(i); maxPrefixSum = std::max(maxPrefixSum, totalSum); minPrefixSum = std::min(minPrefixSum, totalSum); maxInfixSum = std::max(maxInfixSum, totalSum - minPrefixSum); } maxSuffixSum = totalSum - minPrefixSum; for (int i = 1; i <= numberOfNodes; i <<= 1){ int other = myNodeId ^ i; if(other >= numberOfNodes) continue; if(myNodeId & i){ PutLL(other, maxInfixSum); PutLL(other, maxPrefixSum); PutLL(other, maxSuffixSum); PutLL(other, totalSum); Send(other); return 0; } else { Receive(other); LL maxInfixSum2 = GetLL(other); LL maxPrefixSum2 = GetLL(other); LL maxSuffixSum2 = GetLL(other); LL totalSum2 = GetLL(other); maxInfixSum = std::max(maxInfixSum, std::max(maxInfixSum2, maxSuffixSum + maxPrefixSum2)); maxPrefixSum = std::max(maxPrefixSum, totalSum + maxPrefixSum2); maxSuffixSum = std::max(maxSuffixSum2, maxSuffixSum + totalSum2); totalSum += totalSum2; } } printf("%lld\n", maxInfixSum); }
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 | #include <algorithm> #include <cstdio> #include "maklib.h" #include "message.h" typedef long long LL; int main(){ int numberOfNodes = NumberOfNodes(); int myNodeId = MyNodeId(); LL size = Size(); int begin = (size * myNodeId) / numberOfNodes + 1; int end = (size * (myNodeId + 1)) / numberOfNodes + 1; LL maxInfixSum = 0; LL maxPrefixSum = 0; LL maxSuffixSum = 0; LL totalSum = 0; LL minPrefixSum = 0; for(int i = begin; i < end; i++) { totalSum += ElementAt(i); maxPrefixSum = std::max(maxPrefixSum, totalSum); minPrefixSum = std::min(minPrefixSum, totalSum); maxInfixSum = std::max(maxInfixSum, totalSum - minPrefixSum); } maxSuffixSum = totalSum - minPrefixSum; for (int i = 1; i <= numberOfNodes; i <<= 1){ int other = myNodeId ^ i; if(other >= numberOfNodes) continue; if(myNodeId & i){ PutLL(other, maxInfixSum); PutLL(other, maxPrefixSum); PutLL(other, maxSuffixSum); PutLL(other, totalSum); Send(other); return 0; } else { Receive(other); LL maxInfixSum2 = GetLL(other); LL maxPrefixSum2 = GetLL(other); LL maxSuffixSum2 = GetLL(other); LL totalSum2 = GetLL(other); maxInfixSum = std::max(maxInfixSum, std::max(maxInfixSum2, maxSuffixSum + maxPrefixSum2)); maxPrefixSum = std::max(maxPrefixSum, totalSum + maxPrefixSum2); maxSuffixSum = std::max(maxSuffixSum2, maxSuffixSum + totalSum2); totalSum += totalSum2; } } printf("%lld\n", maxInfixSum); } |