#include "kanapka.h" #include "message.h" #include <algorithm> #include <iostream> #include <thread> #include <chrono> using namespace std; //long long GetTaste(long long i); long long elem_from, elem_to; long long len; long long get(long long n) { return GetTaste(n+elem_from-1); } int main() { int nodes = NumberOfNodes(); int myNode = MyNodeId(); // std::this_thread::sleep_for(std::chrono::milliseconds{myNode*100}); auto N = GetN(); elem_from = N*myNode/nodes; elem_to = N*(myNode+1)/nodes; len = elem_to-elem_from; std::vector<long long> pref, suf; pref.resize(len+2, 0); suf.resize(len+2, 0); long long sum = 0; for(auto i = 1; i <= len; ++i) { sum += get(i); pref[i] = std::max(pref[i-1], sum); } sum = 0; for(auto i = len; i >= 1; --i) { sum += get(i); suf[i] = std::max(suf[i+1], sum); } long long max_both = 0; for(auto i = 0; i <= len; ++i) { max_both = std::max(max_both, pref[i] + suf[i+1]); } // printf("0\t"); // for(auto i = 1; i <= len; ++i) { // printf("%lld\t", get(i)); // } // printf("0\n"); // printf("\n"); // // for(auto i = 0; i <= len+1; ++i) { // printf("%lld\t", pref[i]); // } // printf("\n"); // for(auto i = 0; i <= len+1; ++i) { // printf("%lld\t", suf[i]); // } // printf("\n"); // printf("\n"); PutLL(0, sum); PutLL(0, pref[len]); PutLL(0, suf[1]); PutLL(0, max_both); Send(0); if (myNode == 0) { //std::this_thread::sleep_for(std::chrono::milliseconds{1000}); std::vector<long long> block_sums, block_prefix, block_suffix, block_max_both_sides; block_sums.push_back(0); block_prefix.push_back(0); block_suffix.push_back(0); block_max_both_sides.push_back(0); for (auto i = 1; i <= nodes; ++i) { Receive(i-1); block_sums.push_back(GetLL(i-1)); block_prefix.push_back(GetLL(i-1)); block_suffix.push_back(GetLL(i-1)); block_max_both_sides.push_back(GetLL(i-1)); // printf("[%d] Sum: %lld pref: %lld suf: %lld both: %lld\n", i, block_sums[i], block_prefix[i], block_suffix[i], block_max_both_sides[i]); } block_sums.push_back(0); block_prefix.push_back(0); block_suffix.push_back(0); block_max_both_sides.push_back(0); std::vector<long long> pref, suf, sum_prefix, sum_suffix; pref.resize(nodes+2, 0); suf.resize(nodes+2, 0); sum_prefix.resize(nodes+2, 0); sum_suffix.resize(nodes+2, 0); for (auto i = 1; i <= nodes; ++i) { pref[i] = std::max(pref[i-1], sum_prefix[i-1] + block_prefix[i]); sum_prefix[i] = sum_prefix[i-1] + block_sums[i]; } for (auto i = nodes; i >= 1; --i) { suf[i] = std::max(suf[i+1], sum_suffix[i+1] + block_suffix[i]); sum_suffix[i] = sum_suffix[i+1] + block_sums[i]; } // printf("PREF:\n"); // for (auto i = 0; i <= nodes+1; ++i) { // printf("%lld\t", pref[i]); // } // printf("\n\nSUF:\n"); // for (auto i = 0; i <= nodes+1; ++i) { // printf("%lld\t", suf[i]); // } // printf("\n"); // printf("FINISH_AT:\n"); long long result = 0; for (auto i = 0; i <= nodes; ++i) { auto finish_from_left_to_i = pref[i] + suf[i+1]; result = std::max(result, finish_from_left_to_i); // printf("%lld\t", finish_from_left_to_i); } // printf("\n\n"); // printf("BOTH AT:\n"); for (auto i = 1; i <= nodes; ++i) { auto finish_both_at_i = sum_prefix[i-1] + block_max_both_sides[i] + sum_suffix[i+1]; result = std::max(result, finish_both_at_i); // printf("%lld\t", finish_both_at_i); } // printf("\n\n"); printf("%lld\n", result); // max_prefix = 0; // for (auto i = 0; i < nodes-1; ++i) { // max_prefix = std::max(max_prefix, block_sums[i]+block_prefix[i+1]); // } // // max_suffix = 0; // for (auto i = nodes-1; i > 0; --i) { // max_suffix = std::max(max_suffix, block_sums[i]+block_suffix[i-1]); // } // // printf("%lld\n", max_prefix+max_suffix); } 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 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 | #include "kanapka.h" #include "message.h" #include <algorithm> #include <iostream> #include <thread> #include <chrono> using namespace std; //long long GetTaste(long long i); long long elem_from, elem_to; long long len; long long get(long long n) { return GetTaste(n+elem_from-1); } int main() { int nodes = NumberOfNodes(); int myNode = MyNodeId(); // std::this_thread::sleep_for(std::chrono::milliseconds{myNode*100}); auto N = GetN(); elem_from = N*myNode/nodes; elem_to = N*(myNode+1)/nodes; len = elem_to-elem_from; std::vector<long long> pref, suf; pref.resize(len+2, 0); suf.resize(len+2, 0); long long sum = 0; for(auto i = 1; i <= len; ++i) { sum += get(i); pref[i] = std::max(pref[i-1], sum); } sum = 0; for(auto i = len; i >= 1; --i) { sum += get(i); suf[i] = std::max(suf[i+1], sum); } long long max_both = 0; for(auto i = 0; i <= len; ++i) { max_both = std::max(max_both, pref[i] + suf[i+1]); } // printf("0\t"); // for(auto i = 1; i <= len; ++i) { // printf("%lld\t", get(i)); // } // printf("0\n"); // printf("\n"); // // for(auto i = 0; i <= len+1; ++i) { // printf("%lld\t", pref[i]); // } // printf("\n"); // for(auto i = 0; i <= len+1; ++i) { // printf("%lld\t", suf[i]); // } // printf("\n"); // printf("\n"); PutLL(0, sum); PutLL(0, pref[len]); PutLL(0, suf[1]); PutLL(0, max_both); Send(0); if (myNode == 0) { //std::this_thread::sleep_for(std::chrono::milliseconds{1000}); std::vector<long long> block_sums, block_prefix, block_suffix, block_max_both_sides; block_sums.push_back(0); block_prefix.push_back(0); block_suffix.push_back(0); block_max_both_sides.push_back(0); for (auto i = 1; i <= nodes; ++i) { Receive(i-1); block_sums.push_back(GetLL(i-1)); block_prefix.push_back(GetLL(i-1)); block_suffix.push_back(GetLL(i-1)); block_max_both_sides.push_back(GetLL(i-1)); // printf("[%d] Sum: %lld pref: %lld suf: %lld both: %lld\n", i, block_sums[i], block_prefix[i], block_suffix[i], block_max_both_sides[i]); } block_sums.push_back(0); block_prefix.push_back(0); block_suffix.push_back(0); block_max_both_sides.push_back(0); std::vector<long long> pref, suf, sum_prefix, sum_suffix; pref.resize(nodes+2, 0); suf.resize(nodes+2, 0); sum_prefix.resize(nodes+2, 0); sum_suffix.resize(nodes+2, 0); for (auto i = 1; i <= nodes; ++i) { pref[i] = std::max(pref[i-1], sum_prefix[i-1] + block_prefix[i]); sum_prefix[i] = sum_prefix[i-1] + block_sums[i]; } for (auto i = nodes; i >= 1; --i) { suf[i] = std::max(suf[i+1], sum_suffix[i+1] + block_suffix[i]); sum_suffix[i] = sum_suffix[i+1] + block_sums[i]; } // printf("PREF:\n"); // for (auto i = 0; i <= nodes+1; ++i) { // printf("%lld\t", pref[i]); // } // printf("\n\nSUF:\n"); // for (auto i = 0; i <= nodes+1; ++i) { // printf("%lld\t", suf[i]); // } // printf("\n"); // printf("FINISH_AT:\n"); long long result = 0; for (auto i = 0; i <= nodes; ++i) { auto finish_from_left_to_i = pref[i] + suf[i+1]; result = std::max(result, finish_from_left_to_i); // printf("%lld\t", finish_from_left_to_i); } // printf("\n\n"); // printf("BOTH AT:\n"); for (auto i = 1; i <= nodes; ++i) { auto finish_both_at_i = sum_prefix[i-1] + block_max_both_sides[i] + sum_suffix[i+1]; result = std::max(result, finish_both_at_i); // printf("%lld\t", finish_both_at_i); } // printf("\n\n"); printf("%lld\n", result); // max_prefix = 0; // for (auto i = 0; i < nodes-1; ++i) { // max_prefix = std::max(max_prefix, block_sums[i]+block_prefix[i+1]); // } // // max_suffix = 0; // for (auto i = nodes-1; i > 0; --i) { // max_suffix = std::max(max_suffix, block_sums[i]+block_suffix[i-1]); // } // // printf("%lld\n", max_prefix+max_suffix); } return 0; } |