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
#include <cstdlib>
#include <iostream>
#include <stack>

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

using namespace std;
#define int64_t long long int
const int64_t inf = 0x7FFFFFFFFFFFFFFF;

int main() 
{
	if( MyNodeId() != 0 )
   		return 0;
	int n = PipeHeight(), m = NumberOfDiscs();

    stack<int64_t> pipe;

    int64_t minimalOpening = inf;
    for( int i = 0; i < n; ++i )
    {
        int64_t opening = HoleDiameter(i+1);

        minimalOpening = min( minimalOpening, opening );
        pipe.push( minimalOpening );
    }

    int64_t maximalDisc = -1;
    for( int i = 0; i < m; ++i )
    {
        int64_t disc = DiscDiameter(i+1);

        maximalDisc = max( maximalDisc, disc );
        while( !pipe.empty() && pipe.top() < maximalDisc )
            pipe.pop();

        if( pipe.empty() )
        {
            cout << "0";
            return 0;
        }
        else if( i != m-1 )
            pipe.pop();
        else
        {
            cout << pipe.size();
            return 0;
        }
    }

	return 0;
}