// Błędne rozwiązanie do zadania Działka 2. // Na pojedynczym węźle wypisuje liczbę dostępnych kwadracików. #include "dzialka.h" #include "message.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point { int value = 0; long long sum = 0; int size = 0; }; bool operator<(const Point &p1, const Point &p2) { return p1.value < p2.value; }; const int nodeId = MyNodeId(); const int nodes = NumberOfNodes(); const int height = GetFieldHeight(); const int width = GetFieldWidth(); void sendDistances(int receiver, const vector<int> &distances, int size) { for (int i = 0; i < size; i++) { PutInt(receiver, distances[i]); } Send(receiver); } void computeColumns() { vector<int> distances; distances.resize(width / nodes + 1); int receiver = 0; for (int i = 0; i < height && receiver < nodes; i++) { if (i % (height / min(nodes, height)) == 0) { sendDistances(receiver, distances, (width - nodeId + nodes - 1) / nodes); receiver++; } int k = 0; for (int j = nodeId; j < width; j+=nodes) { if (IsUsableCell(i, j)) { distances[k]++; } else { distances[k] = 0; } k++; } } } int update(vector<Point> &rectangles, int val, int length) { if (val == 0) { return 0; } Point v; v.value = val; if (length == 0) { v.size = 1; v.sum = val; rectangles[0] = v; return 1; } Point last = rectangles[length-1]; v.size = last.size + 1; auto iter = lower_bound(rectangles.begin(), rectangles.begin() + length, v); length = distance(rectangles.begin(), iter) + 1; if (iter == rectangles.begin()) { v.sum = val * v.size; } else { iter--; Point prev = *iter; v.sum = val * (v.size - prev.size) + prev.sum; } rectangles[length-1] = v; return length; } void computeRows() { vector<int> distances; distances.resize(width); for (int i = 0; i < min(nodes, width); i++) { Receive(i); for (int j = i; j < width; j+=nodes) { distances[j] = GetInt(i); } } int start = (height / min(height, nodes)) * nodeId; int end; if (nodeId == nodes - 1 || nodeId == height - 1) { end = height - 1; } else { end = (height / min(height, nodes)) * (nodeId + 1) - 1; } long long result = 0; vector<Point> rectangles; rectangles.resize(width); for (int i = start; i <= end; i++) { int length = 0; for (int j = 0; j < width; j++) { if (IsUsableCell(i, j)) { distances[j]++; } else { distances[j]=0; } length = update(rectangles, distances[j], length); if (length > 0) { result += rectangles[length-1].sum; } } } PutLL(0, result); Send(0); } void computeSum() { long long final_result = 0; for (int i = 0; i < min(height,nodes); i++) { int sender = Receive(-1); final_result += GetLL(sender); } cout << final_result << endl; } int main() { if (nodeId < width) { computeColumns(); } if (nodeId < height) { computeRows(); } if (nodeId == 0) { computeSum(); } }
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 | // Błędne rozwiązanie do zadania Działka 2. // Na pojedynczym węźle wypisuje liczbę dostępnych kwadracików. #include "dzialka.h" #include "message.h" #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point { int value = 0; long long sum = 0; int size = 0; }; bool operator<(const Point &p1, const Point &p2) { return p1.value < p2.value; }; const int nodeId = MyNodeId(); const int nodes = NumberOfNodes(); const int height = GetFieldHeight(); const int width = GetFieldWidth(); void sendDistances(int receiver, const vector<int> &distances, int size) { for (int i = 0; i < size; i++) { PutInt(receiver, distances[i]); } Send(receiver); } void computeColumns() { vector<int> distances; distances.resize(width / nodes + 1); int receiver = 0; for (int i = 0; i < height && receiver < nodes; i++) { if (i % (height / min(nodes, height)) == 0) { sendDistances(receiver, distances, (width - nodeId + nodes - 1) / nodes); receiver++; } int k = 0; for (int j = nodeId; j < width; j+=nodes) { if (IsUsableCell(i, j)) { distances[k]++; } else { distances[k] = 0; } k++; } } } int update(vector<Point> &rectangles, int val, int length) { if (val == 0) { return 0; } Point v; v.value = val; if (length == 0) { v.size = 1; v.sum = val; rectangles[0] = v; return 1; } Point last = rectangles[length-1]; v.size = last.size + 1; auto iter = lower_bound(rectangles.begin(), rectangles.begin() + length, v); length = distance(rectangles.begin(), iter) + 1; if (iter == rectangles.begin()) { v.sum = val * v.size; } else { iter--; Point prev = *iter; v.sum = val * (v.size - prev.size) + prev.sum; } rectangles[length-1] = v; return length; } void computeRows() { vector<int> distances; distances.resize(width); for (int i = 0; i < min(nodes, width); i++) { Receive(i); for (int j = i; j < width; j+=nodes) { distances[j] = GetInt(i); } } int start = (height / min(height, nodes)) * nodeId; int end; if (nodeId == nodes - 1 || nodeId == height - 1) { end = height - 1; } else { end = (height / min(height, nodes)) * (nodeId + 1) - 1; } long long result = 0; vector<Point> rectangles; rectangles.resize(width); for (int i = start; i <= end; i++) { int length = 0; for (int j = 0; j < width; j++) { if (IsUsableCell(i, j)) { distances[j]++; } else { distances[j]=0; } length = update(rectangles, distances[j], length); if (length > 0) { result += rectangles[length-1].sum; } } } PutLL(0, result); Send(0); } void computeSum() { long long final_result = 0; for (int i = 0; i < min(height,nodes); i++) { int sender = Receive(-1); final_result += GetLL(sender); } cout << final_result << endl; } int main() { if (nodeId < width) { computeColumns(); } if (nodeId < height) { computeRows(); } if (nodeId == 0) { computeSum(); } } |