#include <stdio.h> #include <algorithm> #define MAX_HOLES 50000000 #include "krazki.h" #include "message.h" using namespace std; using HOLE = int; int findHeight(HOLE* pipe, const int& size, const long long int& minDiameter, const long long int discDiameter, const int maxHeight) { if (maxHeight >= pipe[0] || discDiameter <= minDiameter) return maxHeight+1; auto it = upper_bound(pipe, pipe + size, -1, [&discDiameter](HOLE d1, HOLE d2) { auto getDiameter = [&discDiameter](const HOLE& hole) -> long long int { if (hole == 0) return 0; else if (hole == -1) return discDiameter; else return HoleDiameter(PipeHeight() - hole + 1); }; return getDiameter(d1) > getDiameter(d2); }); return max(maxHeight+1, *it+1); } int insertToPipe(HOLE* pipe, const int& size, const long long int& minDiameter, int insertFromDisc, int insertToDisc) { int lastHeight = findHeight(pipe, size, minDiameter, DiscDiameter(insertFromDisc), 0); for (int disc = insertFromDisc+1; disc<= insertToDisc; ++disc) lastHeight = findHeight(pipe, size, minDiameter, DiscDiameter(disc), lastHeight); return lastHeight; } void buildPipe(HOLE* pipe, const int from, const int to, int& size, long long int& minDiameter) { long long int lastDiameter = HoleDiameter(from); long long int diameter; size = 0; pipe[size++] = to; for (int i=from+1; i<=to; i++) { diameter = HoleDiameter(i); if (diameter < lastDiameter) { lastDiameter = diameter; pipe[size++] = to - i + 1; } } minDiameter = lastDiameter; pipe[size++] = 0; } int getHeight(const int fromDisc, const int toDisc) { HOLE* pipe = new HOLE[MAX_HOLES + 5]; int size; int maxHeight = 0; long long int minDiameter; for (int i=1; i<=PipeHeight(); i+=MAX_HOLES) { buildPipe(pipe, i, min(i + MAX_HOLES - 1, PipeHeight()), size, minDiameter); const int height = insertToPipe(pipe, size, minDiameter, fromDisc, toDisc); maxHeight = max(maxHeight, height); } delete [] pipe; return maxHeight; } void doNodeWork() { Receive(0); const int fromDisc = GetInt(0); Receive(0); const int toDisc = GetInt(0); if (fromDisc > NumberOfDiscs()) { PutInt(0, 0); Send(0); PutInt(0, 0); Send(0); return ; } PutInt(0, min(NumberOfDiscs(), toDisc) - fromDisc + 1); Send(0); PutInt(0, getHeight(fromDisc, min(NumberOfDiscs(), toDisc))); Send(0); } void sendWork() { const int nodes = NumberOfNodes() - 1; const int discPerNode = NumberOfDiscs() / nodes + ((NumberOfDiscs() % nodes) == 0 ? 0 : 1); for (int i=1; i<NumberOfNodes(); ++i) { const int fromDisc = 1 + (i -1) * discPerNode; const int toDisc = 1 + i * discPerNode - 1; PutInt(i, fromDisc); Send(i); PutInt(i, toDisc); Send(i); } } void receiveWork() { int maxHeight = 0; for (int i=1; i<NumberOfNodes(); ++i) { Receive(i); const int discs = GetInt(i); Receive(i); const int height = GetInt(i); if (discs <= 0) continue; maxHeight = max(height, maxHeight + discs); } printf("%d\n", max(0, PipeHeight() - maxHeight + 1)); } int main() { if (MyNodeId() != 0) doNodeWork(); else { sendWork(); receiveWork(); } 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 | #include <stdio.h> #include <algorithm> #define MAX_HOLES 50000000 #include "krazki.h" #include "message.h" using namespace std; using HOLE = int; int findHeight(HOLE* pipe, const int& size, const long long int& minDiameter, const long long int discDiameter, const int maxHeight) { if (maxHeight >= pipe[0] || discDiameter <= minDiameter) return maxHeight+1; auto it = upper_bound(pipe, pipe + size, -1, [&discDiameter](HOLE d1, HOLE d2) { auto getDiameter = [&discDiameter](const HOLE& hole) -> long long int { if (hole == 0) return 0; else if (hole == -1) return discDiameter; else return HoleDiameter(PipeHeight() - hole + 1); }; return getDiameter(d1) > getDiameter(d2); }); return max(maxHeight+1, *it+1); } int insertToPipe(HOLE* pipe, const int& size, const long long int& minDiameter, int insertFromDisc, int insertToDisc) { int lastHeight = findHeight(pipe, size, minDiameter, DiscDiameter(insertFromDisc), 0); for (int disc = insertFromDisc+1; disc<= insertToDisc; ++disc) lastHeight = findHeight(pipe, size, minDiameter, DiscDiameter(disc), lastHeight); return lastHeight; } void buildPipe(HOLE* pipe, const int from, const int to, int& size, long long int& minDiameter) { long long int lastDiameter = HoleDiameter(from); long long int diameter; size = 0; pipe[size++] = to; for (int i=from+1; i<=to; i++) { diameter = HoleDiameter(i); if (diameter < lastDiameter) { lastDiameter = diameter; pipe[size++] = to - i + 1; } } minDiameter = lastDiameter; pipe[size++] = 0; } int getHeight(const int fromDisc, const int toDisc) { HOLE* pipe = new HOLE[MAX_HOLES + 5]; int size; int maxHeight = 0; long long int minDiameter; for (int i=1; i<=PipeHeight(); i+=MAX_HOLES) { buildPipe(pipe, i, min(i + MAX_HOLES - 1, PipeHeight()), size, minDiameter); const int height = insertToPipe(pipe, size, minDiameter, fromDisc, toDisc); maxHeight = max(maxHeight, height); } delete [] pipe; return maxHeight; } void doNodeWork() { Receive(0); const int fromDisc = GetInt(0); Receive(0); const int toDisc = GetInt(0); if (fromDisc > NumberOfDiscs()) { PutInt(0, 0); Send(0); PutInt(0, 0); Send(0); return ; } PutInt(0, min(NumberOfDiscs(), toDisc) - fromDisc + 1); Send(0); PutInt(0, getHeight(fromDisc, min(NumberOfDiscs(), toDisc))); Send(0); } void sendWork() { const int nodes = NumberOfNodes() - 1; const int discPerNode = NumberOfDiscs() / nodes + ((NumberOfDiscs() % nodes) == 0 ? 0 : 1); for (int i=1; i<NumberOfNodes(); ++i) { const int fromDisc = 1 + (i -1) * discPerNode; const int toDisc = 1 + i * discPerNode - 1; PutInt(i, fromDisc); Send(i); PutInt(i, toDisc); Send(i); } } void receiveWork() { int maxHeight = 0; for (int i=1; i<NumberOfNodes(); ++i) { Receive(i); const int discs = GetInt(i); Receive(i); const int height = GetInt(i); if (discs <= 0) continue; maxHeight = max(height, maxHeight + discs); } printf("%d\n", max(0, PipeHeight() - maxHeight + 1)); } int main() { if (MyNodeId() != 0) doNodeWork(); else { sendWork(); receiveWork(); } return 0; } |