#include <stdio.h> #include <vector> #include <algorithm> #include "krazki.h" #include "message.h" using namespace std; const int N = 2000010, X = 110; const long long M = 1000000000000000010; struct part { int l, r, target; }; bool sended[X]; int n, m, nn, mn; long long pipe[N], disc[N], NodeMin[X]; vector <part> Parts, ToMerge, Merged; void PipesEq() { pipe[0] = M; for (int i = 1; i <= nn; ++i) pipe[i] = min(pipe[i - 1], HoleDiameter(n * MyNodeId() + i)); long long prev; if (MyNodeId() > 0) { Receive(MyNodeId() - 1); prev = GetLL(MyNodeId() - 1); } if (MyNodeId() != NumberOfNodes() - 1) { PutLL(MyNodeId() + 1, min(prev, pipe[nn])); Send(MyNodeId() + 1); } for (int i = 1; i <= nn; ++i) pipe[i] = min(pipe[i], prev); } void DiscsEq() { disc[mn + 1] = 0; for (int i = mn; i > 0; --i) disc[i] = max(disc[i + 1], DiscDiameter(m * MyNodeId() + i)); long long prev; if (MyNodeId() != NumberOfNodes() - 1) { Receive(MyNodeId() + 1); prev = GetLL(MyNodeId() + 1); } if (MyNodeId() > 0) { PutLL(MyNodeId() - 1, max(prev, disc[1])); Send(MyNodeId() - 1); } for (int i = 1; i <= mn; ++i) disc[i] = max(disc[i], prev); } void MakeNodeMin() { for (int i = 0; i < NumberOfNodes(); ++i) { PutLL(i, pipe[nn]); Send(i); } for (int i = 0; i < NumberOfNodes(); ++i) { Receive(i); NodeMin[i] = GetLL(i); } } void MakeParts() { int l, r, prev = -1; for (int i = 1; i <= mn; ++i) { int a = 0, b = NumberOfNodes(), c; while (a < b) { c = (a + b) / 2; if (NodeMin[c] >= disc[i]) a = c + 1; else b = c; } if (prev == a) ++r; else { if (prev != -1) { part temp; temp.l = l, temp.r = r, temp.target = prev; Parts.push_back(temp); } l = m * MyNodeId() + i, r = m * MyNodeId() + i, prev = a; } } part temp; temp.l = l, temp.r = r, temp.target = prev; Parts.push_back(temp); } void SendParts() { PutInt(0, (int)Parts.size()); for (int i = 0; i < Parts.size(); ++i) { PutInt(0, Parts[i].l); PutInt(0, Parts[i].r); PutInt(0, Parts[i].target); } Send(0); } void MergeParts() { for (int i = 0; i < NumberOfNodes(); ++i) { Receive(i); int Num = GetInt(i); for (int j = 0; j < Num; ++j) { part temp; temp.l = GetInt(i), temp.r = GetInt(i), temp.target = GetInt(i); ToMerge.push_back(temp); } } int prev = -1; for (int i = 0; i < ToMerge.size(); ++i) { if (ToMerge[i].target != prev) { Merged.push_back(ToMerge[i]); prev = ToMerge[i].target; } else Merged.back().r = ToMerge[i].r; } for (int i = 0; i = Merged.size(); ++i) { Merged[i].l = max(Merged[i].l, Merged[i].r - n); } } void SendMergedParts() { for (int i = 0; i = Merged.size(); ++i) { PutInt(Merged[i].target, Merged[i].l); PutInt(Merged[i].target, Merged[i].r); Send(Merged[i].target); sended[Merged[i].target] = true; } for (int i = 0; i < NumberOfNodes(); ++i) if (!sended[i]) { PutInt(i, -1); PutInt(i, -1); Send(i); } } void ReceiveMergedParts() { Receive(0); int mini = 2000000010; int l = GetInt(0), r = GetInt(0); if (l != -1) { for (int i = l; i <= r; ++i) { long long d = DiscDiameter(i); int a = 1, b = nn, c; while (a < b) { c = (a + b) / 2; if (pipe[c] >= disc[i]) a = c + 1; else b = c; } mini = min(mini, n * MyNodeId() + a - i); } PutInt(0, mini); Send(0); } else { PutInt(0, 2000000010); Send(0); } } void Result() { int mini = 2000000010; for (int i = 0; i < NumberOfNodes(); ++i) { Receive(i); mini = min(mini, GetInt(i)); } printf("%d", mini); } int main() { int n = ceil((double)PipeHeight() / (double)NumberOfNodes()), m = ceil((double)NumberOfDiscs() / (double)NumberOfNodes()); int nn = min(n, PipeHeight() - n * MyNodeId()), mn = min(m, NumberOfDiscs() - m * MyNodeId()); PipesEq(); DiscsEq(); MakeNodeMin(); MakeParts(); SendParts(); if (MyNodeId() == 0) MergeParts(); if (MyNodeId() == 0) SendMergedParts(); ReceiveMergedParts(); if (MyNodeId() == 0) Result(); 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 | #include <stdio.h> #include <vector> #include <algorithm> #include "krazki.h" #include "message.h" using namespace std; const int N = 2000010, X = 110; const long long M = 1000000000000000010; struct part { int l, r, target; }; bool sended[X]; int n, m, nn, mn; long long pipe[N], disc[N], NodeMin[X]; vector <part> Parts, ToMerge, Merged; void PipesEq() { pipe[0] = M; for (int i = 1; i <= nn; ++i) pipe[i] = min(pipe[i - 1], HoleDiameter(n * MyNodeId() + i)); long long prev; if (MyNodeId() > 0) { Receive(MyNodeId() - 1); prev = GetLL(MyNodeId() - 1); } if (MyNodeId() != NumberOfNodes() - 1) { PutLL(MyNodeId() + 1, min(prev, pipe[nn])); Send(MyNodeId() + 1); } for (int i = 1; i <= nn; ++i) pipe[i] = min(pipe[i], prev); } void DiscsEq() { disc[mn + 1] = 0; for (int i = mn; i > 0; --i) disc[i] = max(disc[i + 1], DiscDiameter(m * MyNodeId() + i)); long long prev; if (MyNodeId() != NumberOfNodes() - 1) { Receive(MyNodeId() + 1); prev = GetLL(MyNodeId() + 1); } if (MyNodeId() > 0) { PutLL(MyNodeId() - 1, max(prev, disc[1])); Send(MyNodeId() - 1); } for (int i = 1; i <= mn; ++i) disc[i] = max(disc[i], prev); } void MakeNodeMin() { for (int i = 0; i < NumberOfNodes(); ++i) { PutLL(i, pipe[nn]); Send(i); } for (int i = 0; i < NumberOfNodes(); ++i) { Receive(i); NodeMin[i] = GetLL(i); } } void MakeParts() { int l, r, prev = -1; for (int i = 1; i <= mn; ++i) { int a = 0, b = NumberOfNodes(), c; while (a < b) { c = (a + b) / 2; if (NodeMin[c] >= disc[i]) a = c + 1; else b = c; } if (prev == a) ++r; else { if (prev != -1) { part temp; temp.l = l, temp.r = r, temp.target = prev; Parts.push_back(temp); } l = m * MyNodeId() + i, r = m * MyNodeId() + i, prev = a; } } part temp; temp.l = l, temp.r = r, temp.target = prev; Parts.push_back(temp); } void SendParts() { PutInt(0, (int)Parts.size()); for (int i = 0; i < Parts.size(); ++i) { PutInt(0, Parts[i].l); PutInt(0, Parts[i].r); PutInt(0, Parts[i].target); } Send(0); } void MergeParts() { for (int i = 0; i < NumberOfNodes(); ++i) { Receive(i); int Num = GetInt(i); for (int j = 0; j < Num; ++j) { part temp; temp.l = GetInt(i), temp.r = GetInt(i), temp.target = GetInt(i); ToMerge.push_back(temp); } } int prev = -1; for (int i = 0; i < ToMerge.size(); ++i) { if (ToMerge[i].target != prev) { Merged.push_back(ToMerge[i]); prev = ToMerge[i].target; } else Merged.back().r = ToMerge[i].r; } for (int i = 0; i = Merged.size(); ++i) { Merged[i].l = max(Merged[i].l, Merged[i].r - n); } } void SendMergedParts() { for (int i = 0; i = Merged.size(); ++i) { PutInt(Merged[i].target, Merged[i].l); PutInt(Merged[i].target, Merged[i].r); Send(Merged[i].target); sended[Merged[i].target] = true; } for (int i = 0; i < NumberOfNodes(); ++i) if (!sended[i]) { PutInt(i, -1); PutInt(i, -1); Send(i); } } void ReceiveMergedParts() { Receive(0); int mini = 2000000010; int l = GetInt(0), r = GetInt(0); if (l != -1) { for (int i = l; i <= r; ++i) { long long d = DiscDiameter(i); int a = 1, b = nn, c; while (a < b) { c = (a + b) / 2; if (pipe[c] >= disc[i]) a = c + 1; else b = c; } mini = min(mini, n * MyNodeId() + a - i); } PutInt(0, mini); Send(0); } else { PutInt(0, 2000000010); Send(0); } } void Result() { int mini = 2000000010; for (int i = 0; i < NumberOfNodes(); ++i) { Receive(i); mini = min(mini, GetInt(i)); } printf("%d", mini); } int main() { int n = ceil((double)PipeHeight() / (double)NumberOfNodes()), m = ceil((double)NumberOfDiscs() / (double)NumberOfNodes()); int nn = min(n, PipeHeight() - n * MyNodeId()), mn = min(m, NumberOfDiscs() - m * MyNodeId()); PipesEq(); DiscsEq(); MakeNodeMin(); MakeParts(); SendParts(); if (MyNodeId() == 0) MergeParts(); if (MyNodeId() == 0) SendMergedParts(); ReceiveMergedParts(); if (MyNodeId() == 0) Result(); return 0; } |