#include <algorithm> #include <iostream> #include <tuple> #include "maklib.h" #include "message.h" using namespace std; typedef long long int64; int element_at_getter (int i) { return ElementAt(i); } static int64 * tab; int64 array_getter (int i) { return tab[i]; } template <typename T_Elements> tuple<int64, int64, int64, int64, int64> compute_max_subarray (T_Elements (*element_getter) (int), int begin, int end) { int64 max_sum = 0, sum = 0, sum_from_begin = 0, sum_from_end = 0, sum_total = 0, sum_current, max_left = 0, max_right = 0; int b = -1, flag = 0; --end; for (int i = begin; i < end; ++i) { T_Elements x = element_getter(i); sum_total += x; max_left = max(max_left, sum_total); sum = max(sum + x, 0LL); if (sum > 0) { if (b == -1) { b = i; sum_current = sum_total - x; flag = 1; } if (sum > max_sum) { max_sum = sum; if (flag) { sum_from_begin = sum_current; flag = 0; } } } else { b = -1; } } // last iteration int i = end; if (i > 0) { T_Elements x = element_getter(i); sum_total += x; max_left = max(max_left, sum_total); sum = max(sum + x, 0LL); if (sum > 0) { if (b == -1) { b = i; sum_current = sum_total - x; flag = 1; } if (sum > max_sum) { max_sum = sum; if (flag) { sum_from_begin = sum_current; flag = 0; } } max_right = sum; } else { max_right = 0; b = -1; } } sum_from_end = (sum_total - sum_from_begin - max_sum); return make_tuple(max_sum, sum_from_begin, sum_from_end, max_left, max_right); } int main () { int size = Size(); int nodes = NumberOfNodes(); int me = MyNodeId(); int start = (size / nodes) * me; int mod = (size % nodes); int end = (size / nodes) * (me + 1); if (me == nodes - 1) { end += mod; } tuple<int64, int64, int64, int64, int64> res = compute_max_subarray<int>(element_at_getter, start + 1, end + 1); if (me != 0) { PutLL(0, get<0>(res)); PutLL(0, get<1>(res)); PutLL(0, get<2>(res)); PutLL(0, get<3>(res)); PutLL(0, get<4>(res)); Send(0); } else { tab = new int64 [nodes * 3]; int64 leftright[nodes * 2]; for (int i = 0; i < nodes - 1; i++) { int source = Receive(-1); int s_node = source; int64 s_sum = GetLL(source); int64 s_sfb = GetLL(source); int64 s_sfe = GetLL(source); int64 s_ml = GetLL(source); int64 s_mr = GetLL(source); tab[s_node * 3] = s_sfb; tab[s_node * 3 + 1] = s_sum; tab[s_node * 3 + 2] = s_sfe; leftright[s_node * 2] = s_ml; leftright[s_node * 2 + 1] = s_mr; } tab[0] = get<1>(res); tab[1] = get<0>(res); tab[2] = get<2>(res); leftright[0] = get<3>(res); leftright[1] = get<4>(res); int64 max_final = 0; for (int i = 0; i < nodes; i++) { for (int j = i; j < nodes; j++) { if (i == j) { max_final = max(max_final, tab[i * 3 + 1]); } else { int64 temp_r = tab[i * 3 + 2]; int64 temp_s = tab[i * 3 + 1]; int64 subs = max(temp_s + temp_r, leftright[i * 2 + 1]); tab[i * 3 + 2] = subs; tab[i * 3 + 1] = 0; int64 temp_r_j = tab[j * 3]; int64 temp_s_j = tab[j * 3 + 1]; int64 subs_j = max(temp_s_j + temp_r_j, leftright[j * 2]); tab[j * 3 ] = subs_j; tab[j * 3 + 1] = 0; int64 s = 0; max_final = max(max_final, get<0>(compute_max_subarray<int64>(array_getter, i * 3, (j * 3 + 3)))); tab[j * 3 ] = temp_r_j; tab[j * 3 + 1] = temp_s_j; tab[i * 3 + 2] = temp_r; tab[i * 3 + 1] = temp_s; } } } delete tab; cout << max_final << endl; } }
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #include <algorithm> #include <iostream> #include <tuple> #include "maklib.h" #include "message.h" using namespace std; typedef long long int64; int element_at_getter (int i) { return ElementAt(i); } static int64 * tab; int64 array_getter (int i) { return tab[i]; } template <typename T_Elements> tuple<int64, int64, int64, int64, int64> compute_max_subarray (T_Elements (*element_getter) (int), int begin, int end) { int64 max_sum = 0, sum = 0, sum_from_begin = 0, sum_from_end = 0, sum_total = 0, sum_current, max_left = 0, max_right = 0; int b = -1, flag = 0; --end; for (int i = begin; i < end; ++i) { T_Elements x = element_getter(i); sum_total += x; max_left = max(max_left, sum_total); sum = max(sum + x, 0LL); if (sum > 0) { if (b == -1) { b = i; sum_current = sum_total - x; flag = 1; } if (sum > max_sum) { max_sum = sum; if (flag) { sum_from_begin = sum_current; flag = 0; } } } else { b = -1; } } // last iteration int i = end; if (i > 0) { T_Elements x = element_getter(i); sum_total += x; max_left = max(max_left, sum_total); sum = max(sum + x, 0LL); if (sum > 0) { if (b == -1) { b = i; sum_current = sum_total - x; flag = 1; } if (sum > max_sum) { max_sum = sum; if (flag) { sum_from_begin = sum_current; flag = 0; } } max_right = sum; } else { max_right = 0; b = -1; } } sum_from_end = (sum_total - sum_from_begin - max_sum); return make_tuple(max_sum, sum_from_begin, sum_from_end, max_left, max_right); } int main () { int size = Size(); int nodes = NumberOfNodes(); int me = MyNodeId(); int start = (size / nodes) * me; int mod = (size % nodes); int end = (size / nodes) * (me + 1); if (me == nodes - 1) { end += mod; } tuple<int64, int64, int64, int64, int64> res = compute_max_subarray<int>(element_at_getter, start + 1, end + 1); if (me != 0) { PutLL(0, get<0>(res)); PutLL(0, get<1>(res)); PutLL(0, get<2>(res)); PutLL(0, get<3>(res)); PutLL(0, get<4>(res)); Send(0); } else { tab = new int64 [nodes * 3]; int64 leftright[nodes * 2]; for (int i = 0; i < nodes - 1; i++) { int source = Receive(-1); int s_node = source; int64 s_sum = GetLL(source); int64 s_sfb = GetLL(source); int64 s_sfe = GetLL(source); int64 s_ml = GetLL(source); int64 s_mr = GetLL(source); tab[s_node * 3] = s_sfb; tab[s_node * 3 + 1] = s_sum; tab[s_node * 3 + 2] = s_sfe; leftright[s_node * 2] = s_ml; leftright[s_node * 2 + 1] = s_mr; } tab[0] = get<1>(res); tab[1] = get<0>(res); tab[2] = get<2>(res); leftright[0] = get<3>(res); leftright[1] = get<4>(res); int64 max_final = 0; for (int i = 0; i < nodes; i++) { for (int j = i; j < nodes; j++) { if (i == j) { max_final = max(max_final, tab[i * 3 + 1]); } else { int64 temp_r = tab[i * 3 + 2]; int64 temp_s = tab[i * 3 + 1]; int64 subs = max(temp_s + temp_r, leftright[i * 2 + 1]); tab[i * 3 + 2] = subs; tab[i * 3 + 1] = 0; int64 temp_r_j = tab[j * 3]; int64 temp_s_j = tab[j * 3 + 1]; int64 subs_j = max(temp_s_j + temp_r_j, leftright[j * 2]); tab[j * 3 ] = subs_j; tab[j * 3 + 1] = 0; int64 s = 0; max_final = max(max_final, get<0>(compute_max_subarray<int64>(array_getter, i * 3, (j * 3 + 3)))); tab[j * 3 ] = temp_r_j; tab[j * 3 + 1] = temp_s_j; tab[i * 3 + 2] = temp_r; tab[i * 3 + 1] = temp_s; } } } delete tab; cout << max_final << endl; } } |