Cod sursa(job #857236)

Utilizator stoicatheoFlirk Navok stoicatheo Data 17 ianuarie 2013 17:01:05
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
ifstream fin("loto.in");
ofstream fout("loto.out");
 
struct asd
{
   int a, b, c, sum;
}
s[500001];
 
bool comp(asd a, asd b)
{
    return a.sum < b.sum;
}
 
int main()
{
    int sum, n, v[109], nr = 0;
 
    fin >> n >> sum;
    for(int i = 1; i <= n; i++)
        fin >> v[i];
 
    for(int i = 1; i <= n; i++)
        for(int j = i; j <= n; j++)
            for(int k = j; k <= n; k++)
            {
                s[++nr].sum = v[i] + v[j] + v[k];
                s[nr].a = v[i];
                s[nr].b = v[j];
                s[nr].c = v[k];
            }
 
    sort(s + 1, s + nr + 1, comp);
    int high = nr, low = 1;
 
    while(low <= high)
    {
        if((s[low].sum + s[high].sum) > sum)
            high--;
 
        else if(s[low].sum + s[high].sum < sum)
            low++;
 
        else
            break;
    }
 
    if(low > high)
        fout << -1;
 
    else
        fout << s[low].a << " " << s[low].b << " " << s[low].c << " " << s[high].a << " " << s[high].b << " " << s[high].c;
 
 
    fin.close();
    fout.close();
    return 0;
}