#include <cstdlib> #include <iostream> #include <bits/stdc++.h> using namespace std; #include "krazki.h" #include "message.h" pair<int, int> prz[100]; pair<int, int> przn[100]; long long tab[2000001]; int main() { int id = MyNodeId(); int x=NumberOfNodes(); int n, m; n=PipeHeight(); m=NumberOfDiscs(); int mp=m; int f=1; for(int i=0;i<x;i++){ int tmp=mp/(x-i); mp-=tmp+1; prz[i]=make_pair(f,f+tmp); f+=tmp+1; if(f==m) break; } int np=n; f=1; for(int i=0;i<x;i++){ int tmp=np/(x-i); np-=tmp+1; przn[i]=make_pair(f,f+tmp); f+=tmp+1; if(f==m) break; } int zero=prz[id].first; int last=prz[id].second; tab[0]=DiscDiameter((long long) zero); for(int i=prz[id].first+1;i<=prz[id].second;i++){ long long tmp=DiscDiameter((long long) i); tab[i-zero]=max(tab[i-zero-1],tmp); } long long tmp1=tab[prz[id].first],tmp2; if(id!=0){ Receive(id-1); tmp1=GetLL(id-1); } tmp2=max(tab[prz[id].second],tmp1); if(id!=x-1){ PutLL(id+1,tmp2); Send(id+1); } for(int i=0;i<=last-zero;i++){ if(tab[i]<tmp1){ tab[i]=tmp1; }else{ break; } } if(id==x-1){ for(int i=0;i<x-1;i++){ PutLL(i,tmp2); Send(i); } }else{ Receive(x-1); tmp2=GetLL(x-1); } int zeron=przn[id].first; int lastn=przn[id].second; int it=zeron; while(HoleDiameter((long long)(it))>=tmp2&&it<=lastn) it++; tmp1=it; if(id!=0){ Receive(id-1); tmp1=GetLL(id-1); if(tmp1==zeron){ tmp1=it; } } if(id!=x-1){ PutLL(id+1,tmp1); Send(id+1); Receive(x-1); tmp1=GetLL(x-1); } if(id==x-1){ for(int i=0;i<x-1;i++){ PutLL(i,tmp1); Send(i); } } int a=0,b=tmp1-1; if(id==0){ while(a<b){ int mid=(a+b+1)/2; int bol=1; PutInt(1,mid); Send(1); for(int i=zero;i<=last;i++){ int od_gory=m-i; if(tab[i-zero]>HoleDiameter(mid+od_gory)){ bol=0; break; } } PutInt(1,bol); Send(1); Receive(x-1); if(GetInt(x-1)){ a=m; }else{ b=m-1; } } PutInt(1,-1); Send(1); Receive(x-1); GetInt(x-1); }else{ while(true){ Receive(id-1); int mid=GetInt(id-1); PutInt((id+1)%x,mid); Send((id+1)%x); if(mid==-1) break; int bol=1; for(int i=zero;i<=last;i++){ int od_gory=m-i; if(tab[i-zero]>HoleDiameter(mid+od_gory)){ bol=0; break; } } Receive(id-1); bol=min(bol,GetInt(id-1)); PutInt((id+1)%x,bol); Send((id+1)%x); } return 0; } printf("%d",a); 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 | #include <cstdlib> #include <iostream> #include <bits/stdc++.h> using namespace std; #include "krazki.h" #include "message.h" pair<int, int> prz[100]; pair<int, int> przn[100]; long long tab[2000001]; int main() { int id = MyNodeId(); int x=NumberOfNodes(); int n, m; n=PipeHeight(); m=NumberOfDiscs(); int mp=m; int f=1; for(int i=0;i<x;i++){ int tmp=mp/(x-i); mp-=tmp+1; prz[i]=make_pair(f,f+tmp); f+=tmp+1; if(f==m) break; } int np=n; f=1; for(int i=0;i<x;i++){ int tmp=np/(x-i); np-=tmp+1; przn[i]=make_pair(f,f+tmp); f+=tmp+1; if(f==m) break; } int zero=prz[id].first; int last=prz[id].second; tab[0]=DiscDiameter((long long) zero); for(int i=prz[id].first+1;i<=prz[id].second;i++){ long long tmp=DiscDiameter((long long) i); tab[i-zero]=max(tab[i-zero-1],tmp); } long long tmp1=tab[prz[id].first],tmp2; if(id!=0){ Receive(id-1); tmp1=GetLL(id-1); } tmp2=max(tab[prz[id].second],tmp1); if(id!=x-1){ PutLL(id+1,tmp2); Send(id+1); } for(int i=0;i<=last-zero;i++){ if(tab[i]<tmp1){ tab[i]=tmp1; }else{ break; } } if(id==x-1){ for(int i=0;i<x-1;i++){ PutLL(i,tmp2); Send(i); } }else{ Receive(x-1); tmp2=GetLL(x-1); } int zeron=przn[id].first; int lastn=przn[id].second; int it=zeron; while(HoleDiameter((long long)(it))>=tmp2&&it<=lastn) it++; tmp1=it; if(id!=0){ Receive(id-1); tmp1=GetLL(id-1); if(tmp1==zeron){ tmp1=it; } } if(id!=x-1){ PutLL(id+1,tmp1); Send(id+1); Receive(x-1); tmp1=GetLL(x-1); } if(id==x-1){ for(int i=0;i<x-1;i++){ PutLL(i,tmp1); Send(i); } } int a=0,b=tmp1-1; if(id==0){ while(a<b){ int mid=(a+b+1)/2; int bol=1; PutInt(1,mid); Send(1); for(int i=zero;i<=last;i++){ int od_gory=m-i; if(tab[i-zero]>HoleDiameter(mid+od_gory)){ bol=0; break; } } PutInt(1,bol); Send(1); Receive(x-1); if(GetInt(x-1)){ a=m; }else{ b=m-1; } } PutInt(1,-1); Send(1); Receive(x-1); GetInt(x-1); }else{ while(true){ Receive(id-1); int mid=GetInt(id-1); PutInt((id+1)%x,mid); Send((id+1)%x); if(mid==-1) break; int bol=1; for(int i=zero;i<=last;i++){ int od_gory=m-i; if(tab[i-zero]>HoleDiameter(mid+od_gory)){ bol=0; break; } } Receive(id-1); bol=min(bol,GetInt(id-1)); PutInt((id+1)%x,bol); Send((id+1)%x); } return 0; } printf("%d",a); return 0; } |