// Przykładowe (błędne) rozwiązanie - każdy węzeł wysyła węzłowi o numerze 0 // pierwszą z cen swoich akcji. Węzeł 0 wypisuje sumę tych liczb. #include "message.h" #include "skup.h" #include <assert.h> /*long long NumberOfCompanies() { return 1000; } long long GetShareCost(long long Id) { assert(0 <= Id && Id < 1000); return 1000 * 1000 * 1000; } /// --- Wrappery do Put{Char, Int, LL} --- /// int __CanSend(int Src, int Dest) { if (Src == Dest) { return 1; } return Dest == Src + 1; } void __PutCharWrapper(int Dest, char Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutChar(Dest, Value); } void __PutIntWrapper(int Dest, int Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutInt(Dest, Value); } void __PutLLWrapper(int Dest, long long Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutLL(Dest, Value); } #define PutChar __PutCharWrapper #define PutInt __PutIntWrapper #define PutLL __PutLLWrapper*/ #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; int totm,myid,out[110],flag,pd[110],numvisit,num[110]; vector<int>A[110]; const int inf=5; void visit0(int source){ if (pd[source]==0){ // cout<<"receive "<<myid<<" "<<source<<endl; pd[source]=1; numvisit++; if (myid==0){ PutInt(source,0); Send(source); } return; } } void calleveryone(){ for (int i=0;i<totm;i++) if (i!=myid){ PutInt(i,0); Send(i); } } void getinfofrom(int K){ PutInt(K,0); Send(K); } int x[210000],len; void printans(){ for (int i=0;i<totm;i++) for (int j=0;j<A[i].size();j++) x[++len]=A[i][j]; sort(x+1,x+len+1); long long ans=0; // for (int i=1;i<=len;i++) cout<<x[i]<<" "; cout<<endl; for (int i=1;i<=len;i++) ans+=1ll*x[i]*(len-i+1); cout<<ans<<endl; } void getin(int K){ // cout<<"getin "<<myid<<" "<<K<<" "<<flag<<endl; if (K!=-1&&pd[K]==0){ visit0(K); return; } if (K!=-1) pd[K]++; if (K!=-1&&flag){ PutInt(K,1); Send(K); return; } //cout<<"start "<<myid<<" "<<K<<endl; for (int i=0;i<totm;i++) if (i!=myid){ // cout<<"send "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); } if (myid){ Receive(0); GetInt(0); } /*for (int i=0;i<totm;i++) if (i!=myid){ cout<<"send "<<myid<<" "<<i<<endl; Receive(i); GetInt(i); }*/ pd[myid]++; numvisit++; flag=1; int waitnum=0; for (int i=1;i<totm;i++) if (out[i]){ // cout<<"find "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); waitnum++; } // cout<<"wati "<<myid<<" "<<waitnum<<" "<<flag<<endl; while (waitnum){ int source=Receive(-1); int tag=GetInt(source); // cout<<"receive "<<myid<<" "<<waitnum<<" "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (tag==0){ if (pd[source]==0) visit0(source); else { pd[source]++; PutInt(source,1); Send(source); } continue; } waitnum--; } // cout<<"endwait "<<myid<<endl; if (K!=-1){ PutInt(K,1); Send(K); } } void finishall(){ for (int i=0;i<totm;i++) if (i!=myid){ int ti=0; if (out[i]) ti=1; else ti=4; // cout<<"put "<<myid<<" "<<i<<" "<<ti<<endl; for (int j=1;j<=ti;j++){ PutInt(i,0); Send(i); } } exit(0); } void sendback(int K,int source){ // cout<<"Sendback "<<myid<<" "<<K<<" "<<source<<endl; PutInt(K,-1); PutInt(K,1); PutInt(K,source); PutInt(K,A[source].size()); for (int i=0;i<A[source].size();i++) PutInt(K,A[source][i]); Send(K); } int DecodeInfo(int K,int source){ int n=GetInt(source); if (n==0) return 1; int where=GetInt(source); //cout<<"Decode "<<myid<<" "<<source<<" "<<where<<endl; int size=GetInt(source); // cout<<"getfrom "<<myid<<" "<<where<<" "<<source<<endl; A[where].clear(); for (int j=0;j<size;j++) A[where].push_back(GetInt(source)); if (K!=0&&K!=-1) sendback(K,where); return where==source; } void waitallinfo(int totalsize,int K){ if (totalsize==0) return; while (1){ int source=Receive(-1); int tag=GetInt(source); // cout<<"waitinfo "<<myid<<" "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (tag==0){ if (pd[source]==0){ visit0(source); continue; } pd[source]++; if (num[source]>=3) exit(0); PutInt(source,-1); PutInt(source,0); Send(source); continue; } totalsize-=DecodeInfo(K,source); if (totalsize) continue; return; } } void giveback(int K){ // cout<<"GiveBack "<<myid<<" "<<K<<endl; if (K==0){ for (int i=0;i<totm;i++) if (i!=myid&&A[i].size()>0){ PutInt(K,-1); PutInt(K,1); PutInt(K,i); PutInt(K,A[i].size()); for (int j=0;j<A[i].size();j++) PutInt(K,A[i][j]); Send(K); } } // cout<<"Secdback "<<myid<<" "<<K<<" "<<myid<<endl; PutInt(K,-1); PutInt(K,1); PutInt(K,myid); PutInt(K,A[myid].size()); for (int j=0;j<A[myid].size();j++) PutInt(K,A[myid][j]); Send(K); } void getinfo(int K){ if (K!=-1) pd[K]++; //cout<<"GetInfo "<<myid<<" "<<K<<" "<<flag<<endl; if (K!=-1&&flag==2){ PutInt(K,-1); PutInt(K,0); Send(K); // cout<<"sendback "<<myid<<" "<<K<<endl; return; } // cout<<"asd"<<endl; flag=2; int totalwait=0; for (int i=0;i<totm;i++) if (out[i]){ // cout<<"Send "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); totalwait++; } // cout<<"Wait "<<myid<<" "<<totalwait<<endl; waitallinfo(totalwait,K); // cout<<"end "<<myid<<endl; // for (int i=0;i<totm;i++) cout<<A[i].size()<<" "; cout<<endl; int bo=0; for (int i=0;i<totm;i++) if (A[i].size()==0) bo=1; // cout<<"finishfind "<<myid<<" "<<K<<" "<<bo<<endl; if (bo==0){ printans(); finishall(); } giveback(K); } void waitcall(){ while (1){ int source=Receive(-1); int tag=GetInt(source); // cout<<"called "<<myid<<" "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (pd[source]<2) getin(source); else if (pd[source]==2) getinfo(source); else exit(0); } } int main(){ // cout<<"start"<<endl; totm=NumberOfNodes(); myid=MyNodeId(); flag=0; int num=NumberOfCompanies(); for (int i=0;i<num;i++) A[myid].push_back(GetShareCost(i)); // cout<<"print "<<myid<<endl; // for (int i=0;i<num;i++) cout<<A[myid][i]<<" "; cout<<endl; for (int i=0;i<totm;i++) if (i!=myid){ PutInt(i,1); Send(i); } for (int i=0;i<totm;i++) if (i!=myid){ Receive(i); out[i]=GetInt(i); } //cout<<"asd"<<endl; //for (int i=0;i<totm;i++) cout<<out[i]<<" "; cout<<endl; if (myid==0){ getin(-1); //cerr<<"numvisit "<<myid<<" "<<numvisit<<" "<<totm<<endl; if (numvisit==totm){ getinfo(-1); } for (int i=1;i<totm;i++){ if (out[i]||pd[i]) continue; // cerr<<"startdfs "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); while (1){ int source=Receive(-1),tag=GetInt(source); // cout<<"receiveroot "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (pd[source]==0){ // cout<<"firvisit"<<endl; visit0(source); continue; } pd[source]++; if (source==i) break; // cout<<"Send "<<myid<<" "<<source<<endl; PutInt(source,1); Send(source); } // cerr<<endl<<endl<<endl<<"finish "<<i<<" "<<numvisit<<endl; if (totm==numvisit){ for (int j=0;j<totm;j++) pd[j]=2; // cout<<"Send "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); waitcall(); } } } else { waitcall(); } }
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | // Przykładowe (błędne) rozwiązanie - każdy węzeł wysyła węzłowi o numerze 0 // pierwszą z cen swoich akcji. Węzeł 0 wypisuje sumę tych liczb. #include "message.h" #include "skup.h" #include <assert.h> /*long long NumberOfCompanies() { return 1000; } long long GetShareCost(long long Id) { assert(0 <= Id && Id < 1000); return 1000 * 1000 * 1000; } /// --- Wrappery do Put{Char, Int, LL} --- /// int __CanSend(int Src, int Dest) { if (Src == Dest) { return 1; } return Dest == Src + 1; } void __PutCharWrapper(int Dest, char Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutChar(Dest, Value); } void __PutIntWrapper(int Dest, int Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutInt(Dest, Value); } void __PutLLWrapper(int Dest, long long Value) { if (!__CanSend(MyNodeId(), Dest)) { Value = 0; } PutLL(Dest, Value); } #define PutChar __PutCharWrapper #define PutInt __PutIntWrapper #define PutLL __PutLLWrapper*/ #include <iostream> #include <cmath> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> using namespace std; int totm,myid,out[110],flag,pd[110],numvisit,num[110]; vector<int>A[110]; const int inf=5; void visit0(int source){ if (pd[source]==0){ // cout<<"receive "<<myid<<" "<<source<<endl; pd[source]=1; numvisit++; if (myid==0){ PutInt(source,0); Send(source); } return; } } void calleveryone(){ for (int i=0;i<totm;i++) if (i!=myid){ PutInt(i,0); Send(i); } } void getinfofrom(int K){ PutInt(K,0); Send(K); } int x[210000],len; void printans(){ for (int i=0;i<totm;i++) for (int j=0;j<A[i].size();j++) x[++len]=A[i][j]; sort(x+1,x+len+1); long long ans=0; // for (int i=1;i<=len;i++) cout<<x[i]<<" "; cout<<endl; for (int i=1;i<=len;i++) ans+=1ll*x[i]*(len-i+1); cout<<ans<<endl; } void getin(int K){ // cout<<"getin "<<myid<<" "<<K<<" "<<flag<<endl; if (K!=-1&&pd[K]==0){ visit0(K); return; } if (K!=-1) pd[K]++; if (K!=-1&&flag){ PutInt(K,1); Send(K); return; } //cout<<"start "<<myid<<" "<<K<<endl; for (int i=0;i<totm;i++) if (i!=myid){ // cout<<"send "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); } if (myid){ Receive(0); GetInt(0); } /*for (int i=0;i<totm;i++) if (i!=myid){ cout<<"send "<<myid<<" "<<i<<endl; Receive(i); GetInt(i); }*/ pd[myid]++; numvisit++; flag=1; int waitnum=0; for (int i=1;i<totm;i++) if (out[i]){ // cout<<"find "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); waitnum++; } // cout<<"wati "<<myid<<" "<<waitnum<<" "<<flag<<endl; while (waitnum){ int source=Receive(-1); int tag=GetInt(source); // cout<<"receive "<<myid<<" "<<waitnum<<" "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (tag==0){ if (pd[source]==0) visit0(source); else { pd[source]++; PutInt(source,1); Send(source); } continue; } waitnum--; } // cout<<"endwait "<<myid<<endl; if (K!=-1){ PutInt(K,1); Send(K); } } void finishall(){ for (int i=0;i<totm;i++) if (i!=myid){ int ti=0; if (out[i]) ti=1; else ti=4; // cout<<"put "<<myid<<" "<<i<<" "<<ti<<endl; for (int j=1;j<=ti;j++){ PutInt(i,0); Send(i); } } exit(0); } void sendback(int K,int source){ // cout<<"Sendback "<<myid<<" "<<K<<" "<<source<<endl; PutInt(K,-1); PutInt(K,1); PutInt(K,source); PutInt(K,A[source].size()); for (int i=0;i<A[source].size();i++) PutInt(K,A[source][i]); Send(K); } int DecodeInfo(int K,int source){ int n=GetInt(source); if (n==0) return 1; int where=GetInt(source); //cout<<"Decode "<<myid<<" "<<source<<" "<<where<<endl; int size=GetInt(source); // cout<<"getfrom "<<myid<<" "<<where<<" "<<source<<endl; A[where].clear(); for (int j=0;j<size;j++) A[where].push_back(GetInt(source)); if (K!=0&&K!=-1) sendback(K,where); return where==source; } void waitallinfo(int totalsize,int K){ if (totalsize==0) return; while (1){ int source=Receive(-1); int tag=GetInt(source); // cout<<"waitinfo "<<myid<<" "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (tag==0){ if (pd[source]==0){ visit0(source); continue; } pd[source]++; if (num[source]>=3) exit(0); PutInt(source,-1); PutInt(source,0); Send(source); continue; } totalsize-=DecodeInfo(K,source); if (totalsize) continue; return; } } void giveback(int K){ // cout<<"GiveBack "<<myid<<" "<<K<<endl; if (K==0){ for (int i=0;i<totm;i++) if (i!=myid&&A[i].size()>0){ PutInt(K,-1); PutInt(K,1); PutInt(K,i); PutInt(K,A[i].size()); for (int j=0;j<A[i].size();j++) PutInt(K,A[i][j]); Send(K); } } // cout<<"Secdback "<<myid<<" "<<K<<" "<<myid<<endl; PutInt(K,-1); PutInt(K,1); PutInt(K,myid); PutInt(K,A[myid].size()); for (int j=0;j<A[myid].size();j++) PutInt(K,A[myid][j]); Send(K); } void getinfo(int K){ if (K!=-1) pd[K]++; //cout<<"GetInfo "<<myid<<" "<<K<<" "<<flag<<endl; if (K!=-1&&flag==2){ PutInt(K,-1); PutInt(K,0); Send(K); // cout<<"sendback "<<myid<<" "<<K<<endl; return; } // cout<<"asd"<<endl; flag=2; int totalwait=0; for (int i=0;i<totm;i++) if (out[i]){ // cout<<"Send "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); totalwait++; } // cout<<"Wait "<<myid<<" "<<totalwait<<endl; waitallinfo(totalwait,K); // cout<<"end "<<myid<<endl; // for (int i=0;i<totm;i++) cout<<A[i].size()<<" "; cout<<endl; int bo=0; for (int i=0;i<totm;i++) if (A[i].size()==0) bo=1; // cout<<"finishfind "<<myid<<" "<<K<<" "<<bo<<endl; if (bo==0){ printans(); finishall(); } giveback(K); } void waitcall(){ while (1){ int source=Receive(-1); int tag=GetInt(source); // cout<<"called "<<myid<<" "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (pd[source]<2) getin(source); else if (pd[source]==2) getinfo(source); else exit(0); } } int main(){ // cout<<"start"<<endl; totm=NumberOfNodes(); myid=MyNodeId(); flag=0; int num=NumberOfCompanies(); for (int i=0;i<num;i++) A[myid].push_back(GetShareCost(i)); // cout<<"print "<<myid<<endl; // for (int i=0;i<num;i++) cout<<A[myid][i]<<" "; cout<<endl; for (int i=0;i<totm;i++) if (i!=myid){ PutInt(i,1); Send(i); } for (int i=0;i<totm;i++) if (i!=myid){ Receive(i); out[i]=GetInt(i); } //cout<<"asd"<<endl; //for (int i=0;i<totm;i++) cout<<out[i]<<" "; cout<<endl; if (myid==0){ getin(-1); //cerr<<"numvisit "<<myid<<" "<<numvisit<<" "<<totm<<endl; if (numvisit==totm){ getinfo(-1); } for (int i=1;i<totm;i++){ if (out[i]||pd[i]) continue; // cerr<<"startdfs "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); while (1){ int source=Receive(-1),tag=GetInt(source); // cout<<"receiveroot "<<source<<" "<<tag<<" "<<pd[source]<<endl; if (pd[source]==0){ // cout<<"firvisit"<<endl; visit0(source); continue; } pd[source]++; if (source==i) break; // cout<<"Send "<<myid<<" "<<source<<endl; PutInt(source,1); Send(source); } // cerr<<endl<<endl<<endl<<"finish "<<i<<" "<<numvisit<<endl; if (totm==numvisit){ for (int j=0;j<totm;j++) pd[j]=2; // cout<<"Send "<<myid<<" "<<i<<endl; PutInt(i,0); Send(i); waitcall(); } } } else { waitcall(); } } |