Pagini recente » Cod sursa (job #239892) | Cod sursa (job #1980043) | Cod sursa (job #1764110) | Cod sursa (job #305893) | Cod sursa (job #2224488)
#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 pos){
int st = 1, dr = lim, mij;
while(st <= dr){
mij = (st + dr) / 2;
if(sum[mij].s < difer)
st = mij + 1;
else
dr = mij - 1;
}
if(sum[pos].s + sum[st + 1].s == target)
return st + 1;
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);
for(int i = 1; i <= len; ++i){
int dif = target - sum[i].s, rez = cauta(dif, len, i);
if(rez != -1){
fout << sum[i].a << " " << sum[i].b << " " << sum[i].c << " " << sum[rez].a << " " << sum[rez].b << " " << sum[rez].c << "\n";
return 0;
}
}
fout << -1;
return 0;
}