#include "maklib.h" #include "message.h" #include <algorithm> #include <cstdio> #include <vector> #define LL long long #define INF 1000000000 using namespace std; int from, to, all, me, n; vector<LL> pref; vector<LL> DP; LL best, maxiPref, maxiSuf, sum; void solve() { int siz = to - from + 1; DP.resize(siz); pref.resize(siz); best = -INF; maxiPref = -INF; maxiSuf = -INF; sum = 0; for (int i = 0; i < siz; i++) { pref[i] = 0; if (i != 0) { pref[i] = pref[i - 1]; } LL val = ElementAt(from + i + 1); pref[i] += val; DP[i] = val; if (i != 0 && DP[i - 1] > 0) { DP[i] += DP[i - 1]; } maxiPref = max(maxiPref, pref[i]); best = max(best, DP[i]); } for (int i = 0; i < siz; i++) { LL suf = pref[siz - 1]; if (i != 0) { suf -= pref[i - 1]; } maxiSuf = max(maxiSuf, suf); } sum = pref[siz - 1]; if (me != 0) { PutLL(0, best); PutLL(0, maxiPref); PutLL(0, maxiSuf); PutLL(0, sum); Send(0); } } void combine() { vector<LL> bests = {best}; vector<LL> prefs = {maxiPref}; vector<LL> sufs = {maxiSuf}; vector<LL> sums = {sum}; for (int cli = 1; cli < all; cli++) { Receive(cli); bests.push_back(GetLL(cli)); prefs.push_back(GetLL(cli)); sufs.push_back(GetLL(cli)); sums.push_back(GetLL(cli)); } LL ans = 0; ans = max(ans, best); for (int i = 0; i < all; i++) { ans = max(ans, bests[i]); } for (int a = 0; a < all; a++) { LL now = sufs[a]; for (int b = a + 1; b < all; b++) { ans = max(ans, now + prefs[b]); now += sums[b]; } } printf("%lld\n", ans); } int main() { all = NumberOfNodes(); me = MyNodeId(); n = Size(); if (me >= n) return 0; all = min(all, n); int siz = n / all; from = siz * me; to = siz * (me + 1) - 1; if (me == all - 1) { to = n - 1; } solve(); if (me == 0) { combine(); } 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 | #include "maklib.h" #include "message.h" #include <algorithm> #include <cstdio> #include <vector> #define LL long long #define INF 1000000000 using namespace std; int from, to, all, me, n; vector<LL> pref; vector<LL> DP; LL best, maxiPref, maxiSuf, sum; void solve() { int siz = to - from + 1; DP.resize(siz); pref.resize(siz); best = -INF; maxiPref = -INF; maxiSuf = -INF; sum = 0; for (int i = 0; i < siz; i++) { pref[i] = 0; if (i != 0) { pref[i] = pref[i - 1]; } LL val = ElementAt(from + i + 1); pref[i] += val; DP[i] = val; if (i != 0 && DP[i - 1] > 0) { DP[i] += DP[i - 1]; } maxiPref = max(maxiPref, pref[i]); best = max(best, DP[i]); } for (int i = 0; i < siz; i++) { LL suf = pref[siz - 1]; if (i != 0) { suf -= pref[i - 1]; } maxiSuf = max(maxiSuf, suf); } sum = pref[siz - 1]; if (me != 0) { PutLL(0, best); PutLL(0, maxiPref); PutLL(0, maxiSuf); PutLL(0, sum); Send(0); } } void combine() { vector<LL> bests = {best}; vector<LL> prefs = {maxiPref}; vector<LL> sufs = {maxiSuf}; vector<LL> sums = {sum}; for (int cli = 1; cli < all; cli++) { Receive(cli); bests.push_back(GetLL(cli)); prefs.push_back(GetLL(cli)); sufs.push_back(GetLL(cli)); sums.push_back(GetLL(cli)); } LL ans = 0; ans = max(ans, best); for (int i = 0; i < all; i++) { ans = max(ans, bests[i]); } for (int a = 0; a < all; a++) { LL now = sufs[a]; for (int b = a + 1; b < all; b++) { ans = max(ans, now + prefs[b]); now += sums[b]; } } printf("%lld\n", ans); } int main() { all = NumberOfNodes(); me = MyNodeId(); n = Size(); if (me >= n) return 0; all = min(all, n); int siz = n / all; from = siz * me; to = siz * (me + 1) - 1; if (me == all - 1) { to = n - 1; } solve(); if (me == 0) { combine(); } return 0; } |