Cod sursa(job #962858)
Utilizator | Data | 15 iunie 2013 20:36:35 | |
---|---|---|---|
Problema | Loto | Scor | 20 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 4.29 kb |
#include <fstream>
#include <algorithm>
using namespace std;
long N,S;
long A[105];
long Pos;
long Vec[105*105*105];
int main(void)
{
fstream fin("loto.in",ios::in);
fstream fout("loto.out",ios::out);
fin >> N >> S;
for (long a = 0;a < N;a += 1)
{
fin >> A[a];
}
if (N == 1)
{
if ((A[0] * 6) == S)
{
fout << A[0] << " " << A[0] << " " << A[0] << " " << A[0] << " " << A[0] << " " << A[0];
}
else
{
fout << "-1";
}
}
if (N == 2)
{
long ok = 0;
long a = 0;
long b = 1;
for (long c = 0;c <= 6;c += 1)
{
if (((c * A[a]) + ((6 - c) * A[b])) == S)
{
ok = 1;
long d;
for (d = 0;d < c;d += 1)
{
fout << A[a] << " ";
}
for (;d < 6;d += 1)
{
fout << A[b] << " ";
}
break;
}
}
if (ok == 0)
{
fout << "-1";
}
}
if (N > 2)
{
long ok = 0;
long K;
long Best;
Pos = 0;
for (long a = 0;a < N;a += 1)
{
for (long b = a;b < N;b += 1)
{
for (long c = b;c < N;c += 1)
{
Vec[Pos] = A[a] + A[b] + A[c];
Pos += 1;
}
}
}
sort(Vec + 0,Vec + Pos);
if (ok == 0)
{
for (long a = 0;a < N;a += 1)
{
K = S - (3 * A[a]);
if ((find(Vec + 0,Vec + Pos,K) - Vec) < Pos)
{
fout << A[a] << " " << A[a] << " " << A[a] << " ";
Best = K;
ok = 1;
goto found;
}
}
}
if (ok == 0)
{
for (long a = 0;a < N;a += 1)
{
for (long b = 0;b < N;b += 1)
{
for (long c = 0;c <= 3;c += 1)
{
K = S - (c * A[a]) - ((3 - c) * A[b]);
if ((find(Vec + 0,Vec + Pos,K) - Vec) < Pos)
{
long d;
for (d = 0;d < c;d += 1)
{
fout << A[a] << " ";
}
for (;d < 3;d += 1)
{
fout << A[b] << " ";
}
Best = K;
ok = 1;
goto found;
}
}
}
}
}
/* if (ok == 0)
{
for (long a = 0;a < N;a += 1)
{
for (long b = a;b < N;b += 1)
{
for (long c = b;c <= N;c += 1)
{
K = S - A[a] - A[b] - A[c];
if ((find(Vec + 0,Vec + Pos,K) - Vec) < Pos)
{
fout << A[a] << " " << A[b] << " " << A[c] << " ";
Best = K;
ok = 1;
goto found;
}
}
}
}
}*/
found:;
if (ok == 0)
{
fout << "-1";
}
else
{
for (long a = 0;a < N;a += 1)
{
for (long b = a;b < N;b += 1)
{
for (long c = b;c < N;c += 1)
{
if (Best == (A[a] + A[b] + A[c]))
{
fout << A[a] << " " << A[b] << " " << A[c];
goto finish;
}
}
}
}
}
finish:;
}
fin.close();
fout.close();
return 0;
}