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
#include <bits/stdc++.h>
#include "message.h"
#include "krazki.h"

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

using namespace std;

typedef long long ll;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> VI;
typedef vector <bool> VB;
typedef vector <pii> VP;
typedef vector <ll> VL;
typedef vector <pll> VPL;

typedef vector <VI> VVI;
typedef vector <VL> VVL;
typedef vector <VB> VVB;
typedef vector <VP> VVP;

/*************************************************************************/

ll getHole(int i) {
    return HoleDiameter(i+1);
}

ll getDisc(int i) {
    return DiscDiameter(i+1);
}

/*************************************************************************/

int main() {
    const ll LINF = 1000000000000000001LL;
    
    int nodes = NumberOfNodes();
    int myId = MyNodeId();

    int n = PipeHeight();
    int m = NumberOfDiscs();
    
    if (m > n) {
        if (myId == 0) {
            cout << 0;
        }
        
        return 0;
    }
    
    int pipeBlock = (n + nodes - 1) / nodes;
    int pipeL = pipeBlock * myId;
    int pipeR = min(pipeL + pipeBlock, n) - 1;

    nodes = (n + pipeBlock - 1) / pipeBlock;

    if (myId >= nodes) {
        return 0;
    }
    
    VL myBlockMins(pipeR-pipeL+1);
    FOR(i,pipeL,pipeR) {
        myBlockMins[i-pipeL] = getHole(i);
    }
    
    FOR(i,1,pipeR-pipeL) {
        myBlockMins[i] = min(myBlockMins[i], myBlockMins[i-1]);
    }
    
    FOR(i,0,nodes-1) {
        PutLL(i, myBlockMins.back());
        Send(i);
    }
    
    VL blockMins(nodes, LINF);
    
    FOR(i,0,nodes-1) {
        Receive(i);
        blockMins[i] = GetLL(i);
    }
    
    FOR(i,1,nodes-1) {
        blockMins[i] = min(blockMins[i], blockMins[i-1]);
    }
    
    if (myId < m) {
        int nodesHere = min(m, nodes);
        
        int discsBlock = (m + nodesHere - 1) / nodesHere;
        int discsL = discsBlock * myId;
        int discsR = min(discsL + discsBlock, m) - 1;
        
        int currBlock = nodes - 1;
        VI firstSeen(nodes,m);
        
        FOR(i,discsL,discsR) {
            ll currDisc = getDisc(i);
            
            while (currBlock > 0 && currDisc > blockMins[currBlock - 1]) {
                currBlock--;
            }
            
            firstSeen[currBlock] = min(firstSeen[currBlock], i);            
        }

        FOR(i,0,nodes-1) {
            PutInt(i, firstSeen[i]);
            Send(i);
        }
    }
    
    int discsPos = m;
    FOR(i,0,min(m,nodes)-1) {
        Receive(i);
        int firstSeen = GetInt(i);
        
        discsPos = min(discsPos, firstSeen);
    }
    
    int ans = n;    
    int pipePos = pipeR;

    while (discsPos < m && pipePos >= pipeL) {
        ll currDisc = getDisc(discsPos);
        
        while (pipePos >= pipeL && currDisc > myBlockMins[pipePos-pipeL]) {
            pipePos--;
        }
        
        ans = min(ans, pipePos - (m - discsPos - 1) + 1);
        
        discsPos++;
        pipePos--;
    }
    
    PutInt(0, ans);
    Send(0);
    
    if (myId == 0) {
        int globalAns = n;
        
        FOR(i,0,nodes-1) {
            Receive(i);
            globalAns = min(globalAns, GetInt(i));
        }
        
        globalAns = max(globalAns, 0);
        cout << globalAns;
    }

    return 0;
}

/*************************************************************************/