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
#include <cstdlib>
#include <iostream>
#include <climits>
#include <algorithm>
#include <iterator>
#include <functional>

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

using namespace std;

#define D(x) 
#define Dk(x) 

#define I long long
#define Kth 100
//#define Kth 2
#define MAX_K (210000000/Kth)

I holeKth[MAX_K];
int holeKth_size = 0;
int n, nn, m;


I hole[Kth];
int holeIdx=-1;

inline long long myHoleDiameter(int i) {
    if (i<n) return HoleDiameter(i+1);
    return 0;
}

void getHole(int p) {
    if(holeIdx == p) return;
    holeIdx = p;
    I r = holeKth[p];
    for(int i=0;i<Kth;i++) {
        r = min(myHoleDiameter(i+p*Kth),r);
        hole[i] = r;
    }
}

void printHole(){
    cout << "Hole:";
    for(int i=0;i<Kth;i++) {
        cout << " " << hole[i];
    }
    cout << "\n";
}

int getPos(long long d) {

    int pKth = std::distance(holeKth,std::upper_bound(holeKth, holeKth+holeKth_size, d, std::greater<I>()));
    D(cout << "GP: pKth: " << pKth);
    if(pKth == 0) return 0;
    getHole(pKth-1);
    int idx = std::distance(hole, std::upper_bound(hole, hole+Kth, d, std::greater<I>()));
    D(cout << "  idx: " << idx << "\n");
    D(printHole());
    idx--;
    hole[idx] = d-1;
    if(idx == 0) holeKth[pKth-1] = d-1;
    return idx+(pKth-1)*Kth+1;
}

void calcHoleKth() {
    long long r = LLONG_MAX;

    for(int i=0;i<nn;i++) {
        r = min(myHoleDiameter(i),r);
        if(i%Kth == 0) holeKth[holeKth_size++] = r;
    }
}

void mySend(int &pos, int &length) {
        int target = (MyNodeId()+1)%NumberOfNodes();
        PutInt(target, pos);
        PutInt(target, length);
        Dk(cout << "S(" << MyNodeId() << "): pos:" << pos << " length:" << length << "\n");
        Send(target);
}
void myRecive(int &pos, int &length) {
    int pos2,length2, target = (MyNodeId()+NumberOfNodes() - 1)%NumberOfNodes();
    Receive(target);
    pos2 = GetInt(target);
    length2 = GetInt(target);
    Dk(cout << "R(" << MyNodeId() << "): po2:" << pos2 << " lengt2:" << length2  << " length:" << length << "\n");
    if(length == 0) {
        length = length2;
        pos = pos2;
    } else {
        pos = max(0,min(pos, pos2-length));
    }
    Dk(cout << "K(" << MyNodeId() << "): pos:" << pos << " length:" << length << "\n");
}

int main() {

    n = PipeHeight();
    nn = n-(n%Kth) + Kth;
    calcHoleKth();
    m = NumberOfDiscs();

    int ile = m/NumberOfNodes()+1;
    int from = 1+ile*MyNodeId(), to =1+ile*(MyNodeId()+1);
    from = min(from, m+1);
    to = min(to, m+1);

    Dk(cout << "N(" << MyNodeId() << "): from:" << from << " to:" << to << "\n");
    long long d = 0;
    for(int i=1;i<from;i++) {
        d = max(d, DiscDiameter(i));
    }

    int pos=0;
    for(int i=from;i<to;i++) {
        d = max(d, DiscDiameter(i));
        pos = getPos(d);
        D(cout << "getPos("<<i <<"): " << d <<  " pos:" << pos << "\n");
    }
    int length = to-from;

    if(MyNodeId() == 0) {
        mySend(pos, length);
        length = 0;
        myRecive(pos, length);
        cout << pos << "\n";
    } else {
        myRecive(pos, length);
        mySend(pos, length);
    }

    return EXIT_SUCCESS;
}