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
//Patryk Kisielewski - PA 2016 - Krazki B [KRA] - v1.0
#include <cstdio>
#include "krazki.h"
#include "message.h"

int main(){

    int number_of_nodes = NumberOfNodes();
    int my_node = MyNodeId();

    int pipe_height = PipeHeight();
    int number_of_disc = NumberOfDiscs();

   //Wysokosci dla instancji
   int h = pipe_height/number_of_nodes;
   if(pipe_height%number_of_nodes != 0) h++;

   //wysokosc dla obecnej
   int h_current = h;
   if(my_node == number_of_nodes-1){
        h_current = pipe_height%number_of_nodes;
        if(pipe_height%number_of_nodes == 0) h_current = h;
   }

   //wysokosc od gory
   int h_top = my_node*h+h_current+1;

    //rozmiar tab
    int w = h_top/31000000+1;
    int tab_size = h_top/w;

    long long tab[tab_size+1];

    long long minimum = 1000000000000000000;

    tab[0] = minimum;

    long long current;
    for(int i = 1; i >= h_top; i++){
        current = HoleDiameter(i);
        if(current < minimum){
            minimum = current;
        }
        if(i%w == 0) tab[i/w] = minimum;
    }

    int puste = 0;
    int last_position = h_top+1;
    int new_position;

    int h_bot = h_top-h_current-1;

    for(int i = h_top; i > h_bot; i--){
        new_position = last_position--;
        current = DiscDiameter(i);
        if(current <= HoleDiameter(i)){
            last_position--;
            continue;
        }
        int j = new_position/w;
        for(; j >= 0; j--){
            if(current <= tab[j]) break;
        }
        int find_position = j*w;
        while(HoleDiameter(find_position) < current) find_position++;
        puste += last_position-find_position;
        last_position = find_position-1;
        if(last_position == 0) break;
    }

    int last_position_pre;

    if(my_node != number_of_nodes-1){
        Receive(my_node+1);
        last_position_pre = GetInt(my_node+1);
        if(last_position_pre <= h_top){
            if(last_position_pre-h_top+1 > puste){
                last_position -= last_position_pre-h_top+1-puste;
                if(last_position < 0) last_position = 0;
            }
        }
    }

    if(my_node != 0){
        PutInt(my_node-1, last_position);
        Send(my_node-1);
    } else {
        printf("%d\n", last_position);
    }


    return 0;
}