#include "message.h" #include "kanapka.h" #include <iostream> typedef long long long64; int main() { int node = MyNodeId(); int numNodes = NumberOfNodes(); long64 n = GetN(); long64 size = n / numNodes; if (n < 1000) { if (node != 0) return 0; long64* tab = new long64[n]; long64 max = 0; long64 sum = 0; for (long64 i = 0; i < n; ++i) { //std::cout << GetTaste(i) << " "; sum += GetTaste(i); if (sum > max) { max = sum; } tab[i] = max; } long64 result = 0; max = 0; sum = 0; for (long64 i = n - 1; i >= 0; --i) { if (tab[i] + sum > result) result = tab[i] + sum; sum += GetTaste(i); } if (sum > result) result = sum; std::cout << result << std::endl; delete[] tab; return 0; } long64 start = node * size; long64 end = start + size; if (node == numNodes - 1) end = n; //std::cout << node << ": " << start << "" << end << std::endl; //return 0; long64* topSumArray = new long64[end - start]; long64* bottomVal = new long64[end - start]; long64 topMax = 0; long64 topSum = 0; long64 bottomMax = 0; long64 bottomSum = 0; for (long64 ii = start, i = 0; ii < end; ++i, ++ii) { topSum += GetTaste(ii); if (topSum > topMax) { topMax = topSum; } bottomSum += GetTaste(end - i - 1); if (bottomSum > bottomMax) { bottomMax = bottomSum; } topSumArray[i] = topSum; bottomVal[i] = GetTaste(ii); } //std::cout << node << "[topsum, topmax]: " << bottomSum << " " << bottomMax << std::endl; long64 leftSum = 0; long64 leftMax = 0; long64 rightSum = 0; long64 rightMax = 0; if (node < numNodes / 2) { if (node != 0) { //std::cout << node << ": 1waitFroReceive " << node - 1 << std::endl; int from = Receive(node - 1); leftSum = GetLL(from); leftMax = GetLL(from); //std::cout << node << ": 1Receive " << from << std::endl; topSum += leftSum; topMax += leftSum; if (leftMax > topMax) topMax = leftMax; } PutLL(node + 1, topSum); PutLL(node + 1, topMax); Send(node + 1); //std::cout << node << ": 2Send " << node + 1 << std::endl; int from = Receive(node + 1); rightSum = GetLL(from); rightMax = GetLL(from); //std::cout << node << ": 3Receive " << from << std::endl; bottomSum += rightSum; bottomMax += rightSum; if (rightMax > bottomMax) bottomMax = rightMax; if (node != 0) { PutLL(node - 1, bottomSum); PutLL(node - 1, bottomMax); Send(node - 1); } } else { if (node != numNodes - 1) { //std::cout << node << ": 4waitForReceive " << node + 1 << std::endl; int from = Receive(node + 1); rightSum = GetLL(from); rightMax = GetLL(from); //std::cout << node << ": 4Receive " << from << std::endl; bottomSum += rightSum; bottomMax += rightSum; if (rightMax > bottomMax) bottomMax = rightMax; } PutLL(node - 1, bottomSum); PutLL(node - 1, bottomMax); Send(node - 1); //std::cout << node << ": 5Send " << node - 1 << std::endl; int from = Receive(node - 1); leftSum = GetLL(from); leftMax = GetLL(from); // std::cout << node << ": 6Receive " << from << std::endl; topSum += leftSum; topMax += leftSum; if (leftMax > topMax) topMax = leftMax; if (node != numNodes - 1) { PutLL(node + 1, topSum); PutLL(node + 1, topMax); Send(node + 1); } } //std::cout << node << "[lsum, lmax, rsum, rmax]: " << leftSum << " " << leftMax << " " << rightSum << " " << rightMax << std::endl; long64 maxTotal = 0; for (long64 ii = end - 1, i = end - start - 1; ii >= start; --i, --ii) { topSumArray[i] += leftSum; if (topSumArray[i] + rightMax > maxTotal) maxTotal = topSumArray[i] + rightMax; rightSum += bottomVal[i]; if (rightSum > rightMax) rightMax = rightSum; } //std::cout << node << "[high, lowest]: " << highest << " " << lowest << std::endl; //std::cout << node << "[max, max]: " << leftMax << " " << rightMax << std::endl; if (node < numNodes - 1) { int from = Receive(node + 1); long64 otherResult = GetLL(from); if (otherResult > maxTotal) maxTotal = otherResult; } if (node > 0) { PutLL(node - 1, maxTotal); Send(node - 1); } else { std::cout << maxTotal << std::endl; } delete[] topSumArray; delete[] bottomVal; 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | #include "message.h" #include "kanapka.h" #include <iostream> typedef long long long64; int main() { int node = MyNodeId(); int numNodes = NumberOfNodes(); long64 n = GetN(); long64 size = n / numNodes; if (n < 1000) { if (node != 0) return 0; long64* tab = new long64[n]; long64 max = 0; long64 sum = 0; for (long64 i = 0; i < n; ++i) { //std::cout << GetTaste(i) << " "; sum += GetTaste(i); if (sum > max) { max = sum; } tab[i] = max; } long64 result = 0; max = 0; sum = 0; for (long64 i = n - 1; i >= 0; --i) { if (tab[i] + sum > result) result = tab[i] + sum; sum += GetTaste(i); } if (sum > result) result = sum; std::cout << result << std::endl; delete[] tab; return 0; } long64 start = node * size; long64 end = start + size; if (node == numNodes - 1) end = n; //std::cout << node << ": " << start << "" << end << std::endl; //return 0; long64* topSumArray = new long64[end - start]; long64* bottomVal = new long64[end - start]; long64 topMax = 0; long64 topSum = 0; long64 bottomMax = 0; long64 bottomSum = 0; for (long64 ii = start, i = 0; ii < end; ++i, ++ii) { topSum += GetTaste(ii); if (topSum > topMax) { topMax = topSum; } bottomSum += GetTaste(end - i - 1); if (bottomSum > bottomMax) { bottomMax = bottomSum; } topSumArray[i] = topSum; bottomVal[i] = GetTaste(ii); } //std::cout << node << "[topsum, topmax]: " << bottomSum << " " << bottomMax << std::endl; long64 leftSum = 0; long64 leftMax = 0; long64 rightSum = 0; long64 rightMax = 0; if (node < numNodes / 2) { if (node != 0) { //std::cout << node << ": 1waitFroReceive " << node - 1 << std::endl; int from = Receive(node - 1); leftSum = GetLL(from); leftMax = GetLL(from); //std::cout << node << ": 1Receive " << from << std::endl; topSum += leftSum; topMax += leftSum; if (leftMax > topMax) topMax = leftMax; } PutLL(node + 1, topSum); PutLL(node + 1, topMax); Send(node + 1); //std::cout << node << ": 2Send " << node + 1 << std::endl; int from = Receive(node + 1); rightSum = GetLL(from); rightMax = GetLL(from); //std::cout << node << ": 3Receive " << from << std::endl; bottomSum += rightSum; bottomMax += rightSum; if (rightMax > bottomMax) bottomMax = rightMax; if (node != 0) { PutLL(node - 1, bottomSum); PutLL(node - 1, bottomMax); Send(node - 1); } } else { if (node != numNodes - 1) { //std::cout << node << ": 4waitForReceive " << node + 1 << std::endl; int from = Receive(node + 1); rightSum = GetLL(from); rightMax = GetLL(from); //std::cout << node << ": 4Receive " << from << std::endl; bottomSum += rightSum; bottomMax += rightSum; if (rightMax > bottomMax) bottomMax = rightMax; } PutLL(node - 1, bottomSum); PutLL(node - 1, bottomMax); Send(node - 1); //std::cout << node << ": 5Send " << node - 1 << std::endl; int from = Receive(node - 1); leftSum = GetLL(from); leftMax = GetLL(from); // std::cout << node << ": 6Receive " << from << std::endl; topSum += leftSum; topMax += leftSum; if (leftMax > topMax) topMax = leftMax; if (node != numNodes - 1) { PutLL(node + 1, topSum); PutLL(node + 1, topMax); Send(node + 1); } } //std::cout << node << "[lsum, lmax, rsum, rmax]: " << leftSum << " " << leftMax << " " << rightSum << " " << rightMax << std::endl; long64 maxTotal = 0; for (long64 ii = end - 1, i = end - start - 1; ii >= start; --i, --ii) { topSumArray[i] += leftSum; if (topSumArray[i] + rightMax > maxTotal) maxTotal = topSumArray[i] + rightMax; rightSum += bottomVal[i]; if (rightSum > rightMax) rightMax = rightSum; } //std::cout << node << "[high, lowest]: " << highest << " " << lowest << std::endl; //std::cout << node << "[max, max]: " << leftMax << " " << rightMax << std::endl; if (node < numNodes - 1) { int from = Receive(node + 1); long64 otherResult = GetLL(from); if (otherResult > maxTotal) maxTotal = otherResult; } if (node > 0) { PutLL(node - 1, maxTotal); Send(node - 1); } else { std::cout << maxTotal << std::endl; } delete[] topSumArray; delete[] bottomVal; return 0; } |