#include<algorithm> #include<iostream> #include "maklib.h" #include "message.h" int main() { int number = NumberOfNodes(); int id = MyNodeId(); int size = Size(); if(id >= size) return 0; if(number > size) number = size; int fragsize = size / number; int start = id * fragsize + 1; int end; if(id == number - 1) end = size; else end = (id + 1) * fragsize; long long tmp = 0; long long max = 0; long long sum = 0; long long pref = 0; for(int i = start; i <= end; ++i) { long long elem = ElementAt(i); tmp += elem; tmp = std::max(0LL, tmp); max = std::max(tmp, max); sum += elem; pref = std::max(sum, pref); } tmp = 0; long long suf = 0; for(int i = end; i >= start; --i) { tmp += ElementAt(i); suf = std::max(tmp, suf); } for(int iter = id, count = 0; (1 << count) < number; iter /= 2, ++count) { if(iter % 2 == 0) { int source = id + (1 << count); if(source >= number) continue; Receive(source); long long smax = GetLL(source); long long ssum = GetLL(source); long long spref = GetLL(source); long long ssuf = GetLL(source); max = std::max(max, std::max(smax, suf + spref)); pref = std::max(pref, sum + spref); suf = std::max(ssuf, ssum + suf); sum += ssum; } else { int target = id - (1 << count); PutLL(target, max); PutLL(target, sum); PutLL(target, pref); PutLL(target, suf); Send(target); return 0; } } std::ios_base::sync_with_stdio(false); std::cout << max << std::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 | #include<algorithm> #include<iostream> #include "maklib.h" #include "message.h" int main() { int number = NumberOfNodes(); int id = MyNodeId(); int size = Size(); if(id >= size) return 0; if(number > size) number = size; int fragsize = size / number; int start = id * fragsize + 1; int end; if(id == number - 1) end = size; else end = (id + 1) * fragsize; long long tmp = 0; long long max = 0; long long sum = 0; long long pref = 0; for(int i = start; i <= end; ++i) { long long elem = ElementAt(i); tmp += elem; tmp = std::max(0LL, tmp); max = std::max(tmp, max); sum += elem; pref = std::max(sum, pref); } tmp = 0; long long suf = 0; for(int i = end; i >= start; --i) { tmp += ElementAt(i); suf = std::max(tmp, suf); } for(int iter = id, count = 0; (1 << count) < number; iter /= 2, ++count) { if(iter % 2 == 0) { int source = id + (1 << count); if(source >= number) continue; Receive(source); long long smax = GetLL(source); long long ssum = GetLL(source); long long spref = GetLL(source); long long ssuf = GetLL(source); max = std::max(max, std::max(smax, suf + spref)); pref = std::max(pref, sum + spref); suf = std::max(ssuf, ssum + suf); sum += ssum; } else { int target = id - (1 << count); PutLL(target, max); PutLL(target, sum); PutLL(target, pref); PutLL(target, suf); Send(target); return 0; } } std::ios_base::sync_with_stdio(false); std::cout << max << std::endl; return 0; } |