#include <algorithm> #include <string> #include <vector> #include <list> #include <cctype> #include <cmath> #include <iostream> #include <set> #include <map> #include <sstream> #include <typeinfo> #include "message.h" #include "maklib.h" using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<string> vs; #define SIZE(x) ((int)(x.size())) #define LET(var, val) typeof(val) var = (val) #define FOR(k, a, b) for(typeof(a) k = (a); k <= (b); ++k) #define FORR(k, a, b) for(typeof(a) k = (a); k >= (b); --k) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(LET(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define PF push_front const int INF = 1000000001; const double EPS = 10e-9; const double PI = acos(-1.0); int tab[100]; void func() { int n = NumberOfNodes(); int id = MyNodeId(); ll start = id * Size() / n; ll end = (id + 1) * Size() / n; start++; ll max = 0; ll current = 0; int best_start = start, best_end = start; int current_start = start, current_end = start; FOR(i,start,end) { current += ElementAt(i); current_end++; if (current < 0) { current = 0; current_start = i + 1; } if (current > max) { max = current; best_start = current_start; best_end = current_end; } } ll sum_left = 0; ll sum_right = 0; FOR(i, start, best_start - 1) sum_left += ElementAt(i); FOR(i, best_end, end) sum_right += ElementAt(i); PutLL(0, sum_left); PutLL(0, max); PutLL(0, sum_right); Send(0); } ll collect() { int s = NumberOfNodes() * 3; int *tab = new int[s]; int n = s / 3; REP(i, n) { Receive(i); tab[3 * i] = GetLL(i); tab[3 * i + 1] = GetLL(i); tab[3 * i + 2] = GetLL(i); } ll max = 0; ll current = 0; REP(i, s) { current += tab[i]; if (current < 0) current = 0; if (current > max) max = current; } return max; } int main() { func(); if (MyNodeId() == 0) cout << collect() << 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 | #include <algorithm> #include <string> #include <vector> #include <list> #include <cctype> #include <cmath> #include <iostream> #include <set> #include <map> #include <sstream> #include <typeinfo> #include "message.h" #include "maklib.h" using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<string> vs; #define SIZE(x) ((int)(x.size())) #define LET(var, val) typeof(val) var = (val) #define FOR(k, a, b) for(typeof(a) k = (a); k <= (b); ++k) #define FORR(k, a, b) for(typeof(a) k = (a); k >= (b); --k) #define REP(x, n) for(int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(LET(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define PF push_front const int INF = 1000000001; const double EPS = 10e-9; const double PI = acos(-1.0); int tab[100]; void func() { int n = NumberOfNodes(); int id = MyNodeId(); ll start = id * Size() / n; ll end = (id + 1) * Size() / n; start++; ll max = 0; ll current = 0; int best_start = start, best_end = start; int current_start = start, current_end = start; FOR(i,start,end) { current += ElementAt(i); current_end++; if (current < 0) { current = 0; current_start = i + 1; } if (current > max) { max = current; best_start = current_start; best_end = current_end; } } ll sum_left = 0; ll sum_right = 0; FOR(i, start, best_start - 1) sum_left += ElementAt(i); FOR(i, best_end, end) sum_right += ElementAt(i); PutLL(0, sum_left); PutLL(0, max); PutLL(0, sum_right); Send(0); } ll collect() { int s = NumberOfNodes() * 3; int *tab = new int[s]; int n = s / 3; REP(i, n) { Receive(i); tab[3 * i] = GetLL(i); tab[3 * i + 1] = GetLL(i); tab[3 * i + 2] = GetLL(i); } ll max = 0; ll current = 0; REP(i, s) { current += tab[i]; if (current < 0) current = 0; if (current > max) max = current; } return max; } int main() { func(); if (MyNodeId() == 0) cout << collect() << endl; return 0; } |