Pagini recente » Cod sursa (job #820614) | Cod sursa (job #1783383) | Cod sursa (job #1276239) | Cod sursa (job #1248968) | Cod sursa (job #2224484)
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 105, MAXSUMS = 1000005;
struct perm{
int s, a, b, c;
}sum[MAXSUMS];
int num[MAXN];
int n, target;
bool comp(perm x, perm y){
return x.s <= y.s;
}
int cauta(int difer, int lim){
int st = 1, dr = lim, mij;
while(st <= dr){
mij = (st + dr) / 2;
if(sum[mij].s < difer)
st = mij + 1;
else if(sum[mij].s > difer)
dr = mij - 1;
else
return mij;
}
return -1;
}
int main()
{
ifstream fin("loto.in");
ofstream fout("loto.out");
fin >> n >> target;
for(int i = 1; i <= n; ++i)
fin >> num[i];
int len = 0;
for(int i = 1; i <= n; ++i){
for(int j = i; j <= n; ++j){
for(int k = j; k <= n; ++k){
sum[++len].s = num[i] + num[j] + num[k];
sum[len].a = num[i];
sum[len].b = num[j];
sum[len].c = num[k];
}
}
}
sort(sum + 1, sum + len + 1, comp);
int found = 0;
for(int i = 1; i <= len && !found; ++i){
int dif = target - sum[i].s;
int rez = cauta(dif, len);
if(rez != -1){
fout << sum[i].a << " " << sum[i].b << " " << sum[i].c << " " << sum[rez].a << " " << sum[rez].b << " " << sum[rez].c << "\n";
found = 1;
}
}
if(!found)
fout << -1;
return 0;
}