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
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <complex>
#include <sstream>
using namespace std;

#include "message.h"
#include "krazki.h"
 
typedef long long LL;
typedef unsigned long long ULL;
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 SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()
 
#define pb push_back
#define mp make_pair
#define st first
#define nd second

const LL INFLL = 1e18 + 100;

int NODES, ME, HEIGHT, DISCS;

int height_intervals[200];
int H = 0;

int disc_intervals[200];
int D = 0;

LL minis[200];
void calculate_minis() {
    if (ME < H) {
        minis[ME] = INFLL;
        FOR(i,height_intervals[ME],height_intervals[ME+1]) {
            minis[ME] = min(minis[ME], HoleDiameter(i+1));
        }
        if (ME > 0) {
            PutLL(0, minis[ME]);
            Send(0);
        }
    }
    if (ME == 0) {
        FOR(i,1,H) {
            Receive(i);
            minis[i] = GetLL(i);
        }
        FOR(i,1,NODES) {
            REP(j,H) PutLL(i, minis[j]);
            Send(i); 
        }
    } else {
        Receive(0);
        REP(j,H) minis[j] = GetLL(0);
    }
}

vector<LL> fragment;
int results[200];
void calculate_ranges() {
    if (ME >= D) return;
    
    LL maxi = -1;
    FOR(i, disc_intervals[ME], disc_intervals[ME+1]) maxi = max(maxi, DiscDiameter(i+1));
    
    int interval = 0;
    while (interval < H && minis[interval] >= maxi) ++interval;

    int int_size = disc_intervals[ME+1] - disc_intervals[ME];
    int drop_from = height_intervals[interval] - int_size - 10;
    int drop_to = (interval == H ? HEIGHT : height_intervals[interval + 1]) + int_size + 10;
    drop_to = min(drop_to, HEIGHT - 1);
    
    FOR(i,drop_from,drop_to+1) fragment.pb(i < 0 ? INFLL : HoleDiameter(i+1));
    FOR(i,1,fragment.size()) fragment[i] = min(fragment[i], fragment[i-1]);
    
    int K = fragment.size();
    FOR(i, disc_intervals[ME], disc_intervals[ME+1]) {
        LL size = DiscDiameter(i+1);
        --K;
        while (fragment[K] < size) --K;
    }
    
    results[ME] = K + drop_from;
    if (ME > 0) {
        PutInt(0, results[ME]);
        Send(0);
        return;
    }
  
    FOR(i,1,D) {
        Receive(i);
        results[i] = GetInt(i);
        results[i] = min(results[i], results[i-1] - (disc_intervals[i+1] - disc_intervals[i]));
    }
    
    printf("%d\n", max(0, results[D-1] + 1));
}

int main() {
    NODES = NumberOfNodes();
    ME = MyNodeId();

    HEIGHT = PipeHeight();
    DISCS = NumberOfDiscs();
    
    int K = (HEIGHT + NODES - 1) / NODES;
    REP(i,NODES+1) height_intervals[i] = min(K * i, HEIGHT);
    while (H < NODES && height_intervals[H+1] > height_intervals[H]) ++H;
    K = (DISCS + NODES - 1) / NODES;
    REP(i,NODES+1) disc_intervals[i] = min(K * i, DISCS);
    while (D < NODES && disc_intervals[D+1] > disc_intervals[D]) ++D;
    
    calculate_minis();
    calculate_ranges();  
}