Cod sursa(job #1112177)

Utilizator denis_tdrdenis tdr denis_tdr Data 19 februarie 2014 15:24:54
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct tr{
    int a,b,c,s;
    tr(){};
    tr(int _a, int _b, int _c){s=(a=_a)+(b=_b)+(c=_c);};
};
int n, s, x, i;
vector<int> v;
vector<tr> t;
vector<tr>::iterator it;
tr tr2;
bool cmp(int a, int b)
{return a>b;}
bool cmp2(tr a, tr b)
{return a.s<b.s;}
int main(){
    ifstream f("loto.in");
    ofstream g("loto.out");
    f>>n>>s;
    while(f>>x)v.push_back(x);

    make_heap(v.begin(), v.end());
    sort_heap(v.begin(), v.end());

    for(i=0;i<n;i++)
        for(int j=i;j<n;j++)
            for(int k=j;k<n;k++)
                t.push_back(tr(v[i], v[j], v[k]));

    make_heap(t.begin(), t.end(), cmp2);
    sort_heap(t.begin(), t.end(), cmp2);

    for(i=0;i<t.size();i++)
    {
        tr2.s=s-t[i].s;
        it=lower_bound(t.begin(), t.end(), tr2, cmp2);
        if(t[i].s+it->s==s)
            g<<t[i].a<<" "<<t[i].b<<" "<<t[i].c<<" "<<it->a<<" "<<it->b<<" "<<it->c<<" ", i=t.size()+1;
    }
    if(i==t.size())
        g<<"-1";
    g.close();
    f.close();
    return 0;
}