Cod sursa(job #2703184)

Utilizator xXoctavianXxStanescu Matei Octavian xXoctavianXx Data 7 februarie 2021 15:45:39
Problema Loto Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.39 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("loto.in");
ofstream fout("loto.out");

struct tri
{
    int a,b,c,sum;
};

int n,s,nr;
tri three[1000010];
int v[102];

bool comp(tri x, tri y)
{
    return x.sum<y.sum;
}

int main()
{
    fin>>n>>s;
    for(int i=0; i<n; i++)
    {
        fin>>v[i];
    }
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<n; j++)
        {
            for(int k=0; k<n; k++)
            {
                three[nr].sum=v[i]+v[j]+v[k];
                three[nr].a=v[i];
                three[nr].b=v[j];
                three[nr++].c=v[k];
            }
        }
    }
    sort(three, three+nr, comp);
    for(int i=0; i<nr; i++)
    {
        int target = s - three[i].sum;
        int st=0;
        int dr=nr-1;
        int res =0;
        while(st<dr)
        {
            int mij=(st+dr)/2;
            if(three[mij].sum == target)
            {
                res=mij;
                break;
            }
            else if(three[mij].sum<target)
            {
                st=mij+1;
            }
            else dr=mij;
        }
        if(target == three[res].sum)
        {
            fout<<three[i].a<<" "<<three[i].b<<" "<<three[i].c<<" "
            <<three[res].a<<" "<<three[res].b<<" "<<three[res].c;
            return 0;
        }
    }
    fout<<"-1";
    return 0;
}