Cod sursa(job #2120859)

Utilizator VarticeanNicolae Varticean Varticean Data 2 februarie 2018 23:32:12
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

int S,n,v[105];
struct rec
{
      int a,b,c,s;
}A[500005];
bool cmp( const rec &u , const rec &v)
{
        return u.s < v.s;
}
int  caut_bin( int st, int dr, int x  )
{
     int mid;
     while ( st <= dr )
     {
          mid = ( st + dr ) / 2;
          if( A[mid].s == x ) return mid;
          if( A[mid].s < x ) st = mid +1; else dr = mid -1;

     }
     return -1;
}
int main()
{
     ifstream in("loto.in");
     ofstream out("loto.out");
     in >> n >> S;
     for(int i=1; i<=n; i++) in >> v[i];

     int sz = 0;
     for(int i=1; i<=n; i++)
          for(int j=i; j<=n; j++)
            for(int k=j; k<=n; k++)
          {
             A[++sz].a = v[i];
             A[sz].b = v[j];
             A[sz].c = v[k];
             A[sz].s = v[i] + v[j] + v[k];
          }
     sort( A+1, A+sz+1,cmp);
     for(int i=1; i<=sz; i++)
     {
          int poz = caut_bin(1,sz,S-A[i].s);
          if( poz != -1 )
          {
               out << A[i].a << ' ' <<  A[i].b << ' ' << A[i].c << ' ' << A[poz].a << ' ' << A[poz].b << ' ' << A[poz].c;
               return 0;
          }
     }
     out  << -1;
    return 0;
}