#include "message.h" #include "krazki.h" #include <vector> #include <cstdio> using namespace std; #define LL long long #define INF 3000000000000000000LL int n, m; int numberOfNodes; int myNodeId; int first, last; LL mi = INF; LL ma = INF; vector<LL> data; #define MEMORIZE_EXP (24) #define MEMORIZE_FROM (29 - MEMORIZE_EXP) #define MEMORIZE_OFFSET (mem.size() / 2) int testCounter = 0; int memStart = -1; vector<LL> mem; int spreader = 0; inline LL diameter(int i){ if(testCounter >= MEMORIZE_FROM){ int a = i + MEMORIZE_OFFSET - memStart; if(a >= 0 && a < int(mem.size())){ if(mem[a] != 0)return mem[a]; mem[a] = DiscDiameter(i); return mem[a]; } } return DiscDiameter(i); } bool test(int s){ ++testCounter; if(testCounter == MEMORIZE_FROM){ mem.resize(data.size() + (1 << MEMORIZE_EXP), 0); memStart = m + s - (first + last) / 2; } for(int a=max(first, s); a<last; ++a){ if(a-s >= m)break; if(diameter(m - a + s) > data[a - first]){ return 0; } } return 1; } bool testServer(int s){ if(spreader == 0){ for(int a=1; a<numberOfNodes; ++a){ PutInt(a, s); Send(a); } }else{ PutInt(spreader, s); Send(spreader); } spreader = (spreader + 1) % numberOfNodes; bool b = test(s); for(int a=1; a<numberOfNodes; ++a){ Receive(a); char c = GetChar(a); b &= c; } return b; } int main(){ n = PipeHeight(); m = NumberOfDiscs(); numberOfNodes = NumberOfNodes(); myNodeId = MyNodeId(); first = (((LL)myNodeId) * n) / numberOfNodes; last = (((LL)myNodeId + 1) * n) / numberOfNodes; data.resize(last - first); for(int a=first; a<last; ++a){ LL r = HoleDiameter(a + 1); mi = min(mi, r); data[a - first] = mi; } if(myNodeId != 0){ PutLL(0, mi); Send(0); Receive(0); ma = GetLL(0); }else{ LL q = mi; for(int a=1; a<numberOfNodes; ++a){ PutLL(a, q); Send(a); Receive(a); q = min(q, GetLL(a)); } } for(int a=first; a<last; ++a){ if(data[a - first] > ma){ data[a - first] = ma; } } if(myNodeId != 0){ while(1){ int i = 0; if(myNodeId != spreader){ Receive(spreader); i = GetInt(spreader); }else{ Receive(0); i = GetInt(0); for(int a=1; a<numberOfNodes; ++a){ if(a != myNodeId){ PutInt(a, i); Send(a); } } } if(i == -1)break; PutChar(0, test(i)); Send(0); spreader = (spreader + 1) % numberOfNodes; } }else{ int best = -1; int left = 0; int right = n-m; while(left <= right){ int s = (left + right) / 2; if(testServer(s)){ best = s; left = s + 1; }else{ right = s - 1; } } if(spreader == 0){ for(int a=1; a<numberOfNodes; ++a){ PutInt(a, -1); Send(a); } }else{ PutInt(spreader, -1); Send(spreader); } spreader = (spreader + 1) % numberOfNodes; printf("%d", best + 1); } 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 | #include "message.h" #include "krazki.h" #include <vector> #include <cstdio> using namespace std; #define LL long long #define INF 3000000000000000000LL int n, m; int numberOfNodes; int myNodeId; int first, last; LL mi = INF; LL ma = INF; vector<LL> data; #define MEMORIZE_EXP (24) #define MEMORIZE_FROM (29 - MEMORIZE_EXP) #define MEMORIZE_OFFSET (mem.size() / 2) int testCounter = 0; int memStart = -1; vector<LL> mem; int spreader = 0; inline LL diameter(int i){ if(testCounter >= MEMORIZE_FROM){ int a = i + MEMORIZE_OFFSET - memStart; if(a >= 0 && a < int(mem.size())){ if(mem[a] != 0)return mem[a]; mem[a] = DiscDiameter(i); return mem[a]; } } return DiscDiameter(i); } bool test(int s){ ++testCounter; if(testCounter == MEMORIZE_FROM){ mem.resize(data.size() + (1 << MEMORIZE_EXP), 0); memStart = m + s - (first + last) / 2; } for(int a=max(first, s); a<last; ++a){ if(a-s >= m)break; if(diameter(m - a + s) > data[a - first]){ return 0; } } return 1; } bool testServer(int s){ if(spreader == 0){ for(int a=1; a<numberOfNodes; ++a){ PutInt(a, s); Send(a); } }else{ PutInt(spreader, s); Send(spreader); } spreader = (spreader + 1) % numberOfNodes; bool b = test(s); for(int a=1; a<numberOfNodes; ++a){ Receive(a); char c = GetChar(a); b &= c; } return b; } int main(){ n = PipeHeight(); m = NumberOfDiscs(); numberOfNodes = NumberOfNodes(); myNodeId = MyNodeId(); first = (((LL)myNodeId) * n) / numberOfNodes; last = (((LL)myNodeId + 1) * n) / numberOfNodes; data.resize(last - first); for(int a=first; a<last; ++a){ LL r = HoleDiameter(a + 1); mi = min(mi, r); data[a - first] = mi; } if(myNodeId != 0){ PutLL(0, mi); Send(0); Receive(0); ma = GetLL(0); }else{ LL q = mi; for(int a=1; a<numberOfNodes; ++a){ PutLL(a, q); Send(a); Receive(a); q = min(q, GetLL(a)); } } for(int a=first; a<last; ++a){ if(data[a - first] > ma){ data[a - first] = ma; } } if(myNodeId != 0){ while(1){ int i = 0; if(myNodeId != spreader){ Receive(spreader); i = GetInt(spreader); }else{ Receive(0); i = GetInt(0); for(int a=1; a<numberOfNodes; ++a){ if(a != myNodeId){ PutInt(a, i); Send(a); } } } if(i == -1)break; PutChar(0, test(i)); Send(0); spreader = (spreader + 1) % numberOfNodes; } }else{ int best = -1; int left = 0; int right = n-m; while(left <= right){ int s = (left + right) / 2; if(testServer(s)){ best = s; left = s + 1; }else{ right = s - 1; } } if(spreader == 0){ for(int a=1; a<numberOfNodes; ++a){ PutInt(a, -1); Send(a); } }else{ PutInt(spreader, -1); Send(spreader); } spreader = (spreader + 1) % numberOfNodes; printf("%d", best + 1); } return 0; } |