#include "message.h" #include "kanapka.h" #include <stdio.h> #define MIN(a,b) ((a) > (b)?(b):(a)) #define MAX(a,b) ((a) < (b)?(b):(a)) // Dane dotyczące instancji int nodesCount; int myNodeNr; int nextNodeNr, prevNodeNr; int iAmLast; // Dane dotyczące zadania long long N; long long start, count; // dane tworzone przez program long long sum, sumMax, sumMin, sumMinPoMax, delta; int main() { nodesCount = NumberOfNodes(); myNodeNr = MyNodeId(); nextNodeNr = myNodeNr + 1; prevNodeNr = myNodeNr - 1; N = GetN(); count = N / nodesCount; if(N % nodesCount > 0) count++; start = count * (myNodeNr); if(start < N) { // Jest robota dla mnie count = MIN(count, N - start); if(start + count == N) iAmLast = 1; else iAmLast = 0; fprintf(stderr, "Instancja %d - od %lld do %lld\n", myNodeNr, start, start + count - 1); sum = GetTaste(start); sumMax = sum; sumMin = sum; sumMinPoMax = sum; delta = 0; while(--count) { sum += GetTaste(++start); sumMinPoMax = MIN(sumMinPoMax, sum); delta = MAX(sumMax - sumMinPoMax, delta); if (sum > sumMax) { sumMax = sum; sumMinPoMax = sum; } if(sum < sumMin) { sumMin = sum; } } if(myNodeNr > 0) { long long oldSum, oldSumMax, oldDelta; Receive(prevNodeNr); oldSum = GetLL(prevNodeNr); oldSumMax = GetLL(prevNodeNr); oldDelta = GetLL(prevNodeNr); sum += oldSum; sumMax += oldSum; sumMin += oldSum; delta = MAX(delta, oldSumMax - sumMin); delta = MAX(delta, oldDelta); sumMax = MAX(sumMax, oldSumMax); } if(iAmLast == 1) { printf("%lld",sum + delta); } else { PutLL(nextNodeNr, sum); PutLL(nextNodeNr, sumMax); PutLL(nextNodeNr, delta); Send(nextNodeNr); fprintf(stderr, "%d wysyłam: sum = %lld, sumMax = %lld, delta = %lld\n", myNodeNr, sum, sumMax, delta); } } else { fprintf(stderr, "Instancja %d - Nic do roboty\n", myNodeNr); } 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 | #include "message.h" #include "kanapka.h" #include <stdio.h> #define MIN(a,b) ((a) > (b)?(b):(a)) #define MAX(a,b) ((a) < (b)?(b):(a)) // Dane dotyczące instancji int nodesCount; int myNodeNr; int nextNodeNr, prevNodeNr; int iAmLast; // Dane dotyczące zadania long long N; long long start, count; // dane tworzone przez program long long sum, sumMax, sumMin, sumMinPoMax, delta; int main() { nodesCount = NumberOfNodes(); myNodeNr = MyNodeId(); nextNodeNr = myNodeNr + 1; prevNodeNr = myNodeNr - 1; N = GetN(); count = N / nodesCount; if(N % nodesCount > 0) count++; start = count * (myNodeNr); if(start < N) { // Jest robota dla mnie count = MIN(count, N - start); if(start + count == N) iAmLast = 1; else iAmLast = 0; fprintf(stderr, "Instancja %d - od %lld do %lld\n", myNodeNr, start, start + count - 1); sum = GetTaste(start); sumMax = sum; sumMin = sum; sumMinPoMax = sum; delta = 0; while(--count) { sum += GetTaste(++start); sumMinPoMax = MIN(sumMinPoMax, sum); delta = MAX(sumMax - sumMinPoMax, delta); if (sum > sumMax) { sumMax = sum; sumMinPoMax = sum; } if(sum < sumMin) { sumMin = sum; } } if(myNodeNr > 0) { long long oldSum, oldSumMax, oldDelta; Receive(prevNodeNr); oldSum = GetLL(prevNodeNr); oldSumMax = GetLL(prevNodeNr); oldDelta = GetLL(prevNodeNr); sum += oldSum; sumMax += oldSum; sumMin += oldSum; delta = MAX(delta, oldSumMax - sumMin); delta = MAX(delta, oldDelta); sumMax = MAX(sumMax, oldSumMax); } if(iAmLast == 1) { printf("%lld",sum + delta); } else { PutLL(nextNodeNr, sum); PutLL(nextNodeNr, sumMax); PutLL(nextNodeNr, delta); Send(nextNodeNr); fprintf(stderr, "%d wysyłam: sum = %lld, sumMax = %lld, delta = %lld\n", myNodeNr, sum, sumMax, delta); } } else { fprintf(stderr, "Instancja %d - Nic do roboty\n", myNodeNr); } return 0; } |