Cod sursa(job #2555919)

Utilizator foodinatorfoodinator foodinator Data 24 februarie 2020 15:42:55
Problema Loto Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int n,s,v[105],i,j,k,m,s1,x,h[10],p;
ifstream in("loto.in");
ofstream out("loto.out");
struct suma
{
    int a,b,val,c;
}w[10005];
int cautarebinara(int st,int x, int dr)
{

    while (st<=dr)
    {
        int mijl=(st+dr)/2;
        if (w[mijl].val==x) {return mijl;}
        if (w[mijl].val<x) st=mijl+1;
        else dr=mijl-1;
    }
    return st;
}
bool comp(suma q, suma r)
{
    return (q.val<r.val);
}
int main()
{
    in>>n>>s;
    for (i=1;i<=n;i++) in>>v[i];
    for (i=1;i<=n;i++)
        for (j=i;j<=n;j++)
        for (k=j;k<=n;k++)
    {
        s1=v[i]+v[j]+v[k];
        if (s1<=s)
        {
            m++;
            w[m].val=s1;
            w[m].a=v[i];
            w[m].b=v[j];
            w[m].c=v[k];
        }

    }
    sort(w+1,w+m+1,comp);
    i=1;k=0;
    while (i<=m&&k==0)
    {
        p=cautarebinara(i,s-w[i].val,m);
        if (w[i].val+w[p].val==s) k=1;
        else i++;
    }
    if (k==0) out<<-1;
    else
    {
        h[1]=w[i].a;h[2]=w[i].b;h[3]=w[i].c;h[4]=w[p].a;h[5]=w[p].b;h[6]=w[p].c;
        sort(h+1,h+7);
        for (i=1;i<=6;i++) out<<h[i]<<" ";
    }
}