Cod sursa(job #792048)

Utilizator idainetJohn Doe idainet Data 26 septembrie 2012 12:52:02
Problema Loto Scor 70
Compilator cpp Status done
Runda asem-etapa1 Marime 1.96 kb
//
//  main.cpp
//  a1
//
//  Created by abc on 9/20/12.
//  Copyright (c) 2012 abc. All rights reserved.
//

# include <iostream>
# include <cstdio>
# include <cstdlib>
# include <iomanip>
# include <cmath>
# include <map>
# include <vector>
# include <set>
# include <algorithm>

using namespace std;

# define ISALPHA(Q) (('a' <= Q && Q <= 'z') || ('A' <= Q && Q <= 'Z'))
# define ISDIGIT(a) ('0' <= a  && a <= '9')
# define TODIGIT(a) (a - '0')

# define LIKELY(a)   (__builtin_expect((a), 1))
# define UNLIKELY(a) (__builtin_expect(!!(a), 0))

typedef unsigned char U8;
typedef long long LONG;

void init()
{
    freopen("loto.in", "r", stdin);
    freopen("loto.out", "w", stdout);
}

template <typename T> void read(vector<T> &vec)
{
    for(size_t i=0; i<vec.size(); ++i) {
        cin >> vec[i];
    }
}

typedef struct {
    int a,b,c;
} Cont_t;

int main(int argc, const char * argv[])
{
    init();
    
    int N, S; cin >> N >> S;
    vector<int> vec(N); read(vec);
    //sort(vec.begin(), vec.end());
    
    map<int, Cont_t> conv;    
    for(int i=0; LIKELY(i<N); ++i)
    {
        for(int k=0; LIKELY(k<N); ++k)
        {
            for(int j=0; LIKELY(j<N); ++j)
            {
                conv[vec[i]+vec[k]+vec[j]] = Cont_t { vec[i], vec[k], vec[j] };
            }
        }
    }
    
    map<int, Cont_t>::iterator left = conv.begin();
    map<int, Cont_t>::reverse_iterator right = conv.rbegin();
    for(; left != conv.end() && right != conv.rend() && left->first <= right->first; )
    {
        int tS = left->first + right->first;
        if (tS == S)
        {
            printf("%d %d %d %d %d %d", left->second.a, left->second.b, left->second.c,
                                        right->second.a, right->second.b, right->second.c);
            return 0;
        }
        else if (tS < S) ++left;
        else if (tS > S) ++right;
        else {
            fprintf(stderr, "WTF?!");
            return 1;
        }
    }
    
    cout << -1;
    
    return 0;
}