#include <bits/stdc++.h>
#include "message.h"
#include "krazki.h"
using namespace std;
const long long INF = (long long)(1e18) + 5;
vector<long long> mn;
int main()
{
int men = MyNodeId();
int ins = NumberOfNodes();
int n = PipeHeight();
int m = NumberOfDiscs();
int POD = (n / ins) + 2;
mn.resize(POD + 1);
int a = POD * men, b = POD * men + POD - 1;
// faza 1
long long val = INF;
if (men != 0)
{
Receive(men-1);
val = GetLL(men-1);
}
int pos = 0;
for (int i = a; i <= min(b, n-1); ++ i)
mn[pos ++] = val = min(val, HoleDiameter(i+1));
if (men != ins-1)
{
PutLL(men+1, val);
Send(men+1);
}
// faza 2
int actDisc = 1;
if (men != ins-1)
{
Receive(men+1);
actDisc = GetInt(men+1);
}
if (actDisc == -1)
{
if (men != 0)
{
PutInt(men-1, -1);
Send(men-1);
}
return 0;
}
for (int pos2 = min(n-1,b) % POD; pos2 >= 0; -- pos2)
{
if (DiscDiameter(actDisc) <= mn[pos2])
{
actDisc ++;
if (actDisc == m+1) // znalazlem pozycje
{
printf("%d\n", a+pos2+1);
if (men != 0)
{
PutInt(men-1, -1);
Send(men-1);
}
return 0;
}
}
}
if (men == 0)
{
puts("0");
return 0;
}
PutInt(men-1, actDisc);
Send(men-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 | #include <bits/stdc++.h> #include "message.h" #include "krazki.h" using namespace std; const long long INF = (long long)(1e18) + 5; vector<long long> mn; int main() { int men = MyNodeId(); int ins = NumberOfNodes(); int n = PipeHeight(); int m = NumberOfDiscs(); int POD = (n / ins) + 2; mn.resize(POD + 1); int a = POD * men, b = POD * men + POD - 1; // faza 1 long long val = INF; if (men != 0) { Receive(men-1); val = GetLL(men-1); } int pos = 0; for (int i = a; i <= min(b, n-1); ++ i) mn[pos ++] = val = min(val, HoleDiameter(i+1)); if (men != ins-1) { PutLL(men+1, val); Send(men+1); } // faza 2 int actDisc = 1; if (men != ins-1) { Receive(men+1); actDisc = GetInt(men+1); } if (actDisc == -1) { if (men != 0) { PutInt(men-1, -1); Send(men-1); } return 0; } for (int pos2 = min(n-1,b) % POD; pos2 >= 0; -- pos2) { if (DiscDiameter(actDisc) <= mn[pos2]) { actDisc ++; if (actDisc == m+1) // znalazlem pozycje { printf("%d\n", a+pos2+1); if (men != 0) { PutInt(men-1, -1); Send(men-1); } return 0; } } } if (men == 0) { puts("0"); return 0; } PutInt(men-1, actDisc); Send(men-1); return 0; } |
English