Cod sursa(job #1604768)

Utilizator LuurAndrei Florea Luur Data 18 februarie 2016 16:03:49
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.7 kb
#include <fstream>
#include <cstdio>
#include <set>
#include <algorithm>
#define Tmax 101
using namespace std;
ifstream in("loto.in");
ofstream out("loto.out");
typedef long ll;
typedef struct type{
    int a, b, c;
    ll s;
    bool operator < (type const & x)const{
     return s < x.s;
    }
    type operator = (long const & y) const{
        type z; z.a = z.b = z.c = 0; z.s = y;
        return z;
    }
}myType;

template <class T>
struct my_less
{
    bool operator()(const T& x, const T& y) const
    {
        return (x < y);
    }
};

typedef set<myType, my_less<myType> > ls;
typedef set<myType, my_less<myType> >::iterator ls_i;
ll a[Tmax], N, S;
ls s;
bool t = 1;
int main(){
    in>>N>>S;
    for(int i = 1; i <= N; i++){
        in>>a[i];
    }
    for(int i = 1; i <= N; i++){
        for(int j = i; j <= N; j++){
            for(int k = j; k <= N; k++){
                ll _s = a[i] + a[j] + a[k];
                if (_s <= S){
                        myType T; T.a = a[i]; T.b = a[j]; T.c = a[k]; T.s = _s;
                        s.insert(T);
                }
            }
        }
    }
    for(int i = 1; i <= N && t; i++){
        for(int j = i; j <= N && t; j++){
            for(int k = j; k <= N && t; k++){
                ll _s = a[i] + a[j] + a[k];
                if (S-_s >= 0){
                    myType c; c.a = c.b = c.c = 0; c.s = S-_s;
                    ls_i f = s.find(c);
                    if (f != s.end()){ t = 0;
                        out <<a[i]<<' '<<a[j]<<' '<<a[k]<<' '<<(*f).a<<' '<<(*f).b<<' '<<(*f).c<<'\n';
                    }
                }
            }
        }
    }
    if (t) out<<-1<<'\n';
    return 0;
}