#include "kanapka.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; struct Summary { long long total; long long prefix; long long suffix; long long mixed; Summary(const Summary&) = default; Summary(long long x = 0ll): total(x), prefix(max(0ll, x)), suffix(max(0ll, x)), mixed(max(0ll, x)){}; void Send(int target) { } int Receive(int source) { } }; Summary Append(const Summary& s1, const Summary& s2) { Summary s; s.total = s1.total + s2.total; s.prefix = max(s1.prefix, s1.total + s2.prefix); s.suffix = max(s1.suffix + s2.total, s2.suffix); s.mixed = max({s1.mixed + s2.total, s1.prefix + s2.suffix, s1.total + s2.mixed}); return s; } Summary RangeSummary(long long i_first, long long i_last) { Summary s; for (long long i = i_first; i < i_last; ++i) { s = Append(s, Summary(GetTaste(i))); } return s; } int main() { int num_nodes = NumberOfNodes(); int my_node_id = MyNodeId(); long long N = GetN(); long long i_first = my_node_id * N / num_nodes; long long i_last = (my_node_id + 1) * N / num_nodes; Summary s = RangeSummary(i_first, i_last); if (my_node_id != 0) { PutLL(0, s.total); PutLL(0, s.prefix); PutLL(0, s.suffix); PutLL(0, s.mixed); Send(0); } else { for (int i = 1; i < num_nodes; ++i) { Summary t; Receive(i); t.total = GetLL(i); t.prefix = GetLL(i); t.suffix = GetLL(i); t.mixed = GetLL(i); s = Append(s, t); } cout << s.mixed << '\n'; } 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 | #include "kanapka.h" #include "message.h" #include <algorithm> #include <iostream> using namespace std; struct Summary { long long total; long long prefix; long long suffix; long long mixed; Summary(const Summary&) = default; Summary(long long x = 0ll): total(x), prefix(max(0ll, x)), suffix(max(0ll, x)), mixed(max(0ll, x)){}; void Send(int target) { } int Receive(int source) { } }; Summary Append(const Summary& s1, const Summary& s2) { Summary s; s.total = s1.total + s2.total; s.prefix = max(s1.prefix, s1.total + s2.prefix); s.suffix = max(s1.suffix + s2.total, s2.suffix); s.mixed = max({s1.mixed + s2.total, s1.prefix + s2.suffix, s1.total + s2.mixed}); return s; } Summary RangeSummary(long long i_first, long long i_last) { Summary s; for (long long i = i_first; i < i_last; ++i) { s = Append(s, Summary(GetTaste(i))); } return s; } int main() { int num_nodes = NumberOfNodes(); int my_node_id = MyNodeId(); long long N = GetN(); long long i_first = my_node_id * N / num_nodes; long long i_last = (my_node_id + 1) * N / num_nodes; Summary s = RangeSummary(i_first, i_last); if (my_node_id != 0) { PutLL(0, s.total); PutLL(0, s.prefix); PutLL(0, s.suffix); PutLL(0, s.mixed); Send(0); } else { for (int i = 1; i < num_nodes; ++i) { Summary t; Receive(i); t.total = GetLL(i); t.prefix = GetLL(i); t.suffix = GetLL(i); t.mixed = GetLL(i); s = Append(s, t); } cout << s.mixed << '\n'; } return 0; } |