Cod sursa(job #2120864)

Utilizator VarticeanNicolae Varticean Varticean Data 3 februarie 2018 00:04:27
Problema Loto Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>
using namespace std;

int n,S,v[1005];
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( x > A[mid].s ) 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;
}