Pagini recente » Cod sursa (job #1087813) | Cod sursa (job #1855728) | Cod sursa (job #3277048) | Cod sursa (job #2630918) | Cod sursa (job #616652)
Cod sursa(job #616652)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
struct halfsum
{
int s, x, y, z;
} b[1000001];
void merge (int a, int c)
{
halfsum temp[1000001];
long left = a, left_end = (a+c)/2, mid = left_end+1, mid_end = c, pos = a;
while (left <= left_end && mid <= mid_end)
{
if(b[left].s < b[mid].s)
temp[pos++] = b[left++];
else
temp[pos++] = b[mid++];
}
while (left <= left_end)
temp[pos++] = b[left++];
while (mid <= mid_end)
temp[pos++] = b[mid++];
for (int i = a; i < pos; ++i)
b[i] = temp[i];
}
void sort (int z, int x)
{
if (x - z <= 0)
return;
sort (z, (z+x)/2);
sort ((z+x)/2 + 1, x);
merge (z, x);
}
int main()
{
ifstream in("loto.in");
vector <int> a;
int n, s, temp, x = -1, prev_comp = 0;
in >> n >> s;
for (int i = 0; i < n; ++i)
{
in >> temp;
a.push_back(temp);
}
in.close();
sort(a.begin(), a.begin()+n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k)
{
temp = a[i] + a[j] + a[k];
//if (temp > s || temp <= prev_comp)
// continue;
b[++x].s = prev_comp = temp;
b[x].x = a[i];
b[x].y = a[j];
b[x].z = a[k];
//cout << b[x].x << " " << b[x].y << " " << b[x].z << " = " << b[x].s << "\n";
}
sort(0,x);
int l = 0, sum;
while (l <= x)
{
sum = s - b[l].s - b[x].s;
if (sum == 0)
{
ofstream out("loto.out");
out << b[l].x << " " << b[l].y << " " << b[l].z << " " << b[x].x << " " << b[x].y << " " << b[x].z;
out.close();
return 0;
}
else
if (sum < 0)
--x;
else
++l;
}
ofstream out("loto.out");
out << -1;
out.close();
return 0;
}