#include "message.h" #include <iostream> using namespace std; #include <assert.h> #include <stdio.h> #include <stdlib.h> static int n; static int* data; static void Init() { static int initialized = 0; int i; if (initialized) return; assert(1 == scanf("%d", &n)); data = (int*)malloc((n + 1) * sizeof(int)); assert(data != NULL); for (i = 1; i <= n; ++i) { assert(1 == scanf("%d", data + i)); } initialized = 1; } int Size() { Init(); return n; } int ElementAt(int i) { Init(); assert(1 <= i && i <= n); return data[i]; } int main() { int N = Size(); int iloscWezlow = NumberOfNodes(); int ktoryJestemWezel = MyNodeId(); long long poczatek = (ktoryJestemWezel * N) / iloscWezlow; long long koniec = ((ktoryJestemWezel + 1) * N) / iloscWezlow; long long maxDelta = 0; long long min = 0; long long max = 0; long long suma = 0; for (long long i = poczatek; i < koniec; ++i) { long long nowyElement = ElementAt(i); suma += nowyElement; if (suma < min) min = suma; if (suma > max) max = suma; if (suma - min > maxDelta) maxDelta = suma - min; } if (ktoryJestemWezel > 0) { PutLL(0, maxDelta); PutLL(0, min); PutLL(0, max); PutLL(0, suma); Send(0); } else { for (int instancja = 1; instancja < iloscWezlow; ++instancja) { Receive(instancja); long long newMaxDelta = GetLL(instancja); long long newMin = GetLL(instancja) + suma; long long newMax = GetLL(instancja) + suma; long long newSuma = GetLL(instancja) + suma; suma = newSuma; if (newMaxDelta > maxDelta) maxDelta = newMaxDelta; if (newMax - min > maxDelta) maxDelta = newMax - min; if (newMax > max) max = newMax; if (newMin < min) min = newMin; } cout << maxDelta << 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 | #include "message.h" #include <iostream> using namespace std; #include <assert.h> #include <stdio.h> #include <stdlib.h> static int n; static int* data; static void Init() { static int initialized = 0; int i; if (initialized) return; assert(1 == scanf("%d", &n)); data = (int*)malloc((n + 1) * sizeof(int)); assert(data != NULL); for (i = 1; i <= n; ++i) { assert(1 == scanf("%d", data + i)); } initialized = 1; } int Size() { Init(); return n; } int ElementAt(int i) { Init(); assert(1 <= i && i <= n); return data[i]; } int main() { int N = Size(); int iloscWezlow = NumberOfNodes(); int ktoryJestemWezel = MyNodeId(); long long poczatek = (ktoryJestemWezel * N) / iloscWezlow; long long koniec = ((ktoryJestemWezel + 1) * N) / iloscWezlow; long long maxDelta = 0; long long min = 0; long long max = 0; long long suma = 0; for (long long i = poczatek; i < koniec; ++i) { long long nowyElement = ElementAt(i); suma += nowyElement; if (suma < min) min = suma; if (suma > max) max = suma; if (suma - min > maxDelta) maxDelta = suma - min; } if (ktoryJestemWezel > 0) { PutLL(0, maxDelta); PutLL(0, min); PutLL(0, max); PutLL(0, suma); Send(0); } else { for (int instancja = 1; instancja < iloscWezlow; ++instancja) { Receive(instancja); long long newMaxDelta = GetLL(instancja); long long newMin = GetLL(instancja) + suma; long long newMax = GetLL(instancja) + suma; long long newSuma = GetLL(instancja) + suma; suma = newSuma; if (newMaxDelta > maxDelta) maxDelta = newMaxDelta; if (newMax - min > maxDelta) maxDelta = newMax - min; if (newMax > max) max = newMax; if (newMin < min) min = newMin; } cout << maxDelta << endl; } return 0; } |