#include <algorithm> #include <cstdio> #include <utility> #include <vector> #include "kanapka.h" #include "message.h" class KanSolutionSingle { public: KanSolutionSingle(const int sandwich_length_) : sandwich_length(sandwich_length_) {} void solve() { long long current_max = 0LL; long long sum = 0LL; long long wasted = 0LL; for(int i = 0; i < sandwich_length; ++i) { sum += GetTaste(i); wasted = std::min(wasted, sum - current_max); current_max = std::max(current_max, sum); } printf("%lld\n", sum - wasted); } private: const int sandwich_length; }; std::pair<int, int> get_range(const int node_id, const int sandwich_length) { const int nodes = NumberOfNodes(); const int length_per_node = sandwich_length / nodes; if(node_id == (nodes-1)) return std::make_pair(node_id * length_per_node, sandwich_length-1); return std::make_pair(node_id * length_per_node, (node_id+1) * length_per_node - 1); } struct NodeResult { NodeResult() : wasted_in_node(0LL), max_sum(0LL), min_sum(0LL), sum(0LL) {} long long wasted_in_node; long long max_sum; long long min_sum; long long sum; }; NodeResult get_node_result(const int from, const int to) { NodeResult result; for(int i = from; i <= to; ++i) { result.sum += GetTaste(i); result.wasted_in_node = std::min(result.wasted_in_node, result.sum - result.max_sum); result.max_sum = std::max(result.max_sum, result.sum); result.min_sum = std::min(result.min_sum, result.sum); } return result; } class KanSolutionMaster { public: KanSolutionMaster(const int sandwich_length) : from(get_range(MyNodeId(), sandwich_length).first), to(get_range(MyNodeId(), sandwich_length).second) {} void solve() { std::vector<NodeResult> node_results; node_results.push_back(get_node_result(from, to)); for(int node_id = 1; node_id < NumberOfNodes(); ++node_id) { Receive(node_id); NodeResult node_result; node_result.sum = GetLL(node_id); node_result.wasted_in_node = GetLL(node_id); node_result.max_sum = GetLL(node_id); node_result.min_sum = GetLL(node_id); node_results.push_back(node_result); } long long current_max = 0LL; long long sum = 0LL; long long wasted = 0LL; for(const auto& res : node_results) { wasted = std::min(wasted, res.wasted_in_node); wasted = std::min(wasted, sum + res.min_sum - current_max); current_max = std::max(current_max, sum + res.max_sum); sum += res.sum; } printf("%lld\n", sum - wasted); } private: const int from; const int to; }; class KanSolutionSlave { public: KanSolutionSlave(const int sandwich_length) : from(get_range(MyNodeId(), sandwich_length).first), to(get_range(MyNodeId(), sandwich_length).second) {} void solve() { const auto node_result = get_node_result(from, to); PutLL(0, node_result.sum); PutLL(0, node_result.wasted_in_node); PutLL(0, node_result.max_sum); PutLL(0, node_result.min_sum); Send(0); } private: const int from; const int to; }; int main() { const int sandwich_length = GetN(); if(sandwich_length < 10000) { if(MyNodeId() == 0) { KanSolutionSingle solution(sandwich_length); solution.solve(); } } else { if(MyNodeId() == 0) { KanSolutionMaster solution(sandwich_length); solution.solve(); } else { KanSolutionSlave solution(sandwich_length); solution.solve(); } } }
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | #include <algorithm> #include <cstdio> #include <utility> #include <vector> #include "kanapka.h" #include "message.h" class KanSolutionSingle { public: KanSolutionSingle(const int sandwich_length_) : sandwich_length(sandwich_length_) {} void solve() { long long current_max = 0LL; long long sum = 0LL; long long wasted = 0LL; for(int i = 0; i < sandwich_length; ++i) { sum += GetTaste(i); wasted = std::min(wasted, sum - current_max); current_max = std::max(current_max, sum); } printf("%lld\n", sum - wasted); } private: const int sandwich_length; }; std::pair<int, int> get_range(const int node_id, const int sandwich_length) { const int nodes = NumberOfNodes(); const int length_per_node = sandwich_length / nodes; if(node_id == (nodes-1)) return std::make_pair(node_id * length_per_node, sandwich_length-1); return std::make_pair(node_id * length_per_node, (node_id+1) * length_per_node - 1); } struct NodeResult { NodeResult() : wasted_in_node(0LL), max_sum(0LL), min_sum(0LL), sum(0LL) {} long long wasted_in_node; long long max_sum; long long min_sum; long long sum; }; NodeResult get_node_result(const int from, const int to) { NodeResult result; for(int i = from; i <= to; ++i) { result.sum += GetTaste(i); result.wasted_in_node = std::min(result.wasted_in_node, result.sum - result.max_sum); result.max_sum = std::max(result.max_sum, result.sum); result.min_sum = std::min(result.min_sum, result.sum); } return result; } class KanSolutionMaster { public: KanSolutionMaster(const int sandwich_length) : from(get_range(MyNodeId(), sandwich_length).first), to(get_range(MyNodeId(), sandwich_length).second) {} void solve() { std::vector<NodeResult> node_results; node_results.push_back(get_node_result(from, to)); for(int node_id = 1; node_id < NumberOfNodes(); ++node_id) { Receive(node_id); NodeResult node_result; node_result.sum = GetLL(node_id); node_result.wasted_in_node = GetLL(node_id); node_result.max_sum = GetLL(node_id); node_result.min_sum = GetLL(node_id); node_results.push_back(node_result); } long long current_max = 0LL; long long sum = 0LL; long long wasted = 0LL; for(const auto& res : node_results) { wasted = std::min(wasted, res.wasted_in_node); wasted = std::min(wasted, sum + res.min_sum - current_max); current_max = std::max(current_max, sum + res.max_sum); sum += res.sum; } printf("%lld\n", sum - wasted); } private: const int from; const int to; }; class KanSolutionSlave { public: KanSolutionSlave(const int sandwich_length) : from(get_range(MyNodeId(), sandwich_length).first), to(get_range(MyNodeId(), sandwich_length).second) {} void solve() { const auto node_result = get_node_result(from, to); PutLL(0, node_result.sum); PutLL(0, node_result.wasted_in_node); PutLL(0, node_result.max_sum); PutLL(0, node_result.min_sum); Send(0); } private: const int from; const int to; }; int main() { const int sandwich_length = GetN(); if(sandwich_length < 10000) { if(MyNodeId() == 0) { KanSolutionSingle solution(sandwich_length); solution.solve(); } } else { if(MyNodeId() == 0) { KanSolutionMaster solution(sandwich_length); solution.solve(); } else { KanSolutionSlave solution(sandwich_length); solution.solve(); } } } |