#include <bits/stdc++.h> #define LL long long int #define REP(i, n) for(int i = 0; i < (n); ++i) #define FOR(i, f, n) for(int i = (f); i <= (n); ++i) #include "message.h" #include "kanapka.h" using namespace std; int id, m, n; void runCommonInstance() { int* tastes; LL maxPref = 0, maxSuf = 0, maxPrefSuf = 0, totalSum = 0; id = MyNodeId(); m = NumberOfNodes(); n = GetN(); int d = n/m+(id < n%m); int beg = id*(n/m)+min(id, n%m); int en = beg+d-1; if(d == 0) { PutLL(0, 0); PutLL(0, 0); PutLL(0, 0); PutLL(0, 0); Send(0); return; } //cerr << "Jestem maszyna nr " + to_string(id) + " i biore pola od " + to_string(beg) + " do " + to_string(en) + "!\n"; tastes = new int[d]; REP(i, d) tastes[i] = GetTaste(i+beg); LL currPref = 0; LL currSuf = 0; maxPref = max(0, tastes[0]); currPref = tastes[0]; for(int i = 1; i < d; ++i) { currPref += tastes[i]; maxPref = max(currPref, maxPref); } maxSuf = max(0, tastes[d-1]); currSuf = tastes[d-1]; for(int i = d-2; i >= 0; --i) { currSuf += tastes[i]; maxSuf = max(currSuf, maxSuf); } for(int i = 0; i < d; ++i) totalSum += tastes[i]; maxPrefSuf = max(maxPrefSuf, totalSum); maxPrefSuf = max(maxPrefSuf, maxPref); maxPrefSuf = max(maxPrefSuf, maxSuf); LL maxSoFar = 0; LL maxEndingHere = 0; for(int i = 0; i < d; ++i) { maxEndingHere = max(0ll, maxEndingHere-tastes[i]); maxSoFar = max(maxSoFar, maxEndingHere); } maxPrefSuf = max(maxPrefSuf, totalSum+maxSoFar); /* cerr << "Jestem maszyna nr " + to_string(id) + " i zwracam: " + to_string(maxPref) + " --- " + to_string(maxSuf) + " --- " + to_string(maxPrefSuf) + " --- " + to_string(totalSum) + "\n"; */ PutLL(0, maxPref); PutLL(0, maxSuf); PutLL(0, maxPrefSuf); PutLL(0, totalSum); Send(0); delete [] tastes; } void runInstance0() { LL *maxPref, *maxSuf = 0, *maxPrefSuf = 0, *totalSum = 0; maxPref = new LL[105]; maxSuf = new LL[105]; maxPrefSuf = new LL[105]; totalSum = new LL[105]; REP(i, m) { int cm = Receive(-1); maxPref[cm] = GetLL(cm); maxSuf[cm] = GetLL(cm); maxPrefSuf[cm] = GetLL(cm); totalSum[cm] = GetLL(cm); } LL maxi = 0; for(int i = 0; i < m; ++i) { LL cr = 0; for(int j = 0; j < i; ++j) cr += totalSum[j]; for(int j = i+1; j < m; ++j) cr += totalSum[j]; cr += maxPrefSuf[i]; maxi = max(maxi, cr); cr = 0; for(int j = 0; j < i; ++j) cr += totalSum[j]; cr += maxPref[i]; for(int j = m-1; j > i; --j) { maxi = max(maxi, cr+maxSuf[j]); cr += totalSum[j]; } } printf("%lld\n", maxi); delete [] maxPref; delete [] maxSuf; delete [] maxPrefSuf; delete [] totalSum; } int main() { runCommonInstance(); if(id == 0) runInstance0(); 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 <bits/stdc++.h> #define LL long long int #define REP(i, n) for(int i = 0; i < (n); ++i) #define FOR(i, f, n) for(int i = (f); i <= (n); ++i) #include "message.h" #include "kanapka.h" using namespace std; int id, m, n; void runCommonInstance() { int* tastes; LL maxPref = 0, maxSuf = 0, maxPrefSuf = 0, totalSum = 0; id = MyNodeId(); m = NumberOfNodes(); n = GetN(); int d = n/m+(id < n%m); int beg = id*(n/m)+min(id, n%m); int en = beg+d-1; if(d == 0) { PutLL(0, 0); PutLL(0, 0); PutLL(0, 0); PutLL(0, 0); Send(0); return; } //cerr << "Jestem maszyna nr " + to_string(id) + " i biore pola od " + to_string(beg) + " do " + to_string(en) + "!\n"; tastes = new int[d]; REP(i, d) tastes[i] = GetTaste(i+beg); LL currPref = 0; LL currSuf = 0; maxPref = max(0, tastes[0]); currPref = tastes[0]; for(int i = 1; i < d; ++i) { currPref += tastes[i]; maxPref = max(currPref, maxPref); } maxSuf = max(0, tastes[d-1]); currSuf = tastes[d-1]; for(int i = d-2; i >= 0; --i) { currSuf += tastes[i]; maxSuf = max(currSuf, maxSuf); } for(int i = 0; i < d; ++i) totalSum += tastes[i]; maxPrefSuf = max(maxPrefSuf, totalSum); maxPrefSuf = max(maxPrefSuf, maxPref); maxPrefSuf = max(maxPrefSuf, maxSuf); LL maxSoFar = 0; LL maxEndingHere = 0; for(int i = 0; i < d; ++i) { maxEndingHere = max(0ll, maxEndingHere-tastes[i]); maxSoFar = max(maxSoFar, maxEndingHere); } maxPrefSuf = max(maxPrefSuf, totalSum+maxSoFar); /* cerr << "Jestem maszyna nr " + to_string(id) + " i zwracam: " + to_string(maxPref) + " --- " + to_string(maxSuf) + " --- " + to_string(maxPrefSuf) + " --- " + to_string(totalSum) + "\n"; */ PutLL(0, maxPref); PutLL(0, maxSuf); PutLL(0, maxPrefSuf); PutLL(0, totalSum); Send(0); delete [] tastes; } void runInstance0() { LL *maxPref, *maxSuf = 0, *maxPrefSuf = 0, *totalSum = 0; maxPref = new LL[105]; maxSuf = new LL[105]; maxPrefSuf = new LL[105]; totalSum = new LL[105]; REP(i, m) { int cm = Receive(-1); maxPref[cm] = GetLL(cm); maxSuf[cm] = GetLL(cm); maxPrefSuf[cm] = GetLL(cm); totalSum[cm] = GetLL(cm); } LL maxi = 0; for(int i = 0; i < m; ++i) { LL cr = 0; for(int j = 0; j < i; ++j) cr += totalSum[j]; for(int j = i+1; j < m; ++j) cr += totalSum[j]; cr += maxPrefSuf[i]; maxi = max(maxi, cr); cr = 0; for(int j = 0; j < i; ++j) cr += totalSum[j]; cr += maxPref[i]; for(int j = m-1; j > i; --j) { maxi = max(maxi, cr+maxSuf[j]); cr += totalSum[j]; } } printf("%lld\n", maxi); delete [] maxPref; delete [] maxSuf; delete [] maxPrefSuf; delete [] totalSum; } int main() { runCommonInstance(); if(id == 0) runInstance0(); return 0; } |