Cod sursa(job #3239226)

Utilizator Bianca2507Negret Bianca Bianca2507 Data 3 august 2024 10:22:21
Problema Loto Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("loto.in");
ofstream cout("loto.out");
struct numar
{
    int a,b,s;
} x[10005];
int n,S,v[105],k;
int cmp(numar x,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++)
            x[++k]= {v[i],v[j],v[i]+v[j]};
    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++)
                for(int i4=1; i4<=n; i4++)
                {
                    int aux=S-v[i1]-v[i2]-v[i3]-v[i4];
                    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]<<" "<<v[i4]<<" "<<x[mid].a<<" "<<x[mid].b;
                            return 0;
                        }
                        else if(x[mid].s>aux)
                            dr=mid-1;
                        else
                            st=mid+1;

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