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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <vector>
#include "kanapka.h"
#include "message.h"

using namespace std;

long long id, li, ps, ks, ds, dc, lws, ss, sma, swl, swp, zs, mwp, mwl, mop, mol, wyn, wp;
vector <long long>war;
vector <long long>sl;
vector <long long>sp;
vector <long long>ml;
vector <long long>mp;
/*
void Receive(int instancja)
{
	cout << "wczytaj dane z instancji nr " << instancja << "\n";
}
long long GetLL(int instancja)
{
	long long war;
	cin >> war;
	return war;
}
void PutLL(int instancja, long long war)
{
	cout << war << " ";
}
void Send(int instancja)
{
	cout << "\npowyzsze dane wpisz do instanci nr " << instancja << "\n";
}
long long GetN()
{
	cout << "podaj dlugosc kanapki\n";
	long long war;
	cin >> war;
	return war;
}
long long GetTaste(long long indeks)
{
	cout << "podaj smakowitosc kanapki w indeksie " << indeks << "\n";
	long long war;
	cin >> war;
	return war;
}
int MyNodeId()
{
	int war;
	cout << "podaj moj numer instancji: ";
	cin >> war;
	return war;
}
int NumberOfNodes()
{
	int war;
	cout << "Podaj liczbe instancji: ";
	cin >> war;
	return war;
}*/
int main()
{
	id = MyNodeId();
	li = NumberOfNodes();
	dc = GetN();
	ds = (dc / li);
	ds++;
	lws = (dc / ds);
	lws++;
	if (dc % ds == 0)lws--;
	//cout << ds << " " << lws << "\n";
	if (id < lws)
	{
		ps = ds * id;
		ks = ds * (id + 1) - 1;
		if (id == lws - 1)
		{
			ks = dc - 1;
			ds = (dc - 1) % ds + 1;
		}
		ss = 0;
		//cout << ps << " " << ks << "\n";
		for (int i = ps; i <= ks; i++)		
		{
			sma = GetTaste(i);
			war.push_back(sma);
			ss += sma;
			sl.push_back(ss);
		}
		//koniec etapu 1
		if (id == 0)
		{
			PutLL(id + 1, ss);
			Send(id + 1);
			Receive(id + 1);
			swp = GetLL(id + 1);
			swl = 0;
		}
		if(id == lws - 1)
		{
			PutLL(id - 1, ss);
			Send(id - 1);
			Receive(id - 1);
			swl = GetLL(id - 1);
			swp = 0;
		}
		if (id > 0 && id < lws - 1)
		{
			Receive(id - 1);
			swl = GetLL(id - 1);
			PutLL(id + 1, swl + ss);
			Send(id + 1);
			Receive(id + 1);
			swp = GetLL(id + 1);
			PutLL(id - 1, swp + ss);
			Send(id - 1);
		}
		//koniec etapu 2
		zs = swp;
		mop = 0;
		mol = 0;
		for (int i = ks; i >= ps; i--)
		{
			zs += war[i - ps];
			sp.push_back(zs);
			sl[ks - i] += swl;
			if (sl[ks - i] > mol)mol = sl[ks - i];
			if (zs > mop)mop = zs;
			ml.push_back(mol);
			mp.push_back(mop);
		}
		//koniec etapu 3.0
		if (id == 0)
		{
			mwl = 0;
			PutLL(id + 1, mol);
			Send(id + 1);
			Receive(id + 1);
			mwp = GetLL(id + 1);
		}
		if (id == lws - 1)
		{
			mwp = 0;
			PutLL(id - 1, mop);
			Send(id - 1);
			Receive(id - 1);
			mwl = GetLL(id - 1);
		}
		if (id > 0 && id < lws - 1)
		{
			Receive(id - 1);
			mwl = GetLL(id - 1);
			if (mwl > mol)PutLL(id + 1, mwl);
			else PutLL(id + 1, mol);
			Send(id + 1);
			Receive(id + 1);
			mwp = GetLL(id + 1);
			if (mwp > mop)PutLL(id - 1, mwl);
			else PutLL(id - 1, mol);
			Send(id - 1);
		}
		//koniec etapu 3.1
		wyn = 0;
		for (int i = ps; i <= ks; i++)
		{
			if (mwl > ml[i - ps])ml[i - ps] = mwl;
			if (mwp > mp[i - ps])mp[i - ps] = mwp;
		}
		for (int i = ps; i <= ks; i++)
		{
			if (ml[i - ps] + mp[ks - i] > wyn)wyn = ml[i - ps] + mp[ks - i];
		}
		//koniec etapu 4.0
		if (id == 0)
		{
			Receive(id + 1);
			wp = GetLL(id + 1);
			if (wp > wyn)wyn = wp;
			cout << wyn;
		}
		if (id == lws - 1)
		{
			PutLL(id - 1, wyn);
			Send(id - 1);
		}
		if (id > 0 && id < lws - 1)
		{
			Receive(id + 1);
			wp = GetLL(id + 1);
			if (wp > wyn)wyn = wp;
			PutLL(id - 1, wyn);
			Send(id - 1);
		}
	}
	//system("PAUSE");
}