Cod sursa(job #1434498)

Utilizator Boss4321Andrei Theodore Marginean Boss4321 Data 10 mai 2015 18:36:50
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <cstdio>
#include <algorithm>
using namespace std;
struct loto
{
    int a,b,c,suma;
};
int ceva(loto x,loto y)
{
    return x.suma<y.suma;
}

int m,n,i,j,k,y[101],nr;
loto x[400000];
int main()
{
    freopen("loto.in","r",stdin);
    freopen("loto.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(i=1; i<=n; i++)
        scanf("%d",&y[i]);
    for(i=1; i<=n; i++)
        for(j=i; j<=n; j++)
            for(k=j; k<=n; k++)
            {
                x[++nr].suma=y[i]+y[j]+y[k];
                x[nr].a=y[i];
                x[nr].b=y[j];
                x[nr].c=y[k];
            }
    sort(x+1,x+nr+1,ceva);
    int p=1,q=nr;
    while(p<=q)
    {
        if(x[p].suma+x[q].suma>m)
            q--;
        else if(x[p].suma+x[q].suma<m)
            p++;
        else
            break;
    }
    if(p>q)
        printf("-1");
    else
        printf("%d %d %d %d %d %d",x[p].a,x[p].b,x[p].c,x[q].a,x[q].b,x[q].c);
}