Cod sursa(job #308886)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 28 aprilie 2009 20:17:37
Problema Loto Scor 100
Compilator cpp Status done
Runda tot Marime 1.12 kb
#include<cstdio>   
#include<algorithm>   
#include<vector>   
using namespace std;   
int N,v[105],S;   
struct Numere   
{   
    int a,b,c,suma;   
};   
vector <Numere> a;   
int cmp(const Numere &x,const Numere &y)   
{   
    return x.suma<y.suma;
}
int main()
{   
    freopen("loto.in","r",stdin);   
    freopen("loto.out","w",stdout);   
    int i,j,k;   
    scanf("%d %d",&N,&S);   
    for(i=1;i<=N;i++) scanf("%d",&v[i]);   
    for(i=1;i<=N;i++)   
        for(j=i;j<=N;j++)   
            for(k=j;k<=N;k++)   
            {   
                Numere x;   
                x.a=v[i];x.b=v[j];x.c=v[k];   
                x.suma=x.a+x.b+x.c;   
                a.push_back(x);   
            }   
    sort(a.begin(),a.end(),cmp);
    i=0;j=a.size()-1;   
    while(i<=j)   
    {   
        int dif=S-a[i].suma;   
        while(a[j].suma>dif) j--;   
        if(a[j].suma==dif)   
        {   
            printf("%d %d %d %d %d %d\n",a[i].a,a[i].b,a[i].c,a[j].a,a[j].b,a[j].c);   
            return 0;   
        }   
        i++;   
    }   
    printf("-1\n");   
    return 0;   
}