#include <cstdio> #include <vector> #include "message.h" #include "kanapka.h" using namespace std; int main(){ long long n = GetN(); int comp = NumberOfNodes(); int id = MyNodeId(); if (comp >= n){ comp = n; } if (id >= n){ return 0; } long long e = id * n / comp; long long f = ((id+1) * n / comp) - 1; if (id == comp-1){ f = n-1; } long long sum = 0, best_pocz = 0, best_kon = 0, best_both = 0, all = 0; for (int i = f; i >= e; i--){ sum += GetTaste(i); if (sum > best_kon){ best_kon = sum; } } all = sum; sum = 0; best_both = 0; if (all > 0){ best_both = all; } for (int i = e; i <= f; i++){ sum += GetTaste(i); if (all - sum + best_pocz > best_both){ best_both = all - sum + best_pocz; } if (sum > best_pocz){ best_pocz = sum; } } PutLL(0,all); PutLL(0,best_pocz); PutLL(0,best_kon); PutLL(0,best_both); Send(0); if (id == 0){ long long best = 0; long long *pocz = new long long [comp], *kon = new long long [comp], *all = new long long [comp], *both = new long long [comp], *pref = new long long [comp], *suf = new long long [comp]; for (int i = 0; i < comp; i++){ Receive(i); all[i] = GetLL(i); pocz[i] = GetLL(i); if (i > 0){ pref[i] = pref[i-1] + all[i]; } else { pref[i] = all[i]; } kon[i] = GetLL(i); both[i] = GetLL(i); } if (pref[ comp-1 ] > best){ best = pref[ comp-1 ]; } for (int i = comp-1; i >= 0; i--){ if (i == comp-1){ suf[i] = all[i]; } else { suf[i] = suf[i+1] + all[i]; } } for (int i = 0; i < comp; i++){ for (int j = i+1; j < comp; j++){ long long p = 0, k = 0; if (i > 0){ p = pref[i-1]; } if (j < comp-1){ k = suf[j+1]; } if (p + pocz[i] + k + kon[j] > best){ best = p + pocz[i] + k + kon[j]; } } } for (int i = 0; i < comp; i++){ long long p = 0, k = 0; if (i > 0){ p = pref[i-1]; } if (i < comp-1){ k = suf[i+1]; } if (p + k + both[i] > best){ best = p + k + both[i]; } } printf("%lld\n", best); } }
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 | #include <cstdio> #include <vector> #include "message.h" #include "kanapka.h" using namespace std; int main(){ long long n = GetN(); int comp = NumberOfNodes(); int id = MyNodeId(); if (comp >= n){ comp = n; } if (id >= n){ return 0; } long long e = id * n / comp; long long f = ((id+1) * n / comp) - 1; if (id == comp-1){ f = n-1; } long long sum = 0, best_pocz = 0, best_kon = 0, best_both = 0, all = 0; for (int i = f; i >= e; i--){ sum += GetTaste(i); if (sum > best_kon){ best_kon = sum; } } all = sum; sum = 0; best_both = 0; if (all > 0){ best_both = all; } for (int i = e; i <= f; i++){ sum += GetTaste(i); if (all - sum + best_pocz > best_both){ best_both = all - sum + best_pocz; } if (sum > best_pocz){ best_pocz = sum; } } PutLL(0,all); PutLL(0,best_pocz); PutLL(0,best_kon); PutLL(0,best_both); Send(0); if (id == 0){ long long best = 0; long long *pocz = new long long [comp], *kon = new long long [comp], *all = new long long [comp], *both = new long long [comp], *pref = new long long [comp], *suf = new long long [comp]; for (int i = 0; i < comp; i++){ Receive(i); all[i] = GetLL(i); pocz[i] = GetLL(i); if (i > 0){ pref[i] = pref[i-1] + all[i]; } else { pref[i] = all[i]; } kon[i] = GetLL(i); both[i] = GetLL(i); } if (pref[ comp-1 ] > best){ best = pref[ comp-1 ]; } for (int i = comp-1; i >= 0; i--){ if (i == comp-1){ suf[i] = all[i]; } else { suf[i] = suf[i+1] + all[i]; } } for (int i = 0; i < comp; i++){ for (int j = i+1; j < comp; j++){ long long p = 0, k = 0; if (i > 0){ p = pref[i-1]; } if (j < comp-1){ k = suf[j+1]; } if (p + pocz[i] + k + kon[j] > best){ best = p + pocz[i] + k + kon[j]; } } } for (int i = 0; i < comp; i++){ long long p = 0, k = 0; if (i > 0){ p = pref[i-1]; } if (i < comp-1){ k = suf[i+1]; } if (p + k + both[i] > best){ best = p + k + both[i]; } } printf("%lld\n", best); } } |