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
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <bits/stdc++.h>
#include "krazki.h"
#include "message.h"
using namespace std;
/*namespace {

int pipe_height, number_of_discs;
std::vector<long long int> pipe, discs;

void Init() {
  static bool initialized = false;
  if (!initialized) {
    initialized = true;
    std::cin >> pipe_height >> number_of_discs;
    pipe.resize(pipe_height);
    for (int i = 0; i < pipe_height; i++) {
      std::cin >> pipe[i];
    }
    discs.resize(number_of_discs);
    for (int i = 0; i < number_of_discs; i++) {
      std::cin >> discs[i];
    }
  }
}

}  // namespace

int PipeHeight() {
  Init();
  return pipe_height;
}

int NumberOfDiscs() {
  Init();
  return number_of_discs;
}

long long int HoleDiameter(long long int i) {
  Init();
  assert(1 <= i && i <= pipe_height);
  return pipe[i - 1];
}

long long int DiscDiameter(long long int j) {
  Init();
  assert(1 <= j && j <= number_of_discs);
  return discs[j - 1];
}*/

long long tab[10000002];
int main()
{
	int instancje=NumberOfNodes(), numer=MyNodeId();
	int n=PipeHeight(), m=NumberOfDiscs();
	int ilosc=n/instancje;
	int pocz=ilosc*numer+1, kon=ilosc*(numer+1);
	if (ilosc==0 && numer!=0)
        return 0;
    if (ilosc==0)
    {
        pocz=1;
        kon=n;
    }
	if (numer==instancje-1)
        kon=n;
    //cout << "URUCHOMIONO " << numer << " " << pocz << " " << kon << "\n";
	tab[pocz-pocz]=HoleDiameter(pocz);
	for (int i=pocz+1; i<=kon; i++)
	{
		tab[i-pocz]=min(tab[i-1-pocz], HoleDiameter(i));
	}
	long long pom=-1;
	if (numer!=0)
	{
		int a=Receive(numer-1);
		pom=GetLL(numer-1);
		if (pom<tab[kon-pocz])
			tab[kon-pocz]=pom;
	}
	if (numer<instancje-1 && ilosc>0)
	{
		PutLL(numer+1, tab[kon-pocz]);
		Send(numer+1);
	}
	else
	{
		PutLL(numer, 1);
		Send(numer);
	}
	if (pom!=-1)
	{
		for (int i=pocz; i<=kon; i++)
		{
			if (tab[i-pocz]>pom)
				tab[i-pocz]=pom;
            //cout << i << " " << tab[i] << "t\n";
		}
	}
	int a=Receive(-1);
	long long kr=GetLL(a);
	if (kr==-1)
	{
		//przeslij dalej konczenie
		if (numer!=0)
		{
			PutLL(numer-1, -1);
			Send(numer-1);
		}
		//cout << numer << "KONCZE\n";
		return 0;
	}
	//cout << numer << " " << kr << "\n";
	//cout << DiscDiameter(kr) << " " << tab[kon] << "\n";
	int akt=kon;
	while (kr<=m && akt>=pocz)
	{
		if (DiscDiameter(kr)<=tab[akt-pocz])
		{
			kr++;
		}
		akt--;
		//cout << numer << " " << akt << " " << kr << "\n";
	}
	if (kr>m)
	{
	    if (numer>0)
            cout << akt+1;
		kr=-1;
	}
	if (numer==0)
	{
		if (kr!=-1)
		{
			cout << 0;
		}
		else
			cout << akt+1;
	}
	else
	{
		PutLL(numer-1, kr);
		Send(numer-1);
	}
	//cout << numer << " " << akt << " " << kr << "k\n";
	return 0;
}