#include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> #include <cmath> using namespace std; int main() { int size = Size(); int id = MyNodeId(); int nodes = NumberOfNodes(); int rangeSize = ceil((double)size / (double)nodes); int start = id * rangeSize; int end = min(start + rangeSize, size); long long wEnd = 0, wStart = 0; long long wyn = 0, sumAll = 0; for(int i = start; i < end; i++){ int a = ElementAt(i+1); sumAll += a; if(wEnd > 0) wEnd += a; else wEnd = a; if(wEnd > wyn) wyn = wEnd; } for(int i = end - 1; i >= start; i--){ int a = ElementAt(i+1); if(wStart > 0) wStart += a; else wStart = a; } if(id > 0){ PutLL(0, wyn); PutLL(0, wStart); PutLL(0, wEnd); PutLL(0, sumAll); Send(0); }else{ long long *arrWyn = new long long[nodes]; long long *arrStart = new long long[nodes]; long long *arrEnd = new long long[nodes]; long long *arrSum = new long long[nodes]; arrWyn[0] = wyn; arrStart[0] = wStart; arrEnd[0] = wEnd; arrSum[0] = sumAll; for(int i=1; i<nodes; i++){ Receive(i); arrWyn[i] = GetLL(i); arrStart[i] = GetLL(i); arrEnd[i] = GetLL(i); arrSum[i] = GetLL(i); } long long result = 0; //cout << rangeSize << endl; for(int i = 0; i < nodes; i++){ if(arrWyn[i] > result) result = arrWyn[i]; //cout << arrWyn[i] << " " << arrStart[i] << " " << arrEnd[i] << " " << arrSum[i] << endl; long long sum = arrEnd[i]; for(int j = i + 1; j < nodes; j++){ if(sum + arrStart[j] > result) result = sum + arrStart[j]; sum += arrSum[j]; } } delete [] arrWyn; delete [] arrStart; delete [] arrEnd; cout << result << endl; } 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 | #include "maklib.h" #include "message.h" #include <algorithm> #include <iostream> #include <cmath> using namespace std; int main() { int size = Size(); int id = MyNodeId(); int nodes = NumberOfNodes(); int rangeSize = ceil((double)size / (double)nodes); int start = id * rangeSize; int end = min(start + rangeSize, size); long long wEnd = 0, wStart = 0; long long wyn = 0, sumAll = 0; for(int i = start; i < end; i++){ int a = ElementAt(i+1); sumAll += a; if(wEnd > 0) wEnd += a; else wEnd = a; if(wEnd > wyn) wyn = wEnd; } for(int i = end - 1; i >= start; i--){ int a = ElementAt(i+1); if(wStart > 0) wStart += a; else wStart = a; } if(id > 0){ PutLL(0, wyn); PutLL(0, wStart); PutLL(0, wEnd); PutLL(0, sumAll); Send(0); }else{ long long *arrWyn = new long long[nodes]; long long *arrStart = new long long[nodes]; long long *arrEnd = new long long[nodes]; long long *arrSum = new long long[nodes]; arrWyn[0] = wyn; arrStart[0] = wStart; arrEnd[0] = wEnd; arrSum[0] = sumAll; for(int i=1; i<nodes; i++){ Receive(i); arrWyn[i] = GetLL(i); arrStart[i] = GetLL(i); arrEnd[i] = GetLL(i); arrSum[i] = GetLL(i); } long long result = 0; //cout << rangeSize << endl; for(int i = 0; i < nodes; i++){ if(arrWyn[i] > result) result = arrWyn[i]; //cout << arrWyn[i] << " " << arrStart[i] << " " << arrEnd[i] << " " << arrSum[i] << endl; long long sum = arrEnd[i]; for(int j = i + 1; j < nodes; j++){ if(sum + arrStart[j] > result) result = sum + arrStart[j]; sum += arrSum[j]; } } delete [] arrWyn; delete [] arrStart; delete [] arrEnd; cout << result << endl; } return 0; } |