Pagini recente » Cod sursa (job #2266226) | Cod sursa (job #1957665) | Cod sursa (job #2245171) | Cod sursa (job #1238088) | Cod sursa (job #1789091)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
struct suma
{
suma(int tx, int ty, int tz)
{
x = tx;
y = ty;
z = tz;
s = tx + ty + tz;
}
suma()
{
}
int x, y, z;
int s;
};
ifstream in("loto.in");
ofstream out("loto.out");
int n, s;
int v[105];
suma sum[105 * 105 * 105];
void citire()
{
in >> n >> s;
for(int i = 1; i <= n; ++i)
in >> v[i];
}
bool comp(suma x, int val)
{
if(x.s < val) return true;
return false;
}
bool cmp(suma x, suma y)
{
if(x.s < y.s)
return true;
return false;
}
void rezolvare()
{
int pos = 0;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
for(int k = 1; k <= n; ++k)
{
sum[pos] = suma(v[i], v[j], v[k]);
pos++;
}
sort(sum, sum + pos, cmp);
int rasp[6];
rasp[0] = -1;
int rest, st, dr, mid;
for(int i = 0; i < pos; ++i)
{
rest = s - sum[i].s;
st = 0;
dr = pos - 1;
while(st <= dr)
{
mid = (st + dr) / 2;
if(sum[mid].s == rest)
{
rasp[0] = sum[i].x;
rasp[1] = sum[i].y;
rasp[2] = sum[i].z;
rasp[3] = sum[mid].x;
rasp[4] = sum[mid].y;
rasp[5] = sum[mid].z;
break;
}
else if(sum[mid].s > rest)
dr = mid - 1;
else
st = mid + 1;
}
}
if(rasp[0] != -1)
{
for(int i = 0; i < 6; ++i)
out << rasp[i] << " ";
}
else
out << -1;
}
int main()
{
citire();
rezolvare();
return 0;
}