Cod sursa(job #3239227)

Utilizator Bianca2507Negret Bianca Bianca2507 Data 3 august 2024 10:28:41
Problema Loto Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("loto.in");
ofstream cout("loto.out");
struct numar
{
    int a,b,c,s;
} x[1000005];
int n,S,v[105],k;
int cmp(const numar &x,const numar &y)
{
    return x.s<y.s;
}
int main()
{
    cin>>n>>S;
    for(int i=1; i<=n; i++)
        cin>>v[i];
    sort(v+1,v+n+1);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            for(int s=1; s<=n; s++)
                x[++k]= {v[i],v[j],v[s],v[i]+v[j]+v[s]};
    sort(x+1,x+k+1,cmp);
    for(int i1=1; i1<=n; i1++)
        for(int i2=1; i2<=n; i2++)
            for(int i3=1; i3<=n; i3++)
                {
                    int aux=S-v[i1]-v[i2]-v[i3];
                    int st=1,dr=k;
                    while(st<=dr)
                    {
                        int mid=(st+dr)/2;
                        if(x[mid].s==aux)
                        {
                            cout<<v[i1]<<" "<<v[i2]<<" "<<v[i3]<<" "<<x[mid].a<<" "<<x[mid].b<<" "<<x[mid].c;
                            return 0;
                        }
                        else if(x[mid].s>aux)
                            dr=mid-1;
                        else
                            st=mid+1;

                    }
                }
    cout<<-1;
    return 0;
}