Cod sursa(job #2154214)

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

queue < long long > 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;
    size_t loc = 1,start = 6;
    while(auxs != 0)
    {
        for(i = start; i >= 1; i --)
        {
            caut01(auxs / i);
            if(auxs < 0)
                break;
        }

        if(auxs != 0)
        {
            while(!q.empty())q.pop();
            auxs = s;
            auxs -= a[loc];
            q.push(a[loc]);
            loc ++;
            start --;
        }

    }

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