#include "kanapka.h" #include "message.h" #include <algorithm> #include <cstdio> using namespace std; long long segmentSum(int first, int last) { long long sum = 0; for (int i = first; i <= last; ++i) sum += GetTaste(i); return sum; } long long segmentMinSubsequence(int first, int last) { long long minSubsequence = 0, tempSubsequence = 0; for (int i = first; i <= last; ++i) { tempSubsequence += GetTaste(i); if (tempSubsequence > GetTaste(i)) tempSubsequence = GetTaste(i); minSubsequence = min(minSubsequence, tempSubsequence); } return minSubsequence; } long long segmentMinPrefix(int first, int last) { long long minPrefix = 0, tempPrefix = 0; for (int i = first; i <= last; ++i) { tempPrefix += GetTaste(i); minPrefix = min(minPrefix, tempPrefix); } return minPrefix; } long long segmentMinSufix(int first, int last) { long long minSufix = 0, tempSufix = 0; for (int i = last; i >= first; --i) { tempSufix += GetTaste(i); minSufix = min(minSufix, tempSufix); } return minSufix; } int main() { int numOfNodes = NumberOfNodes(); int nodeId = MyNodeId(); int n = GetN(); if (n < numOfNodes) { if (nodeId == 0) printf("%lld", segmentSum(0, n - 1) - segmentMinSubsequence(0, n - 1)); return 0; } int first = n / numOfNodes * nodeId; int last; if (nodeId == numOfNodes - 1) last = n - 1; else last = first + n / numOfNodes - 1; long long sum = segmentSum(first, last); long long minSubsequence = segmentMinSubsequence(first, last); long long minPrefix = segmentMinPrefix(first, last); long long minSufix = segmentMinSufix(first, last); if (nodeId == 0) { for (int i = 1; i < numOfNodes; ++i) { Receive(i); long long tempSegmentSum = GetLL(i); long long tempSegmentMinSubsequence = GetLL(i); long long tempSegmentMinPrefix = GetLL(i); long long tempSegmentMinSufix = GetLL(i); sum += tempSegmentSum; minSubsequence = min(minSubsequence, min(tempSegmentMinSubsequence, minSufix + tempSegmentMinPrefix)); minSufix = min(tempSegmentMinSufix, tempSegmentSum + minSufix); } printf("%lld", sum - minSubsequence); } else { PutLL(0, sum); PutLL(0, minSubsequence); PutLL(0, minPrefix); PutLL(0, minSufix); 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 77 78 79 80 81 82 83 84 | #include "kanapka.h" #include "message.h" #include <algorithm> #include <cstdio> using namespace std; long long segmentSum(int first, int last) { long long sum = 0; for (int i = first; i <= last; ++i) sum += GetTaste(i); return sum; } long long segmentMinSubsequence(int first, int last) { long long minSubsequence = 0, tempSubsequence = 0; for (int i = first; i <= last; ++i) { tempSubsequence += GetTaste(i); if (tempSubsequence > GetTaste(i)) tempSubsequence = GetTaste(i); minSubsequence = min(minSubsequence, tempSubsequence); } return minSubsequence; } long long segmentMinPrefix(int first, int last) { long long minPrefix = 0, tempPrefix = 0; for (int i = first; i <= last; ++i) { tempPrefix += GetTaste(i); minPrefix = min(minPrefix, tempPrefix); } return minPrefix; } long long segmentMinSufix(int first, int last) { long long minSufix = 0, tempSufix = 0; for (int i = last; i >= first; --i) { tempSufix += GetTaste(i); minSufix = min(minSufix, tempSufix); } return minSufix; } int main() { int numOfNodes = NumberOfNodes(); int nodeId = MyNodeId(); int n = GetN(); if (n < numOfNodes) { if (nodeId == 0) printf("%lld", segmentSum(0, n - 1) - segmentMinSubsequence(0, n - 1)); return 0; } int first = n / numOfNodes * nodeId; int last; if (nodeId == numOfNodes - 1) last = n - 1; else last = first + n / numOfNodes - 1; long long sum = segmentSum(first, last); long long minSubsequence = segmentMinSubsequence(first, last); long long minPrefix = segmentMinPrefix(first, last); long long minSufix = segmentMinSufix(first, last); if (nodeId == 0) { for (int i = 1; i < numOfNodes; ++i) { Receive(i); long long tempSegmentSum = GetLL(i); long long tempSegmentMinSubsequence = GetLL(i); long long tempSegmentMinPrefix = GetLL(i); long long tempSegmentMinSufix = GetLL(i); sum += tempSegmentSum; minSubsequence = min(minSubsequence, min(tempSegmentMinSubsequence, minSufix + tempSegmentMinPrefix)); minSufix = min(tempSegmentMinSufix, tempSegmentSum + minSufix); } printf("%lld", sum - minSubsequence); } else { PutLL(0, sum); PutLL(0, minSubsequence); PutLL(0, minPrefix); PutLL(0, minSufix); Send(0); } return 0; } |