#include <message.h> #include "krazki.h" #include <vector> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; int myid; int numOfNodes; int H, D; int getFirstH(int id) { return (ll(id) * H) / numOfNodes + 1; } int getFirstD(int id) { return (ll(id) * D) / numOfNodes + 1; } int main() { H = PipeHeight(); D = NumberOfDiscs(); numOfNodes = NumberOfNodes(); // int numOfNodes = min(N, NumberOfNodes()); // H large, D tiny // H tiny, D large // testy: żaden walec się nie mieści // wszystkie walce się swobodnie mieszczą, ale i tak wystają myid = MyNodeId(); // if (myid >= numOfNodes) // return 0; int firstH = getFirstH(myid); int lastH = getFirstH(myid + 1); // cout << numOfNodes << " " << firstN << " " << lastN << endl; ll minHole = numeric_limits<ll>::max(); for (int h = firstH; h < lastH; h++) minHole = min(minHole, HoleDiameter(h)); vector<ll> minHoles(numOfNodes); if (myid > 0) { PutLL(0, minHole); Send(0); Receive(0); for (int i = 0; i < numOfNodes; i++) minHoles[i] = GetLL(0); } else { minHoles[0] = minHole; for (int i = 1; i < numOfNodes; i++) { int id = Receive(-1); minHoles[id] = GetLL(id); } for (int id = 1; id < numOfNodes; id++) { for (int i = 0; i < numOfNodes; i++) PutLL(id, minHoles[i]); Send(id); } } // --------------------------------------------------- // int firstD = getFirstD(myid); int lastD = getFirstD(myid + 1); ll maxDisc = 0; for (int d = firstD; d < lastD; d++) maxDisc = max(maxDisc, DiscDiameter(d)); int holesSegment = numOfNodes; for (int id = 0; id < numOfNodes; id++) if (minHoles[id] < maxDisc) { holesSegment = id; break; } int minBase = H - (D - firstD); if (holesSegment < numOfNodes) { int lastH = min(H+1, getFirstH(holesSegment + 1) + lastD - firstD + 1); int firstH = max(1, getFirstH(holesSegment) - 1); ll minHole = numeric_limits<ll>::max(); vector<ll> holes(lastH - firstH); for (int h = firstH; h < lastH; h++) { minHole = min(minHole, HoleDiameter(h)); holes[h - firstH] = minHole; } int h = lastH - 1; for (int d = firstD; d < lastD; d++) { ll disc = DiscDiameter(d); while (h >= firstH && holes[h - firstH] < disc) h--; minBase = min(minBase, h - (D - d)); } } // --------------------------------------------------- // if (myid > 0) { PutInt(0, minBase); Send(0); } else { // cout << 0 << ": " << minBase << "\n"; for (int i = 1; i < numOfNodes; i++) { int id = Receive(-1); int mb = GetInt(id); minBase = min(minBase, mb); // cout << id << ": " << mb << "\n"; } cout << max(0, minBase) << "\n"; } 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 | #include <message.h> #include "krazki.h" #include <vector> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; int myid; int numOfNodes; int H, D; int getFirstH(int id) { return (ll(id) * H) / numOfNodes + 1; } int getFirstD(int id) { return (ll(id) * D) / numOfNodes + 1; } int main() { H = PipeHeight(); D = NumberOfDiscs(); numOfNodes = NumberOfNodes(); // int numOfNodes = min(N, NumberOfNodes()); // H large, D tiny // H tiny, D large // testy: żaden walec się nie mieści // wszystkie walce się swobodnie mieszczą, ale i tak wystają myid = MyNodeId(); // if (myid >= numOfNodes) // return 0; int firstH = getFirstH(myid); int lastH = getFirstH(myid + 1); // cout << numOfNodes << " " << firstN << " " << lastN << endl; ll minHole = numeric_limits<ll>::max(); for (int h = firstH; h < lastH; h++) minHole = min(minHole, HoleDiameter(h)); vector<ll> minHoles(numOfNodes); if (myid > 0) { PutLL(0, minHole); Send(0); Receive(0); for (int i = 0; i < numOfNodes; i++) minHoles[i] = GetLL(0); } else { minHoles[0] = minHole; for (int i = 1; i < numOfNodes; i++) { int id = Receive(-1); minHoles[id] = GetLL(id); } for (int id = 1; id < numOfNodes; id++) { for (int i = 0; i < numOfNodes; i++) PutLL(id, minHoles[i]); Send(id); } } // --------------------------------------------------- // int firstD = getFirstD(myid); int lastD = getFirstD(myid + 1); ll maxDisc = 0; for (int d = firstD; d < lastD; d++) maxDisc = max(maxDisc, DiscDiameter(d)); int holesSegment = numOfNodes; for (int id = 0; id < numOfNodes; id++) if (minHoles[id] < maxDisc) { holesSegment = id; break; } int minBase = H - (D - firstD); if (holesSegment < numOfNodes) { int lastH = min(H+1, getFirstH(holesSegment + 1) + lastD - firstD + 1); int firstH = max(1, getFirstH(holesSegment) - 1); ll minHole = numeric_limits<ll>::max(); vector<ll> holes(lastH - firstH); for (int h = firstH; h < lastH; h++) { minHole = min(minHole, HoleDiameter(h)); holes[h - firstH] = minHole; } int h = lastH - 1; for (int d = firstD; d < lastD; d++) { ll disc = DiscDiameter(d); while (h >= firstH && holes[h - firstH] < disc) h--; minBase = min(minBase, h - (D - d)); } } // --------------------------------------------------- // if (myid > 0) { PutInt(0, minBase); Send(0); } else { // cout << 0 << ": " << minBase << "\n"; for (int i = 1; i < numOfNodes; i++) { int id = Receive(-1); int mb = GetInt(id); minBase = min(minBase, mb); // cout << id << ": " << mb << "\n"; } cout << max(0, minBase) << "\n"; } return 0; } |