#include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i = (a), i ## __ = (n); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i = (a), i ## __ = (n); i >= i ## __; --i) #define REP(i,n) FOR(i, 0, n - 1) #define REPD(i, n) FORD(i, n - 1, 0) #define ALL(x) x.begin(), x.end() #define SZ(x) (int(x.size())) #define EB emplace_back #define V vector #define ST first #define ND second #define RS resize template<class T, class B> inline void mini(T &&a, B &&b) { if (b < a) a = b; } template<class T, class B> inline void maxi(T &&a, B &&b) { if (b > a) a = b; } int ceil2(int x) { return x < 2 ? 1 : 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } constexpr char nl = '\n'; template<class T> struct DumpOff { T a, b; }; template<class T> auto dumpOff(T &x) -> DumpOff<decltype(x.begin())> { return {x.begin(), x.end()}; } template<class T> auto dumpOff(T &x) -> decltype(cerr << x, x) { return x; } template<class T> auto dumpOff(T &x) -> decltype(x.first, x) { return x; } struct Debug { ~Debug() { cerr << nl; } template<class T> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << dumpOff(x); return *this; } template<class T> auto operator<<(T x) -> decltype(x.begin(), *this) { cerr << "{\n"; for (auto a = x.begin(), b = x.end(); a != b; ++a) *this << " " << distance(x.begin(), a) << ": " << dumpOff(*a) << '\n'; return *this << "}"; } template<class T, class B> Debug& operator<<(pair<T, B> p) { return *this << "(" << dumpOff(p.first) << ", " << dumpOff(p.second) << ")"; } template<class T> Debug& operator<<(DumpOff<T> d) { cerr << "{"; for (auto a = d.a, b = d.b; a != b; ++a) *this << dumpOff(*a) << (next(a) == b ? "" : ", "); return *this << "}"; } }; struct Foo {template<class T>Foo& operator<<(T) {return *this;}}; #ifdef DEBUG # define D Debug() #else # define D Foo() # include "message.h" # include "kanapka.h" #endif #define I(x...) #x ": " << x << " " typedef long long LL; typedef pair<int, int> PII; typedef V<int> VI; typedef V<VI> VVI; typedef V<PII> VPII; typedef V<VPII> VVPII; typedef V<bool> VB; typedef V<VB> VVB; typedef V<LL> VLL; int main() { LL n = GetN(); int NON = NumberOfNodes(); LL ID = MyNodeId(); if(LL(NON) > n) NON = n; if(ID >= NON) return 0; int k = n / (NON); int r = n - (NON) * k; int a = k * (ID); int b = k * (ID + 1) - 1; if(ID < r) a += ID, b += ID + 1; else a += r, b += r; VLL front(b - a + 1); VLL back(b - a + 1); LL sfr = 0, sba = 0; int q = 0; FOR(i, a, b) { sfr += GetTaste(i); front[q] = sfr; if(q != 0) maxi(front[q], front[q - 1]); q++; } q = b - a; FORD(i, b, a) { sba += GetTaste(i); back[q] = sba; if(q != b - a) maxi(back[q], back[q + 1]); q--; } LL pref = 0, suff = 0; LL mxfr = 0, mxba = 0; if(ID != 0) { Receive(ID - 1); pref = GetLL(ID - 1); mxfr = GetLL(ID - 1); } if(ID != NON - 1) { PutLL(ID + 1, sfr + pref); PutLL(ID + 1, max(mxfr, front[q - 1] + pref)); Send(ID + 1); Receive(ID + 1); suff = GetLL(ID + 1); mxba = GetLL(ID + 1); } if(ID != 0) { PutLL(ID - 1, sba + suff); PutLL(ID - 1, max(mxba, back[0] + suff)); Send(ID - 1); } LL mx = 0; maxi(mx, max(mxfr, pref) + max(mxba, suff + back[0])); REP(i, b - a) maxi(mx, max(mxfr, pref + front[i]) + max(mxba, suff + back[i + 1])); maxi(mx, max(mxfr, pref + front[b - a]) + max(mxba, suff)); if(ID != 0) PutLL(0, mx), Send(0); else { REP(i, NON - 1) Receive(i + 1), maxi(mx, GetLL(i + 1)); cout << mx << "\n"; } 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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i = (a), i ## __ = (n); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i = (a), i ## __ = (n); i >= i ## __; --i) #define REP(i,n) FOR(i, 0, n - 1) #define REPD(i, n) FORD(i, n - 1, 0) #define ALL(x) x.begin(), x.end() #define SZ(x) (int(x.size())) #define EB emplace_back #define V vector #define ST first #define ND second #define RS resize template<class T, class B> inline void mini(T &&a, B &&b) { if (b < a) a = b; } template<class T, class B> inline void maxi(T &&a, B &&b) { if (b > a) a = b; } int ceil2(int x) { return x < 2 ? 1 : 1 << (sizeof(x) * 8 - __builtin_clz(x - 1)); } constexpr char nl = '\n'; template<class T> struct DumpOff { T a, b; }; template<class T> auto dumpOff(T &x) -> DumpOff<decltype(x.begin())> { return {x.begin(), x.end()}; } template<class T> auto dumpOff(T &x) -> decltype(cerr << x, x) { return x; } template<class T> auto dumpOff(T &x) -> decltype(x.first, x) { return x; } struct Debug { ~Debug() { cerr << nl; } template<class T> auto operator<<(T x) -> decltype(cerr << x, *this) { cerr << dumpOff(x); return *this; } template<class T> auto operator<<(T x) -> decltype(x.begin(), *this) { cerr << "{\n"; for (auto a = x.begin(), b = x.end(); a != b; ++a) *this << " " << distance(x.begin(), a) << ": " << dumpOff(*a) << '\n'; return *this << "}"; } template<class T, class B> Debug& operator<<(pair<T, B> p) { return *this << "(" << dumpOff(p.first) << ", " << dumpOff(p.second) << ")"; } template<class T> Debug& operator<<(DumpOff<T> d) { cerr << "{"; for (auto a = d.a, b = d.b; a != b; ++a) *this << dumpOff(*a) << (next(a) == b ? "" : ", "); return *this << "}"; } }; struct Foo {template<class T>Foo& operator<<(T) {return *this;}}; #ifdef DEBUG # define D Debug() #else # define D Foo() # include "message.h" # include "kanapka.h" #endif #define I(x...) #x ": " << x << " " typedef long long LL; typedef pair<int, int> PII; typedef V<int> VI; typedef V<VI> VVI; typedef V<PII> VPII; typedef V<VPII> VVPII; typedef V<bool> VB; typedef V<VB> VVB; typedef V<LL> VLL; int main() { LL n = GetN(); int NON = NumberOfNodes(); LL ID = MyNodeId(); if(LL(NON) > n) NON = n; if(ID >= NON) return 0; int k = n / (NON); int r = n - (NON) * k; int a = k * (ID); int b = k * (ID + 1) - 1; if(ID < r) a += ID, b += ID + 1; else a += r, b += r; VLL front(b - a + 1); VLL back(b - a + 1); LL sfr = 0, sba = 0; int q = 0; FOR(i, a, b) { sfr += GetTaste(i); front[q] = sfr; if(q != 0) maxi(front[q], front[q - 1]); q++; } q = b - a; FORD(i, b, a) { sba += GetTaste(i); back[q] = sba; if(q != b - a) maxi(back[q], back[q + 1]); q--; } LL pref = 0, suff = 0; LL mxfr = 0, mxba = 0; if(ID != 0) { Receive(ID - 1); pref = GetLL(ID - 1); mxfr = GetLL(ID - 1); } if(ID != NON - 1) { PutLL(ID + 1, sfr + pref); PutLL(ID + 1, max(mxfr, front[q - 1] + pref)); Send(ID + 1); Receive(ID + 1); suff = GetLL(ID + 1); mxba = GetLL(ID + 1); } if(ID != 0) { PutLL(ID - 1, sba + suff); PutLL(ID - 1, max(mxba, back[0] + suff)); Send(ID - 1); } LL mx = 0; maxi(mx, max(mxfr, pref) + max(mxba, suff + back[0])); REP(i, b - a) maxi(mx, max(mxfr, pref + front[i]) + max(mxba, suff + back[i + 1])); maxi(mx, max(mxfr, pref + front[b - a]) + max(mxba, suff)); if(ID != 0) PutLL(0, mx), Send(0); else { REP(i, NON - 1) Receive(i + 1), maxi(mx, GetLL(i + 1)); cout << mx << "\n"; } return 0; } |