#include <iostream> #include "krazki.h" #include "message.h" #include <vector> #include <algorithm> #include <cassert> #define ll long long using namespace std; int my_id; int nodes; int pipe_len; int num_discs; vector<int> pipe_for_node; int my_pipe_start; int my_pipe_end; vector<ll> pipe; vector<ll> mx_rad; int BATCH = 10; int current_depth; void init() { my_id = MyNodeId(); nodes = NumberOfNodes(); pipe_len = PipeHeight(); num_discs = NumberOfDiscs(); BATCH = max(BATCH, pipe_len/nodes/300); } void read_pipe() { for(ll i = my_pipe_start;i<my_pipe_end;i++ ){ pipe.push_back(HoleDiameter(i+1)); } mx_rad.push_back(pipe[0]); for(int i=1;i<pipe.size();i++) { mx_rad.push_back(min(mx_rad[i-1], pipe[i])); } if(my_id>0) { Receive(my_id - 1); ll in_rad = GetLL(my_id - 1); for(int i=0;i<mx_rad.size();i++) { mx_rad[i]=min(mx_rad[i],in_rad); } } if (my_id + 1 < nodes) { PutLL(my_id + 1, mx_rad.back()); Send(my_id+1); } } void insert(ll disc) { current_depth--; while(current_depth>=0 && disc>mx_rad[current_depth])current_depth--; } void receive_prev_status(char &stat, int &pos, ll &val) { int prev_id = my_id+1; if(prev_id==nodes)throw "WRONG node id"; //cout << my_id << " waiting for status\n"; Receive(prev_id); stat = GetChar(prev_id); if(stat == 'x') { pos = (int)GetLL(prev_id); val = GetLL(prev_id); } } void send_next_status(char status) { if(my_id == 0)throw "WRONG next node id"; //cout << my_id <<": sending status "<<status<<endl; PutChar(my_id - 1, status); Send(my_id - 1); } void solve(int my_disc_start, int my_disc_end, bool prev_finished, vector<ll> val) { ll unmatching = -1; ll unmatching_val; current_depth = pipe.size(); int v_id=0; int batch_read =0; for(ll i = my_disc_start; i<my_disc_end;i++ ) { ll d; if(val[v_id] ==-1){ d= DiscDiameter(i+1); batch_read ++; } else { d = val[v_id]; // cout <<"reusing value\n"; } val[v_id++]=d; insert(d); if(current_depth <0) { unmatching = i; unmatching_val = d; break; } if( batch_read == BATCH) { batch_read =0; if(my_id >0){ send_next_status('o'); } if (!prev_finished) { char status; int pos;ll v; receive_prev_status(status,pos,v); if(status == 'o') { // cout << my_id << ": Got status ok\n"; } else if(status =='f') { prev_finished = true; // cout << my_id<<": Previous is done"; } else if(status == 'x') { // cout << my_id <<": Got new start point "<<pos<<" with val "<<val<<endl; int added = my_disc_start - pos; added = min(added, my_pipe_end - my_pipe_start); assert(added>0); vector<ll>add = vector<ll>(added,-1); val.insert(val.begin(), add.begin(),add.end()); my_disc_start = pos; return solve(my_disc_start, my_disc_end, true, val); } } } } while(!prev_finished) { char status; int pos;ll v; receive_prev_status(status,pos,v); if(status=='f') prev_finished = true; if(status =='x') { //cout << my_id <<": Got new start point "<<pos<<" with val "<<val<<endl; int added = my_disc_start - pos; added = min(added, my_pipe_end - my_pipe_start); assert(added>0); vector<ll>add = vector<ll>(added,-1); val.insert(val.begin(), add.begin(),add.end()); my_disc_start = pos; return solve(my_disc_start, my_disc_end, true,val); } } if(my_id == 0) { } else { if(current_depth >=0) { send_next_status('f'); } else { //cout << my_id <<": sending x status "<<endl; PutChar(my_id-1,'x'); PutLL(my_id-1,unmatching); PutLL(my_id-1,unmatching_val); Send(my_id-1); } } //cout << my_id <<" finished ["<<my_disc_start<<","<<my_disc_end<<"] depth "<<current_depth<<endl; if(my_disc_end==num_discs && my_disc_start < my_disc_end) { if(current_depth <0) { if(my_id == 0){ //cout <<my_id<<" RESULT 0\n"; cout <<"0\n"; } } else { ll result = current_depth; for(int i=my_id-1;i>=0;i--)result += pipe_for_node[i]; //cout <<my_id<<" RESULT "<<result + 1<<endl; cout <<result + 1<<endl; } } } void solve() { read_pipe(); int my_disc_start; int my_disc_end; int p=0; for(int node_id = nodes-1;node_id >=0;node_id--) { int end = min(num_discs, p + pipe_for_node[node_id]); if(node_id == my_id){ my_disc_start = p;my_disc_end=end;} p = end; } bool prev_finished = my_id == nodes - 1; vector<ll>val = vector<ll>(my_disc_end - my_disc_start, -1); solve(my_disc_start,my_disc_end,prev_finished,val); } int main() { init(); if(num_discs > pipe_len) { if(my_id==0) { cout << "0\n"; } return 0; } if(nodes > pipe_len) { nodes = pipe_len; if(my_id >=nodes)return 0; } int per_node = pipe_len/nodes; int rest = pipe_len%nodes; int start =0; for(int i=0;i<nodes;i++) { int end = start + per_node; if(i<rest)end++; pipe_for_node.push_back(end-start); if(i==my_id){ my_pipe_start = start; my_pipe_end = end; } start = end; } solve(); //cout << my_id<<": "<<disc_calls<<" "<<hole_calls<<"messages "<<messages_sent<<endl; 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | #include <iostream> #include "krazki.h" #include "message.h" #include <vector> #include <algorithm> #include <cassert> #define ll long long using namespace std; int my_id; int nodes; int pipe_len; int num_discs; vector<int> pipe_for_node; int my_pipe_start; int my_pipe_end; vector<ll> pipe; vector<ll> mx_rad; int BATCH = 10; int current_depth; void init() { my_id = MyNodeId(); nodes = NumberOfNodes(); pipe_len = PipeHeight(); num_discs = NumberOfDiscs(); BATCH = max(BATCH, pipe_len/nodes/300); } void read_pipe() { for(ll i = my_pipe_start;i<my_pipe_end;i++ ){ pipe.push_back(HoleDiameter(i+1)); } mx_rad.push_back(pipe[0]); for(int i=1;i<pipe.size();i++) { mx_rad.push_back(min(mx_rad[i-1], pipe[i])); } if(my_id>0) { Receive(my_id - 1); ll in_rad = GetLL(my_id - 1); for(int i=0;i<mx_rad.size();i++) { mx_rad[i]=min(mx_rad[i],in_rad); } } if (my_id + 1 < nodes) { PutLL(my_id + 1, mx_rad.back()); Send(my_id+1); } } void insert(ll disc) { current_depth--; while(current_depth>=0 && disc>mx_rad[current_depth])current_depth--; } void receive_prev_status(char &stat, int &pos, ll &val) { int prev_id = my_id+1; if(prev_id==nodes)throw "WRONG node id"; //cout << my_id << " waiting for status\n"; Receive(prev_id); stat = GetChar(prev_id); if(stat == 'x') { pos = (int)GetLL(prev_id); val = GetLL(prev_id); } } void send_next_status(char status) { if(my_id == 0)throw "WRONG next node id"; //cout << my_id <<": sending status "<<status<<endl; PutChar(my_id - 1, status); Send(my_id - 1); } void solve(int my_disc_start, int my_disc_end, bool prev_finished, vector<ll> val) { ll unmatching = -1; ll unmatching_val; current_depth = pipe.size(); int v_id=0; int batch_read =0; for(ll i = my_disc_start; i<my_disc_end;i++ ) { ll d; if(val[v_id] ==-1){ d= DiscDiameter(i+1); batch_read ++; } else { d = val[v_id]; // cout <<"reusing value\n"; } val[v_id++]=d; insert(d); if(current_depth <0) { unmatching = i; unmatching_val = d; break; } if( batch_read == BATCH) { batch_read =0; if(my_id >0){ send_next_status('o'); } if (!prev_finished) { char status; int pos;ll v; receive_prev_status(status,pos,v); if(status == 'o') { // cout << my_id << ": Got status ok\n"; } else if(status =='f') { prev_finished = true; // cout << my_id<<": Previous is done"; } else if(status == 'x') { // cout << my_id <<": Got new start point "<<pos<<" with val "<<val<<endl; int added = my_disc_start - pos; added = min(added, my_pipe_end - my_pipe_start); assert(added>0); vector<ll>add = vector<ll>(added,-1); val.insert(val.begin(), add.begin(),add.end()); my_disc_start = pos; return solve(my_disc_start, my_disc_end, true, val); } } } } while(!prev_finished) { char status; int pos;ll v; receive_prev_status(status,pos,v); if(status=='f') prev_finished = true; if(status =='x') { //cout << my_id <<": Got new start point "<<pos<<" with val "<<val<<endl; int added = my_disc_start - pos; added = min(added, my_pipe_end - my_pipe_start); assert(added>0); vector<ll>add = vector<ll>(added,-1); val.insert(val.begin(), add.begin(),add.end()); my_disc_start = pos; return solve(my_disc_start, my_disc_end, true,val); } } if(my_id == 0) { } else { if(current_depth >=0) { send_next_status('f'); } else { //cout << my_id <<": sending x status "<<endl; PutChar(my_id-1,'x'); PutLL(my_id-1,unmatching); PutLL(my_id-1,unmatching_val); Send(my_id-1); } } //cout << my_id <<" finished ["<<my_disc_start<<","<<my_disc_end<<"] depth "<<current_depth<<endl; if(my_disc_end==num_discs && my_disc_start < my_disc_end) { if(current_depth <0) { if(my_id == 0){ //cout <<my_id<<" RESULT 0\n"; cout <<"0\n"; } } else { ll result = current_depth; for(int i=my_id-1;i>=0;i--)result += pipe_for_node[i]; //cout <<my_id<<" RESULT "<<result + 1<<endl; cout <<result + 1<<endl; } } } void solve() { read_pipe(); int my_disc_start; int my_disc_end; int p=0; for(int node_id = nodes-1;node_id >=0;node_id--) { int end = min(num_discs, p + pipe_for_node[node_id]); if(node_id == my_id){ my_disc_start = p;my_disc_end=end;} p = end; } bool prev_finished = my_id == nodes - 1; vector<ll>val = vector<ll>(my_disc_end - my_disc_start, -1); solve(my_disc_start,my_disc_end,prev_finished,val); } int main() { init(); if(num_discs > pipe_len) { if(my_id==0) { cout << "0\n"; } return 0; } if(nodes > pipe_len) { nodes = pipe_len; if(my_id >=nodes)return 0; } int per_node = pipe_len/nodes; int rest = pipe_len%nodes; int start =0; for(int i=0;i<nodes;i++) { int end = start + per_node; if(i<rest)end++; pipe_for_node.push_back(end-start); if(i==my_id){ my_pipe_start = start; my_pipe_end = end; } start = end; } solve(); //cout << my_id<<": "<<disc_calls<<" "<<hole_calls<<"messages "<<messages_sent<<endl; return 0; } |