#include <cstdio> #include <algorithm> #include "maklib.h" #include "message.h" using namespace std; int n; int nodes; int myId; int left, right; long long total, smallest, result, totalBefore, smallestBefore; int ceil(int p, int q) { if(p % q == 0) return p/q; return p/q + 1; } void getLeft() { left = ceil(n, nodes) * myId + 1; } void getRight() { right = min(ceil(n,nodes) * (myId + 1), n); } void getTotal() { total = 0; totalBefore = 0; for(int i = left;i<=right;i++) total += ElementAt(i); if(myId != 0) { Receive(myId - 1); totalBefore = GetLL(myId - 1); total += totalBefore; } if(myId != nodes - 1) { PutLL(myId + 1, total); Send(myId + 1); } } void getSmallest() { smallest = 0; long long temp = totalBefore; for(int i = left;i<=right;i++) { temp += ElementAt(i); smallest = min(temp, smallest); } if(myId != 0) { Receive(myId - 1); smallestBefore = GetLL(myId - 1); smallest = min(smallest, smallestBefore); } if(myId != nodes - 1) { PutLL(myId + 1, smallest); Send(myId + 1); } } void getResult() { result = 0; smallest = smallestBefore; long long temp = totalBefore; for(int i = left;i<=right;i++) { temp += ElementAt(i); smallest = min(temp, smallest); result = max(result, temp - smallest); } if(myId != 0) { Receive(myId - 1); result = max(result, GetLL(myId - 1)); } if(myId != nodes - 1) { PutLL(myId + 1, result); Send(myId + 1); } } int main() { nodes = NumberOfNodes(); myId = MyNodeId(); n = Size(); getLeft(); getRight(); getTotal(); getSmallest(); getResult(); if(myId == nodes - 1) printf("%lld\n", result); 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 107 108 | #include <cstdio> #include <algorithm> #include "maklib.h" #include "message.h" using namespace std; int n; int nodes; int myId; int left, right; long long total, smallest, result, totalBefore, smallestBefore; int ceil(int p, int q) { if(p % q == 0) return p/q; return p/q + 1; } void getLeft() { left = ceil(n, nodes) * myId + 1; } void getRight() { right = min(ceil(n,nodes) * (myId + 1), n); } void getTotal() { total = 0; totalBefore = 0; for(int i = left;i<=right;i++) total += ElementAt(i); if(myId != 0) { Receive(myId - 1); totalBefore = GetLL(myId - 1); total += totalBefore; } if(myId != nodes - 1) { PutLL(myId + 1, total); Send(myId + 1); } } void getSmallest() { smallest = 0; long long temp = totalBefore; for(int i = left;i<=right;i++) { temp += ElementAt(i); smallest = min(temp, smallest); } if(myId != 0) { Receive(myId - 1); smallestBefore = GetLL(myId - 1); smallest = min(smallest, smallestBefore); } if(myId != nodes - 1) { PutLL(myId + 1, smallest); Send(myId + 1); } } void getResult() { result = 0; smallest = smallestBefore; long long temp = totalBefore; for(int i = left;i<=right;i++) { temp += ElementAt(i); smallest = min(temp, smallest); result = max(result, temp - smallest); } if(myId != 0) { Receive(myId - 1); result = max(result, GetLL(myId - 1)); } if(myId != nodes - 1) { PutLL(myId + 1, result); Send(myId + 1); } } int main() { nodes = NumberOfNodes(); myId = MyNodeId(); n = Size(); getLeft(); getRight(); getTotal(); getSmallest(); getResult(); if(myId == nodes - 1) printf("%lld\n", result); return 0; } |