Cod sursa(job #2557180)

Utilizator Codrut112Codrut Copas Codrut112 Data 25 februarie 2020 16:46:21
Problema Loto Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb

#include <bits/stdc++.h>

using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
struct loto
{
    int suma;
    int i,j,l;
};
bool comp(loto a,loto b)
{
    return a.suma<b.suma;

}
loto v[1000001];
int n,s,i,j,l,x,ok,k,w[101],nr,u[7];
int main()
{
    f>>n;
    f>>s;
    for(i=1; i<=n; i++)

        f>>w[i];


    for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
            for(l=1; l<=n; l++)
            {
                if(w[i]+w[j]+w[l]<s)
                {
                    nr++;
                    v[nr].suma=w[i]+w[j]+w[l];
                    v[nr].i=i;
                    v[nr].j=j;
                    v[nr].l=l;
                }
            }
    sort(v+1,v+nr+1,comp);

    for(i=1; i<=nr; i++)
    {
        if(ok==1) break;
        if(v[nr].suma+v[i].suma<s)continue;
        int sumabuna=s-v[i].suma;
        int dr=nr;
        int st=1;
        while(st<=dr)
        {
            if(ok==1)break;
            int  mid=(st+dr)/2;
            if(v[mid].suma==sumabuna)

            {
                u[1]=w[v[i].i];
                u[2]=w[v[i].j];
                u[3]=w[v[i].l];
                u[4]=w[v[mid].i];
                u[5]=w[v[mid].j];
                u[6]=w[v[mid].l];
                sort(u+1,u+6+1);
                for(l=1; l<=6; l++)g<<u[l]<<" ";
                ok=1;
            }
            else
            {
                if(v[mid].suma<sumabuna)st=mid+1;
                else dr=mid-1;
            }

        }



    }if(ok==0)g<<-1;

}