Cod sursa(job #2154151)

Utilizator trz59lollMurariu Iulian trz59loll Data 6 martie 2018 18:56:43
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include<bits/stdc++.h>
#define pb push_back
#define maxn 5010
using namespace std;
ifstream f("loto.in");
ofstream g("loto.out");
int n,a[maxn],s,auxs;

queue < int > q;

void caut01(int x)
{
    size_t st = 1,dr = n,m;
    while(st <= dr)
    {
        m = (st + dr) / 2;
        if(a[m] == x)
        {
          q.push(a[m]);
          auxs -= a[m];
          return ;
        }
        else
        {
            if(a[m] > x)
                dr = m - 1;
            else
                st = m + 1;
        }
    }
    auxs -= a[m];
    q.push(a[m]);
}

int main()
{
    size_t i;
    f>>n>>s;
    for(i = 1; i <= n; i ++)
        {
            f>>a[i];
            if(6 * a[i] == s)
                {
                    for(size_t j = 1; j <= 6; j ++)
                    {
                        g<<a[i]<<" ";
                    }
                    return 0;
                }
        }
    sort (a + 1, a + n + 1);
    if(a[n] * 6 < s)
        {
            g<<"-1";
            return 0;
        }
    auxs = s;
    while(auxs != 0)
    {
        for(i = 6; i >= 1; i --)
        {
            caut01(auxs / i);
            if(auxs < 0)
                break;
        }
    }

    while(!q.empty())
    {
        size_t x;
        x = q.front();
        q.pop();
        g<<x<<" ";
    }
}