Pagini recente » Cod sursa (job #2496389) | Cod sursa (job #1425436) | Cod sursa (job #222683) | Cod sursa (job #708) | Cod sursa (job #2788328)
#include <bits/stdc++.h>
using namespace std;
/**
*/
ifstream fin("loto.in");
ofstream fout("loto.out");
struct trei
{
short i, j, k;
int suma; /// suma = a[i]+a[j]+a[k]
bool operator<(const trei w) const
{
return suma < w.suma;
}
};
int a[103], n, s;
trei t[200000];
int nt;
int CautBin(int x, int st, int dr)
{
int mid;
if (x < 0) return -1;
while (st <= dr)
{
mid = (st + dr) / 2;
if (t[mid].suma == x) return mid;
if (t[mid].suma < x) st = mid + 1;
else dr = mid - 1;
}
return -1;
}
int main()
{
short i, j, k;
int x, p;
trei w;
fin >> n >> s;
for (i = 1; i <= n; i++)
fin >> a[i];
for (i = 1; i <= n; i++)
for (j = i; j <= n; j++)
for (k = j; k <= n; k++)
{
w.i = i; w.j = j; w.k = k;
w.suma = a[i] + a[j] + a[k];
t[nt++] = w;
}
sort(t, t + nt);
for (i = 0; i < nt; i++)
{
x = t[i].suma;
/// cauta binar daca in t[] exista elementul care are
/// suma s - x > 0
p = CautBin(s - x, i, nt - 1);
if (p >= 0)
{
fout << t[i].i << " " << t[i].j << " " << t[i].k << " ";
fout << t[p].i << " " << t[p].j << " " << t[p].k << "\n";
fout.close();
return 0;
}
}
fout << "-1\n";
fout.close();
return 0;
}