#include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> #include "kanapka.h" #include "message.h" using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; using ll = long long; const ll INF = 1e9+10; const int MASTER = 0; struct BlockResult { ll sum; ll min_prefix; ll min_suffix; ll min_sum; }; int main() { ios_base::sync_with_stdio(0); const ll id = MyNodeId(); const ll n = GetN(); const ll k = min(n, (ll)NumberOfNodes()); //don't need such instances if (id >= k) return 0; const ll block_len = ceil(n / (double)k); const ll range_begin = id*block_len; const ll range_end = min(n-1, (id+1)*block_len-1); const ll range_len = range_end-range_begin+1; vector<ll> a(range_len); /* reading values in my range */ for (int i = range_begin; i <= range_end; ++i) { a[i-range_begin] = GetTaste(i); } // computing local result // computing sum ll sum = 0; for (auto v : a) { sum += v; } // computing min_prefix ll min_prefix = 0; { ll cum_sum = 0; for (auto v : a) { cum_sum += v; min_prefix = min(min_prefix, cum_sum); } } // computing min_suffix ll min_suffix = 0; { ll cum_sum = 0; for (auto it = a.rbegin(); it != a.rend(); ++it) { cum_sum += *it; min_suffix = min(min_suffix, cum_sum); } } // computing min sum subsegment ll min_sum = 0; { ll min_ending_here = 0; for (auto v : a) { min_ending_here = min(min_ending_here + v, v); min_sum = min(min_sum, min_ending_here); } } // joining results PutLL(MASTER, sum); PutLL(MASTER, min_prefix); PutLL(MASTER, min_suffix); PutLL(MASTER, min_sum); Send(MASTER); if (id == MASTER) { vector<BlockResult> a(k); for (int sender_id = 0; sender_id < k; ++sender_id) { Receive(sender_id); BlockResult res; res.sum = GetLL(sender_id); res.min_prefix = GetLL(sender_id); res.min_suffix = GetLL(sender_id); res.min_sum = GetLL(sender_id); a[sender_id] = res; } ll total = 0; for (auto b : a) { total += b.sum; } ll min_sum = 0; // case 1: min_sum is inside a single block for (auto b : a) { min_sum = min(min_sum, b.min_sum); } // case 2: min_sum is spanned across multiple blocks /* for (int i = 0; i < k; ++i) { ll sum_between = 0; for (int j = i+1; j < k; ++j) { ll min_here = a[i].min_suffix + sum_between + a[j].min_prefix; min_sum = min(min_sum, min_here); sum_between += a[j].sum; } } */ for (int i = 0; i < k; ++i) { for (int j = i+1; j < k; ++j) { ll min_here = a[i].min_suffix; for (int x = i+1; x < j; ++x) { min_here += a[x].sum; } min_here += a[j].min_prefix; min_sum = min(min_sum, min_here); } } cout << total - min_sum << 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 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 | #include <iostream> #include <cstdio> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <algorithm> #include <cstring> #include <ctime> #include <cassert> #include "kanapka.h" #include "message.h" using namespace std; #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define vi vector<int> #define vpii vector<pii> #define SZ(x) ((int)(x.size())) #define DBG cerr << "debug here" << endl; #define DBGV(vari) cerr << #vari<< " = "<< (vari) <<endl; using ll = long long; const ll INF = 1e9+10; const int MASTER = 0; struct BlockResult { ll sum; ll min_prefix; ll min_suffix; ll min_sum; }; int main() { ios_base::sync_with_stdio(0); const ll id = MyNodeId(); const ll n = GetN(); const ll k = min(n, (ll)NumberOfNodes()); //don't need such instances if (id >= k) return 0; const ll block_len = ceil(n / (double)k); const ll range_begin = id*block_len; const ll range_end = min(n-1, (id+1)*block_len-1); const ll range_len = range_end-range_begin+1; vector<ll> a(range_len); /* reading values in my range */ for (int i = range_begin; i <= range_end; ++i) { a[i-range_begin] = GetTaste(i); } // computing local result // computing sum ll sum = 0; for (auto v : a) { sum += v; } // computing min_prefix ll min_prefix = 0; { ll cum_sum = 0; for (auto v : a) { cum_sum += v; min_prefix = min(min_prefix, cum_sum); } } // computing min_suffix ll min_suffix = 0; { ll cum_sum = 0; for (auto it = a.rbegin(); it != a.rend(); ++it) { cum_sum += *it; min_suffix = min(min_suffix, cum_sum); } } // computing min sum subsegment ll min_sum = 0; { ll min_ending_here = 0; for (auto v : a) { min_ending_here = min(min_ending_here + v, v); min_sum = min(min_sum, min_ending_here); } } // joining results PutLL(MASTER, sum); PutLL(MASTER, min_prefix); PutLL(MASTER, min_suffix); PutLL(MASTER, min_sum); Send(MASTER); if (id == MASTER) { vector<BlockResult> a(k); for (int sender_id = 0; sender_id < k; ++sender_id) { Receive(sender_id); BlockResult res; res.sum = GetLL(sender_id); res.min_prefix = GetLL(sender_id); res.min_suffix = GetLL(sender_id); res.min_sum = GetLL(sender_id); a[sender_id] = res; } ll total = 0; for (auto b : a) { total += b.sum; } ll min_sum = 0; // case 1: min_sum is inside a single block for (auto b : a) { min_sum = min(min_sum, b.min_sum); } // case 2: min_sum is spanned across multiple blocks /* for (int i = 0; i < k; ++i) { ll sum_between = 0; for (int j = i+1; j < k; ++j) { ll min_here = a[i].min_suffix + sum_between + a[j].min_prefix; min_sum = min(min_sum, min_here); sum_between += a[j].sum; } } */ for (int i = 0; i < k; ++i) { for (int j = i+1; j < k; ++j) { ll min_here = a[i].min_suffix; for (int x = i+1; x < j; ++x) { min_here += a[x].sum; } min_here += a[j].min_prefix; min_sum = min(min_sum, min_here); } } cout << total - min_sum << endl; } return 0; } |