#include <cstdlib> #include <iostream> #include <stdio.h> #include <map> #include <vector> #include <climits> using namespace std; #include "krazki.h" #include "message.h" void worker() { int pipe_start; int pipe_end; vector<long long> pipe_mins; long long pipe_min = LLONG_MAX; int first_empty; int p = PipeHeight(); while(true) { Receive(0); char operation = GetChar(0); if (operation =='a') { pipe_start = GetInt(0); pipe_end = GetInt(0); first_empty = pipe_end; // printf("ps %d %d\n", pipe_start, pipe_end); for(int i = pipe_start+1; i<= pipe_end ; i++){ long long diameter = HoleDiameter(i); pipe_min = min(pipe_min,diameter); pipe_mins.push_back(pipe_min); } PutLL(0, pipe_min); Send(0); } else if (operation =='b') { int ds_start = GetInt(0); int ds_end = GetInt(0); long long ds_max = -1; // printf("ds %d %d\n", ds_start, ds_end); for(int i = ds_start; i< ds_end; i++) { long long diameter = DiscDiameter(i); ds_max = max(ds_max,diameter); } PutLL(0, ds_max); Send(0); } else if (operation =='c') { int ds_start = GetInt(0); int ds_end = GetInt(0); // printf("pipe %d %d %d\n", pipe_start, pipe_end,first_empty); for(int i = ds_start; i< ds_end; i++) { long long diameter = DiscDiameter(i); // printf("disk1 %d %d %d\n",i, diameter, first_empty); while(first_empty > pipe_start && pipe_mins[first_empty-pipe_start-1] < diameter) { // printf("mins %d %d %d\n", first_empty, first_empty-pipe_start,pipe_mins[first_empty-pipe_start-1]); first_empty--; } // printf("mins %d %d %d\n", first_empty, first_empty-pipe_start,pipe_mins[first_empty-pipe_start-1]); if(first_empty != pipe_end || pipe_end == p) { // printf("dec firstempry\n"); first_empty--; } } // printf("first empty %d\n",first_empty); PutInt(0, first_empty); PutInt(0, pipe_end); PutInt(0, first_empty + (ds_end-ds_start)); Send(0); // printf("thread sent c %d\n", MyNodeId()); } else{ break; } } } struct PipeSection{ int start; int end; long long min; int task_id; }; struct DiscSet{ int start; int end; long long max; int pipe_start; int pipe_section_end; int pipe_end; int task_id; int task_id2; }; void master() { int n = NumberOfNodes(); int p = PipeHeight(); int d = NumberOfDiscs(); vector<PipeSection> pipe; vector<DiscSet> discs; int start = 0; int task_id = 1; // printf("a0"); while(start < p){ PipeSection ps; ps.start = start; if(task_id+1<n){ ps.end = ps.start + max(p/(n-1),1); } else { ps.end = p; } ps.task_id = task_id; pipe.push_back(ps); start = ps.end; task_id++; } start = 1; task_id = 1; // printf("a01"); while(start <=d){ DiscSet ds; ds.start = start; if(task_id+1<n){ ds.end = ds.start + max(d/(n-1),1); } else { ds.end = d+1; } ds.task_id = task_id; start = ds.end; discs.push_back(ds); task_id++; } // printf("a1"); for (const auto& ps : pipe) { // printf("node %d\n", ps.task_id); PutChar(ps.task_id, 'a'); PutInt(ps.task_id,ps.start); PutInt(ps.task_id,ps.end); Send(ps.task_id); } // printf("a2"); for (auto& ps : pipe) { Receive(ps.task_id); ps.min = GetLL(ps.task_id); // printf("min %lld\n", ps.min); } // printf("a3"); for (const auto& ds : discs) { PutChar(ds.task_id, 'b'); PutInt(ds.task_id,ds.start); PutInt(ds.task_id,ds.end); Send(ds.task_id); } // printf("a4"); for (auto& ds : discs) { Receive(ds.task_id); ds.max = GetLL(ds.task_id); // printf("max %lld\n ", ds.max); } // printf("a5"); int prev = pipe.size()-1; for(int i = 0; i<discs.size(); i++){ int j; for(j=0; j<pipe.size(); j++){ if(j == prev || pipe[j].min<discs[i].max){ PutChar(pipe[j].task_id, 'c'); PutInt(pipe[j].task_id,discs[i].start); PutInt(pipe[j].task_id,discs[i].end); Send(pipe[j].task_id); discs[i].task_id = pipe[j].task_id; break; } } } // printf("a6"); for (auto& ds : discs) { Receive(ds.task_id); ds.pipe_start = GetInt(ds.task_id); ds.pipe_section_end = GetInt(ds.task_id); ds.pipe_end = GetInt(ds.task_id); // printf("discs %d %d %d\n",ds.pipe_start, ds.pipe_section_end,ds.pipe_end); ds.task_id2 = -1; } // printf("a7"); for(int i = 1; i<discs.size();i++){ if(discs[i].pipe_end>discs[i].pipe_section_end) { if(discs[i-1].pipe_start>discs[i].pipe_section_end){ discs[i].task_id2 = discs[i-1].task_id; PutChar(discs[i].task_id2, 'c'); PutInt(discs[i].task_id2,discs[i].end - (discs[i].pipe_end-discs[i].pipe_section_end)); PutInt(discs[i].task_id2,discs[i].end); Send(discs[i].task_id2); // printf("send to task %d\n", discs[i].task_id2); } } } // printf("a8"); for (auto& ds : discs) { if(ds.task_id2 != -1){ // printf("recieve to task %d\n", ds.task_id2); Receive(ds.task_id2); // printf("a81"); int pipe_start = GetInt(ds.task_id2); // printf("a82"); int pipe_section_end = GetInt(ds.task_id2); // printf("a83"); int pipe_end = GetInt(ds.task_id2); // printf("a84"); ds.pipe_end = min(ds.pipe_end, pipe_end); ds.pipe_start = ds.pipe_end - (ds.end-ds.start); } } // printf("a9"); for(int i = 1; i<discs.size();i++){ if(discs[i-1].pipe_start<discs[i].pipe_end) { discs[i].pipe_end = discs[i-1].pipe_start; discs[i].pipe_start = discs[i].pipe_end-(discs[i].end-discs[i].start); } } for(int i = 1; i<n;i++){ PutChar(i, 'd'); Send(i); } // printf("a10\n"); if(discs[discs.size()-1].pipe_start<0) { printf("0\n"); } else { printf("%d\n", discs[discs.size()-1].pipe_start+1); } } int main() { if (MyNodeId() != 0) { worker(); } else { master(); } return EXIT_SUCCESS; }
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | #include <cstdlib> #include <iostream> #include <stdio.h> #include <map> #include <vector> #include <climits> using namespace std; #include "krazki.h" #include "message.h" void worker() { int pipe_start; int pipe_end; vector<long long> pipe_mins; long long pipe_min = LLONG_MAX; int first_empty; int p = PipeHeight(); while(true) { Receive(0); char operation = GetChar(0); if (operation =='a') { pipe_start = GetInt(0); pipe_end = GetInt(0); first_empty = pipe_end; // printf("ps %d %d\n", pipe_start, pipe_end); for(int i = pipe_start+1; i<= pipe_end ; i++){ long long diameter = HoleDiameter(i); pipe_min = min(pipe_min,diameter); pipe_mins.push_back(pipe_min); } PutLL(0, pipe_min); Send(0); } else if (operation =='b') { int ds_start = GetInt(0); int ds_end = GetInt(0); long long ds_max = -1; // printf("ds %d %d\n", ds_start, ds_end); for(int i = ds_start; i< ds_end; i++) { long long diameter = DiscDiameter(i); ds_max = max(ds_max,diameter); } PutLL(0, ds_max); Send(0); } else if (operation =='c') { int ds_start = GetInt(0); int ds_end = GetInt(0); // printf("pipe %d %d %d\n", pipe_start, pipe_end,first_empty); for(int i = ds_start; i< ds_end; i++) { long long diameter = DiscDiameter(i); // printf("disk1 %d %d %d\n",i, diameter, first_empty); while(first_empty > pipe_start && pipe_mins[first_empty-pipe_start-1] < diameter) { // printf("mins %d %d %d\n", first_empty, first_empty-pipe_start,pipe_mins[first_empty-pipe_start-1]); first_empty--; } // printf("mins %d %d %d\n", first_empty, first_empty-pipe_start,pipe_mins[first_empty-pipe_start-1]); if(first_empty != pipe_end || pipe_end == p) { // printf("dec firstempry\n"); first_empty--; } } // printf("first empty %d\n",first_empty); PutInt(0, first_empty); PutInt(0, pipe_end); PutInt(0, first_empty + (ds_end-ds_start)); Send(0); // printf("thread sent c %d\n", MyNodeId()); } else{ break; } } } struct PipeSection{ int start; int end; long long min; int task_id; }; struct DiscSet{ int start; int end; long long max; int pipe_start; int pipe_section_end; int pipe_end; int task_id; int task_id2; }; void master() { int n = NumberOfNodes(); int p = PipeHeight(); int d = NumberOfDiscs(); vector<PipeSection> pipe; vector<DiscSet> discs; int start = 0; int task_id = 1; // printf("a0"); while(start < p){ PipeSection ps; ps.start = start; if(task_id+1<n){ ps.end = ps.start + max(p/(n-1),1); } else { ps.end = p; } ps.task_id = task_id; pipe.push_back(ps); start = ps.end; task_id++; } start = 1; task_id = 1; // printf("a01"); while(start <=d){ DiscSet ds; ds.start = start; if(task_id+1<n){ ds.end = ds.start + max(d/(n-1),1); } else { ds.end = d+1; } ds.task_id = task_id; start = ds.end; discs.push_back(ds); task_id++; } // printf("a1"); for (const auto& ps : pipe) { // printf("node %d\n", ps.task_id); PutChar(ps.task_id, 'a'); PutInt(ps.task_id,ps.start); PutInt(ps.task_id,ps.end); Send(ps.task_id); } // printf("a2"); for (auto& ps : pipe) { Receive(ps.task_id); ps.min = GetLL(ps.task_id); // printf("min %lld\n", ps.min); } // printf("a3"); for (const auto& ds : discs) { PutChar(ds.task_id, 'b'); PutInt(ds.task_id,ds.start); PutInt(ds.task_id,ds.end); Send(ds.task_id); } // printf("a4"); for (auto& ds : discs) { Receive(ds.task_id); ds.max = GetLL(ds.task_id); // printf("max %lld\n ", ds.max); } // printf("a5"); int prev = pipe.size()-1; for(int i = 0; i<discs.size(); i++){ int j; for(j=0; j<pipe.size(); j++){ if(j == prev || pipe[j].min<discs[i].max){ PutChar(pipe[j].task_id, 'c'); PutInt(pipe[j].task_id,discs[i].start); PutInt(pipe[j].task_id,discs[i].end); Send(pipe[j].task_id); discs[i].task_id = pipe[j].task_id; break; } } } // printf("a6"); for (auto& ds : discs) { Receive(ds.task_id); ds.pipe_start = GetInt(ds.task_id); ds.pipe_section_end = GetInt(ds.task_id); ds.pipe_end = GetInt(ds.task_id); // printf("discs %d %d %d\n",ds.pipe_start, ds.pipe_section_end,ds.pipe_end); ds.task_id2 = -1; } // printf("a7"); for(int i = 1; i<discs.size();i++){ if(discs[i].pipe_end>discs[i].pipe_section_end) { if(discs[i-1].pipe_start>discs[i].pipe_section_end){ discs[i].task_id2 = discs[i-1].task_id; PutChar(discs[i].task_id2, 'c'); PutInt(discs[i].task_id2,discs[i].end - (discs[i].pipe_end-discs[i].pipe_section_end)); PutInt(discs[i].task_id2,discs[i].end); Send(discs[i].task_id2); // printf("send to task %d\n", discs[i].task_id2); } } } // printf("a8"); for (auto& ds : discs) { if(ds.task_id2 != -1){ // printf("recieve to task %d\n", ds.task_id2); Receive(ds.task_id2); // printf("a81"); int pipe_start = GetInt(ds.task_id2); // printf("a82"); int pipe_section_end = GetInt(ds.task_id2); // printf("a83"); int pipe_end = GetInt(ds.task_id2); // printf("a84"); ds.pipe_end = min(ds.pipe_end, pipe_end); ds.pipe_start = ds.pipe_end - (ds.end-ds.start); } } // printf("a9"); for(int i = 1; i<discs.size();i++){ if(discs[i-1].pipe_start<discs[i].pipe_end) { discs[i].pipe_end = discs[i-1].pipe_start; discs[i].pipe_start = discs[i].pipe_end-(discs[i].end-discs[i].start); } } for(int i = 1; i<n;i++){ PutChar(i, 'd'); Send(i); } // printf("a10\n"); if(discs[discs.size()-1].pipe_start<0) { printf("0\n"); } else { printf("%d\n", discs[discs.size()-1].pipe_start+1); } } int main() { if (MyNodeId() != 0) { worker(); } else { master(); } return EXIT_SUCCESS; } |