Cod sursa(job #2623673)

Utilizator YesterdayIlie George Ciprian Yesterday Data 3 iunie 2020 16:22:53
Problema Loto Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("loto.in");
ofstream out("loto.out");
int i,n,s,vect[1000001],j,o,val,bilet,poz;
bool ok;
struct loto
{
  int x,y,z,suma;
}v[10000001];
//bool compar(loto nr1,loto nr2)
//{
    //return nr1.suma<nr2.suma;
//}
int compar(loto a,loto b)
{
    if(a.suma<b.suma)
        return 1;
    else return 0;
}

int caut(int s,int d,int x)
{
    if(s>d)
        return -1;
    else
    {
        int m=(s+d)/2;
        if(x == v[m].suma)
            return m;
        if(x < v[m].suma)
            return caut(s,m-1,x);
        if(x > v[m].suma)
            return caut(m+1,d,x);
    }
}

int main()
{
    f>>n;
    f>>s;
    int nr=0;
    for(i=1;i<=n;i++)
        f>>vect[i];
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            for(o=j;o<=n;o++)
    {
        v[nr].suma=vect[i]+vect[j]+vect[o];
        v[nr].x=vect[i];
        v[nr].y=vect[j];
        v[nr].z=vect[o];
        nr++;
    }
    sort(v,v+nr,compar);

    for(i=0;i<nr;i++)
    {
         ok=0;
        bilet=0;
        val=s-v[i].suma;
        if(caut(0,nr-1,val)!=-1)
            {bilet=val;
            poz=caut(0,nr-1,val);
            }
        if(v[i].suma+bilet == s)
            {out<<v[i].x<<" "<<v[i].y<<" "<<v[i].z<<" ";
            out<<v[poz].x<<" "<<v[poz].y<<" "<<v[poz].z<<" ";
            //cout<<v[i].suma<<" "<<bilet;

            ok=1;
            break;
            }
    }
    if(ok==0)
        out<<-1;

     // for(i=0;i<nr;i++)
       // cout<<v[i].suma;
    return 0;
}