Cod sursa(job #2100139)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 5 ianuarie 2018 11:59:17
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("loto.in");
ofstream cout("loto.out");
const int nmax=100;
int n,s,v[nmax+5];
struct data
{
    int a,b,c,sum;
};
bool operator<(data x,data y)
{
    return x.sum<y.sum;
}
data sol[nmax*nmax*nmax+5];
int y;
int cauta(int val)
{
    int st=1,dr=y,med;
    while(st<dr)
    {
        med=(st+dr)/2;
        if(sol[med].sum<val)
            st=med+1;
        else
            dr=med;
    }
    if(sol[st].sum!=val)
        return -1;
    return st;
}
int main()
{
    cin>>n>>s;
    for(int i=1;i<=n;i++)
        cin>>v[i];
    for(int i=1;i<=n;i++)
        for(int j=i;j<=n;j++)
            for(int l=j;l<=n;l++)
            {
                y++;
                sol[y]={v[i],v[j],v[l],v[i]+v[j]+v[l]};
            }
    sort(sol+1,sol+y+1);
    for(int i=1;i<=y;i++)
    {
        int location=cauta(s-sol[i].sum);
        if(location!=-1)
        {
            cout<<sol[i].a<<" "<<sol[i].b<<" "<<sol[i].c<<" ";
            cout<<sol[location].a<<" "<<sol[location].b<<" "<<sol[location].c<<" ";
            return 0;
        }
    }
    cout<<-1;
    return 0;
}