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
#include <iostream>
#include "message.h"
#include "krazki.h"

using namespace std;

const int MAX_DL = 3e6 + 5;
const int INF = 2e9;
const int MAX_T = 105;

long long tab_poc[MAX_DL];
long long pip_loc[MAX_DL];
long long moj_min[MAX_T];

int H, M, T, ID, DL_M, DL_H, MOJ_M, MOJ_H;

int krazek(long long kr) {
    int poc = 0;
    int kon = MOJ_M - 1;
    while(poc < kon) {
        //cout<<poc<<" "<<kon<<endl;
        int sr = (poc + kon + 1) / 2;
        //cout<<kr<<" "<<pip_loc[sr]<<endl;
        if(kr <= pip_loc[sr]) {
            poc = sr;
        }
        else {
            kon = sr - 1;
        }
    }
    if(kr > pip_loc[poc]) {
        return 0;
    }
    return poc + 1;
}

int findd(long long kr) {
    int ret = H -1;
    for(int i = 0; i < T; i++) {
        if(kr > moj_min[i]) {
            int dl = i == T-1 ? DL_H : H - (T - 1) * DL_H;
            for(int j = 0; j < dl; j++) {
                int it = j + i * DL_H + 1;
                if(HoleDiameter(it) < kr) {
                    ret = it;
                    break;
                }
            }
            break;
        }
    }
    return ret;
}

int main() {
/*
    pip_loc[0] = 5;
    pip_loc[1] = 5;
    pip_loc[2] = 5;
    pip_loc[3] = 3;
    pip_loc[4] = 6;
    pip_loc[5] = 2;
    pip_loc[6] = 3;
    MOJ_M = 7;
    cout<<krazek(1LL)<<endl;
    */
/*
    pip_loc[0] = 5;
    pip_loc[1] = 5;
    pip_loc[2] = 5;
    pip_loc[3] = 3;
    pip_loc[4] = 6;
    pip_loc[5] = 2;
    pip_loc[6] = 3;
    MOJ_M = 6;
    */
    //cout<<findd(3LL)<<endl;
    H = PipeHeight();
    M = NumberOfDiscs();
    T = NumberOfNodes();
    ID = MyNodeId();
    /*
    M = 42313;
    H = 12342;
    T = 100;
    ID = 93;
    */
    MOJ_M = DL_M = M/ (T-1);
    if (ID == T - 1) {
        MOJ_M = M - (T - 1) * DL_M;
    }
    MOJ_H = DL_H = H / (T-1);
    if (ID == T - 1) {
        MOJ_H = H - (T - 1) * DL_H;
    }

    for (int i = 0; i < MOJ_H; i++) {
        int it = i + ID * DL_H + 1;
        tab_poc[i] = HoleDiameter(it);
        if (i > 0 && tab_poc[i] > tab_poc[i - 1]) {
            tab_poc[i] = tab_poc[i - 1];
        }

    }

    moj_min[ID] = tab_poc[MOJ_H - 1];

    for (int j = 0; j < T; j++) {
        if(ID!=j) {
            PutLL(j, moj_min[ID]);
            Send(j);
        }
    }

    for (int j = 0; j < T; j++) {
        if (ID != j) {
            Receive(j);
            moj_min[j] = GetLL(j);
        }
    }

    for(int i = 0; i < T; i++) {
        if(i!=0 && moj_min[i] > moj_min[i-1]) {
            moj_min[i] = moj_min[i-1];
        }
    }
    long long kr_poc = DiscDiameter(ID * DL_M + 1);

    int poz_poc = findd(kr_poc);

    int wynik = INF;
    for (int i = 0; i < MOJ_M; i++) {
        int it = i + poz_poc + 1;
        if(it >= H) {break;}
        pip_loc[i] = HoleDiameter(it);
    }
    for (int i = 0; i < MOJ_M; i++) {
        if (i > 0 && pip_loc[i] > pip_loc[i - 1]) {
            pip_loc[i] = pip_loc[i - 1];
        }
    }
    for (int i = 0; i < MOJ_M; i++) {
        int it = i + poz_poc + 1;
        if(it >= H) {break;}
        wynik = min(wynik, krazek(DiscDiameter(it)) - i);
    }

    if (ID != 0) {
        PutInt(0, wynik);
        Send(0);
    }
    else {
        for (int i = 1; i < T; i++) {
            Receive(i);
            wynik = min(wynik, GetInt(i));
        }
    }

    wynik = max(0, wynik);
    if(ID == 0) {
        cout << wynik << endl;
    }
    return 0;
}