#include "kanapka.h" #include "message.h" #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; long long N = GetN(); long long nodes = NumberOfNodes(); long long id = MyNodeId(); long long SafeGetTaste(long long i) { if (i >= N) return 0; return -GetTaste(i); } int main() { long long range_length = (N + nodes - 1) / nodes; long long bestStart = 0, bestEnd = 0, bestMiddle = 0; long long prefix = 0; long long middle = 0; for (long long i = range_length * id; i < range_length * (id + 1); i++) { long long current = SafeGetTaste(i); prefix += current; if (prefix > bestStart) bestStart = prefix; if (middle > 0) middle += current; else middle = current; if (middle > bestMiddle) bestMiddle = middle; } long long suffix = 0; for (long long i = range_length * (id + 1) - 1; i >= range_length * id; i--) { long long current = SafeGetTaste(i); suffix += current; if (suffix > bestEnd) bestEnd = suffix; } if (id > 0) { PutLL(0, bestStart); PutLL(0, bestMiddle); PutLL(0, bestEnd); PutLL(0, prefix); // sum Send(0); } else { long long starts[nodes], middles[nodes], ends[nodes], prefixes[nodes]; starts[0] = bestStart; middles[0] = bestMiddle; ends[0] = bestEnd; prefixes[0] = prefix; long long wholeSum = prefixes[0]; long long res = max(0ll, middles[0]); for (long long i = 1; i < nodes; i++) { Receive((int)i); starts[i] = GetLL(i); middles[i] = GetLL(i); ends[i] = GetLL(i); prefixes[i] = GetLL(i); wholeSum += prefixes[i]; res = max(res, middles[i]); } for (long long i = 0; i < nodes; i++) { for (long long j = i + 1; j < nodes; j++) { long long current = ends[i] + starts[j]; for (long long k = i + 1; k < j; k++) current += prefixes[k]; res = max(res, current); } } cout << -wholeSum + res << 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 | #include "kanapka.h" #include "message.h" #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; long long N = GetN(); long long nodes = NumberOfNodes(); long long id = MyNodeId(); long long SafeGetTaste(long long i) { if (i >= N) return 0; return -GetTaste(i); } int main() { long long range_length = (N + nodes - 1) / nodes; long long bestStart = 0, bestEnd = 0, bestMiddle = 0; long long prefix = 0; long long middle = 0; for (long long i = range_length * id; i < range_length * (id + 1); i++) { long long current = SafeGetTaste(i); prefix += current; if (prefix > bestStart) bestStart = prefix; if (middle > 0) middle += current; else middle = current; if (middle > bestMiddle) bestMiddle = middle; } long long suffix = 0; for (long long i = range_length * (id + 1) - 1; i >= range_length * id; i--) { long long current = SafeGetTaste(i); suffix += current; if (suffix > bestEnd) bestEnd = suffix; } if (id > 0) { PutLL(0, bestStart); PutLL(0, bestMiddle); PutLL(0, bestEnd); PutLL(0, prefix); // sum Send(0); } else { long long starts[nodes], middles[nodes], ends[nodes], prefixes[nodes]; starts[0] = bestStart; middles[0] = bestMiddle; ends[0] = bestEnd; prefixes[0] = prefix; long long wholeSum = prefixes[0]; long long res = max(0ll, middles[0]); for (long long i = 1; i < nodes; i++) { Receive((int)i); starts[i] = GetLL(i); middles[i] = GetLL(i); ends[i] = GetLL(i); prefixes[i] = GetLL(i); wholeSum += prefixes[i]; res = max(res, middles[i]); } for (long long i = 0; i < nodes; i++) { for (long long j = i + 1; j < nodes; j++) { long long current = ends[i] + starts[j]; for (long long k = i + 1; k < j; k++) current += prefixes[k]; res = max(res, current); } } cout << -wholeSum + res << endl; } return 0; } |