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

using namespace std;

typedef long long int Radius;
Radius inf = 2000000000000000000LL;

class OneNodeSolver {

public:
    int offsetDiscs;
    int unusedPipe;

    OneNodeSolver() {
        offsetDiscs = 0;
        unusedPipe = 0;
    }

    OneNodeSolver(int _offsetDiscs, int _unusedPipe) {
        offsetDiscs = _offsetDiscs;
        unusedPipe = _unusedPipe;
    }

    int iterSearch(Radius* t, Radius r, int beg, int end) {
        if (end <= beg)
            return  0;
        int i = beg;
        while (i < end-1 && t[i+1] >= r)
            i++;
        return i;
    }

    /*
     * Wyszukaj ostatni element wiekszy lub rowny r, w malejacej tablicy
     * w zakresie [beg, end)
     */
    int binSearch(Radius* t, Radius r, int beg, int end) {
        if (end - beg < 8)
            return iterSearch(t, r, beg, end);

        int m = (beg + end) / 2;
        if (t[m] < r)
            return binSearch(t, r, beg, m);
        else
            return binSearch(t, r, m, end);
    }

    int solve() {
        int n = PipeHeight();
        int m = NumberOfDiscs();
        int size = n - unusedPipe;
        Radius* cumMin = new Radius[size+1];
        cumMin[0] = inf;
        for (int i = 1; i <= size; ++i) {
            Radius a = HoleDiameter(i);
            cumMin[i] = min(cumMin[i-1], a);
        }
        int L = size+1;
        Radius currMax = 0;
        for (int i = 1 + offsetDiscs; i <= m; ++i) {
            if (L <= 0) {
                L -= m - i + 1;
                return L;
            }

            Radius r = DiscDiameter(i);
            if (r > currMax) {
                currMax = r;
                if (cumMin[L-1] >= r)
                    L--;
                else
                    L = binSearch(cumMin, r, 0, L);
            } else {
                L--;
            }
        }
        return L;
    }
};

int main() {
    ios_base::sync_with_stdio(0);

    if (MyNodeId() != 0) {
      return 0;
    }

    if (PipeHeight() < NumberOfDiscs()) {
        cout << 0 << endl;
        return 0;
    }

    if (PipeHeight() < 1100000) {
        OneNodeSolver solver;
        int s = solver.solve();
        cout << (s < 0 ? 0 : s) << endl;
    }  else {
        int n = PipeHeight();
        int m = NumberOfDiscs();
        int unusedPipe, offsetDiscs;

        if (n <= 10000000)
            unusedPipe = int(0.6 * n);
        else if (n <= 50000000)
            unusedPipe = int(0.9 * n);
        else
            unusedPipe = int(0.98 * n);

        m = static_cast<int>( static_cast<double>(m) * (n - unusedPipe) / n);


        OneNodeSolver solver(offsetDiscs, unusedPipe);
        int s = solver.solve();
        cout << (s < 0 ? 0 : s) << endl;
    }
    return 0;
}