Cod sursa(job #916700)

Utilizator gener.omerGener Omer gener.omer Data 16 martie 2013 19:47:42
Problema Loto Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <map> 
 
using namespace std;
 
#define NMAX 105
 
int N, S, A[NMAX];
map<int, bool> m;
 
int main()
{
    freopen("loto.in", "rt", stdin);
    freopen("loto.out", "wt", stdout);
 
    scanf("%d %d", &N, &S);
    for(int i = 0; i < N; i++)
        scanf("%d", &A[i]);
     
    sort(A, A+N);
         
    vector<int> sums;
    for(int i = 0; i < N;++i)
        for(int j = 0; j < N; ++j){
            sums.push_back(A[i]+A[j]);
			m[A[i]+A[j]] = true;
		}
     
    sort(sums.begin(), sums.end());
     
    int sz = sums.size();
     
    for(int i = 0; i < sz; ++i)
        for(int j = 0; j < sz; j++)
        {
            if(m[S - sums[i] - sums[j]])
            {
                int s1, s2, s3;
                s1 = sums[i];
                s2 = sums[j];
                s3 = S - s1 - s2;
                // solution
                for(int i = 0; i < N; ++i){
                    int p = s1 - A[i];
                    if(binary_search(A, A+N, p)){
                        printf("%d %d ", A[i] , p);
                        break;
                    }
                }
                 
                for(int i = 0; i < N; ++i){
                    int p = s2 - A[i];
                    if(binary_search(A, A+N, p)){
                        printf("%d %d ", A[i] , p);
                        break;
                    }
                }
                 
                for(int i = 0; i < N; ++i){
                    int p = s3 - A[i];
                    if(binary_search(A, A+N, p)){
                        printf("%d %d ", A[i] , p);
                        break;
                    }
                }
                return 0;
            }
        }
         
    printf("%d", -1);
     
    return 0;
}