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
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cassert>
#include <limits.h>

#include "krazki.h"
#include "message.h"

using namespace std;

#define ST first
#define ND second
#define MP make_pair
#define PB push_back

typedef long long LL;
typedef long double LD;

typedef vector<int> VI;
typedef pair<int,int> PII;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(VAR(i,a);i<=(b);++i)
#define FORD(i,a,b) for(VAR(i,a);i>=(b);--i)
#define FORE(a,b) for(VAR(a,(b).begin());a!=(b).end();++a)
#define VAR(a,b) __typeof(b) a=(b)
#define SIZE(a) ((int)((a).size()))
#define ALL(x) (x).begin(),(x).end()
#define CLR(x,a) memset(x,a,sizeof(x))
#define ZERO(x) memset(x,0,sizeof(x))
#define S(t,i) scanf("%" ## t, &i)
#define SI(i) scanf("%d", &i)

const int MAXN=10000000;

LL holes[MAXN*2+9];

int n, m;
int nodeStart;
int nodeEnd;
int nodeHeight;
int nodeId;
int nodes;

int checkDiscs(int t) {
    int discStart;
    int discEnd;
    if (nodeEnd<t || nodeStart>t+m-1) {
        return 0;
    } else if (t>=nodeStart && t<=nodeEnd) {
        discStart=1;
    } else {
        int tmp = (nodeStart-t+nodeHeight-1)/nodeHeight;
        discStart=1+nodeHeight*tmp;
    }
    discEnd=min(m,discStart+nodeHeight-1);
    FOR(di, discStart, discEnd) {
//        if (di<0 || di>m) {
//            fprintf(stderr, "t %d di %d\n", t, di);
//        }
//        if (t-nodeStart+di<1) {
//            fprintf(stderr, "t %d nodeStart %d ndoeEnd %d di %d nodeheight %d \n", t, nodeStart, nodeEnd, di, nodeHeight);
//            fflush(stderr);
//        }
//        assert(t-nodeStart+di>=1);
//        assert(t-nodeStart+di<=2*nodeHeight);
        if (DiscDiameter(di)>holes[t-nodeStart+di]) {
            return 1;
        }
    }
    
    return 0;
}

int main() {
    nodes = NumberOfNodes();
    nodeId = MyNodeId();
    n = PipeHeight();
    m = NumberOfDiscs();
    
    if (m>n) {
        if (nodeId == 0) {
            cout<<0;
        }
        return 0;
    }
    if (n<100000) {
        // small case
        if (nodeId == 0) {
            LL last = LLONG_MAX;
            FORD(i, n, 1) {
                last = holes[i] = min(last, HoleDiameter(n-i+1));
            }
            holes[n+1]=LLONG_MAX;
            int pos=0;
            FOR(i, 1, m) {
                LL disc=DiscDiameter(i);
                if (pos<=n) pos++;
                while (holes[pos]<disc) pos++;
            }
            
            cout<<n+1-pos;
        }
        return 0;
    }
    
    nodeHeight = (n+nodes-1)/nodes;
    
    nodeStart = 1 + nodeHeight*nodeId;
    nodeEnd = min(n, nodeStart+nodeHeight-1);
    
    // compute pipe holes
    
    LL last = LLONG_MAX;
    FORD(i, nodeEnd, nodeStart) {
        last = holes[i-nodeStart+1] = min(last, HoleDiameter(n-i+1));
    }
    
    LL upperConstraint=0;
    if (nodeId == nodes-1) {
        PutLL(nodeId-1, last);
        Send(nodeId-1);
    } else if (nodeId>0) {
        Receive(nodeId+1);
        upperConstraint = GetLL(nodeId+1);
        
        PutLL(nodeId-1, min(upperConstraint, last));
        Send(nodeId-1);
    } else {
        Receive(nodeId+1);
        upperConstraint = GetLL(nodeId+1);
    }
    
    if ((nodeId < nodes-1)){
        FORD(i, nodeEnd, nodeStart) {
            if (upperConstraint < holes[i-nodeStart+1]) {
                holes[i-nodeStart+1] = upperConstraint;
            } else {
                break;
            }
        }
    }
    
    if (nodeId > 0) {
        PutLL(nodeId-1, holes[nodeEnd-nodeStart+1]);
        Send(nodeId-1);
    }
    if (nodeId < nodes-1) {
        Receive(nodeId+1);
        last = GetLL(nodeId+1);
        
        int upperNodeStart = nodeEnd + 1;
        int upperNodeEnd = min(n, nodeEnd+nodeHeight);
        
        FORD(i, upperNodeEnd, upperNodeStart) {
            last = holes[i-nodeStart+1] = min(last, HoleDiameter(n-i+1));
        }
    }
    
    // binary search
    int t=0;
    if (nodeId == 0) {
        int searchStart = 0;
        int searchEnd = n-m+1+1;
        int result=0;
        
        while (searchEnd-searchStart>1) {
            t = (searchEnd+searchStart)/2;
            
            FOR(i, 1, nodes-1) {
                int nodeIStart = 1 + nodeHeight*i;
                int nodeIEnd = min(n, nodeIStart+nodeHeight-1);
                if (!(nodeIEnd<t)) {
                    PutInt(i, t);
                    Send(i);
                    break;
                }
            }
//            fprintf(stderr, "\nt %d ", t);
            result = checkDiscs(t);
//            fprintf(stderr, "%d ", result);
            FOR(i, 1, nodes-1) {
                int nodeIStart = 1 + nodeHeight*i;
                int nodeIEnd = min(n, nodeIStart+nodeHeight-1);
                if (!(nodeIEnd<t)) {
                    Receive(i);
                    result += GetInt(i);
                }
//                fprintf(stderr, "%d ", result);
            }
//            fprintf(stderr, "\n");
            if (result>0) {
                // fail
                searchStart = t;
            } else {
                // success
                searchEnd = t;
            }
            
        }
        FOR(i, 1, nodes-1) {
            PutInt(i, 0);
            Send(i);
        }
        
        if (result>0) {
            t++;
        }
        cout<<n+1-(t+m-1);
        
    } else {
        int from=Receive(-1);
        t = GetInt(from);
        if (t && nodeId<nodes-1) {
            PutInt(nodeId+1, t);
            Send(nodeId+1);
        }
        while (t) {
            int result = checkDiscs(t);
            
            PutInt(0, result);
            Send(0);
            
            from=Receive(-1);
            t = GetInt(from);
            if (t && nodeId<nodes-1) {
                PutInt(nodeId+1, t);
                Send(nodeId+1);
            }
        }
    }
    return 0;
}