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

using namespace std;

int main() {
  
    int wysokosc = PipeHeight();
    int ile_krazkow = NumberOfDiscs();   
    
 if(ile_krazkow > wysokosc){ cout << 0 << '\n' ;}
 else{     
    int N = NumberOfNodes();
    vector<long long int> rurka(N);
    //if(N>=wysokosc){
    int ile = wysokosc/N;//jaka wysokosc przypada na dany wezel
    int reszta=wysokosc-ile*N;
    int start = ile * MyNodeId();//0, 100, 200
    int end;
    if(MyNodeId() != N-1){int end = ile * (MyNodeId() + 1);}//100, 200, 300
    else{end=wysokosc;}
    
    
    rurka[start]=HoleDiameter(start);
  for(int i=start+1; i<end;i++){
     rurka[i]=HoleDiameter(i);
     if(rurka[i-1]<rurka[i]) {
        rurka[i]=rurka[i-1];
      }
    }
    int64_t szer;
    if(MyNodeId() != NumberOfNodes() -1){
       PutLL(MyNodeId() +1,rurka[end-2]);
       Send(MyNodeId() +1);
       Receive(MyNodeId() -1);
       szer=GetLL( MyNodeId() -1);
       for(int i=start; i<end;i++){
          if(rurka[i]>szer){rurka[i]=szer;}
          else{break;}
       }
    }
    //mamy stworzona rurke, teraz wrzucamy krazki
    
       
    int ile_przeslac=0;
    int ost = end-1; 
    int64_t dl_k;
    for(int i=start; i<end; i++){
      if(i<ile_krazkow){
         dl_k=DiscDiameter(i+1);
         for(int k=ost; k>=start; k--){
            if(dl_k>rurka[k]){
                ile_przeslac++;
                ost--;
            }
            else{ break;}
         }
      }
      else{break;}
    }
    int zostalo= ost-start;
    int64_t suma=0, plus=0, odp=0;
    int x=5000;
    for(int i=N-1; i>=0; i--){
       if(MyNodeId() ==i ){
         if(MyNodeId() != N-1) {Receive(MyNodeId() +1);
         plus=GetLL( MyNodeId() +1);}
         suma = suma -zostalo+plus;
         if(suma>0){
         PutLL(MyNodeId() -1,suma);
         Send(MyNodeId() -1);
         }
         else{suma=0;
         PutLL(MyNodeId() -1,0);
         Send(MyNodeId() -1);}
        }
        if(ost==end-1 and plus==0){x=i+1;}
        else if (i==0 and ost-plus>=0){x=6000; cout << ost-plus << '\n';}
    }
    if(x==5000){cout << 0 << '\n';}
    if(MyNodeId() == x){
    odp=ost-plus;
    cout << odp << '\n';
    }
    

}
return 0;
}