#include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #include "message.h" #include "maklib.h" #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; int ProcNum, ProcID; int N; int main() { ProcNum = NumberOfNodes(); ProcID = MyNodeId(); N = Size(); int begIdx = int(((LL(ProcID)) * LL(N)) / ProcNum); int endIdx = int((LL(ProcID + 1) * LL(N)) / ProcNum); LL maxPref = 0, maxSuf = 0, sum = 0, bestSum = 0; LL w = 0; FOR (i, begIdx, endIdx) { int x = ElementAt(i+1); sum += x; maxPref = max(maxPref, sum); maxSuf = max(maxSuf, -sum); if (w > 0) { w += x; } else { w = x; } bestSum = max(bestSum, w); } maxSuf += sum; //cerr << ProcID << ": " << sum << " " << bestSum << " " << maxPref << " " << maxSuf << "\n"; if (ProcID != 0) { PutLL(0, sum); PutLL(0, maxPref); PutLL(0, maxSuf); PutLL(0, bestSum); Send(0); } else { LL res = bestSum; vector<LL> sums(ProcNum), maxPrefs(ProcNum), maxSufs(ProcNum); sums[0] = sum; maxPrefs[0] = maxPref; maxSufs[0] = maxSuf; REP (i, ProcNum-1) { int senderID = Receive(-1); sums[senderID] = GetLL(senderID); maxPrefs[senderID] = GetLL(senderID); maxSufs[senderID] = GetLL(senderID); res = max(res, GetLL(senderID)); } FOR (i, 0, ProcNum) { FOR (j, i+1, ProcNum) { LL tmp = maxSufs[i] + maxPrefs[j]; FOR (k, i+1, j) { tmp += sums[k]; } res = max(res, tmp); } } printf("%lld\n", res); } 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 | #include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #include "message.h" #include "maklib.h" #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; int ProcNum, ProcID; int N; int main() { ProcNum = NumberOfNodes(); ProcID = MyNodeId(); N = Size(); int begIdx = int(((LL(ProcID)) * LL(N)) / ProcNum); int endIdx = int((LL(ProcID + 1) * LL(N)) / ProcNum); LL maxPref = 0, maxSuf = 0, sum = 0, bestSum = 0; LL w = 0; FOR (i, begIdx, endIdx) { int x = ElementAt(i+1); sum += x; maxPref = max(maxPref, sum); maxSuf = max(maxSuf, -sum); if (w > 0) { w += x; } else { w = x; } bestSum = max(bestSum, w); } maxSuf += sum; //cerr << ProcID << ": " << sum << " " << bestSum << " " << maxPref << " " << maxSuf << "\n"; if (ProcID != 0) { PutLL(0, sum); PutLL(0, maxPref); PutLL(0, maxSuf); PutLL(0, bestSum); Send(0); } else { LL res = bestSum; vector<LL> sums(ProcNum), maxPrefs(ProcNum), maxSufs(ProcNum); sums[0] = sum; maxPrefs[0] = maxPref; maxSufs[0] = maxSuf; REP (i, ProcNum-1) { int senderID = Receive(-1); sums[senderID] = GetLL(senderID); maxPrefs[senderID] = GetLL(senderID); maxSufs[senderID] = GetLL(senderID); res = max(res, GetLL(senderID)); } FOR (i, 0, ProcNum) { FOR (j, i+1, ProcNum) { LL tmp = maxSufs[i] + maxPrefs[j]; FOR (k, i+1, j) { tmp += sums[k]; } res = max(res, tmp); } } printf("%lld\n", res); } return 0; } |