Cod sursa(job #2918109)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 9 august 2022 23:09:29
Problema Loto Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>
#define nmax 102
using namespace std;

ifstream f("loto.in");
ofstream g("loto.out");

int v[nmax];
int n,s;
int c[nmax*nmax*nmax];
int main()
{
    f>>n>>s;
    for(int i=0;i<n;i++)
    {
        f>>v[i];
    }
    //cout<<"here\n";
    int n3=n*n*n;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<n;k++)
            {
                c[i+j*n+k*n*n]=v[i]+v[j]+v[k];
            }
        }
    }
    //cout<<"here\n";

    sort(c,c+n3);

    int a=0,b=n3-1;
    bool ok=0;
    for(;a<n3&&a<=b;a++)
    {
        while(c[a]+c[b]>s) b--;
        if(c[a]+c[b]==s)
        {
            ok=1;
            break;
        }
    }
    //cout<<"here\n";
    if(!ok)
    {
        g<<-1;
        return 0;
    }
    bool low=0,up=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            for(int k=0;k<n;k++)
            {
                if(!low&&v[i]+v[j]+v[k]==c[a])
                {
                    low=1;
                    g<<v[i]<<' '<<v[j]<<' '<<v[k]<<' ';
                }
                else if(!up&&v[i]+v[j]+v[k]==c[b])
                {
                    up=1;
                    g<<v[i]<<' '<<v[j]<<' '<<v[k]<<' ';
                }
                if(low&&up)
                {
                    return 0;
                }
            }
        }
    }
    //cout<<"here\n";


    return 0;
}