Cod sursa(job #2020085)

Utilizator CiprianC111Constantinescu Ciprian CiprianC111 Data 9 septembrie 2017 13:38:28
Problema Loto Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

struct loto
{
    int elm;
    int ind;
};

bool cmp(const loto &a, const loto &b)
{
    return a.elm < b.elm;
}

vector <int>v;
vector <loto>rez;
vector <loto>::iterator it;

int sum() { return rez[0].elm + rez[1].elm + rez[2].elm + rez[3].elm + rez[4].elm + rez[5].elm; }

int main()
{
    freopen("loto.in", "r", stdin);
    freopen("loto.out", "w", stdout);
    int n, s, x, mx = -1, mi = 100000001;
    loto tmp;
    scanf("%d %d", &n, &s);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &x);
        v.push_back(x);
        mi = min(mi, x);
        mx = max(mx, x);
    }
    if(s > 6 * mx or 6 * mi > s){ printf("-1"); return 0; }
    sort(v.begin(), v.end());
    if(abs((6 * mx) - s) < abs((6 * mi) - s))
    {
        for(int i = 0; i < 6; i++)
        {
            tmp.elm = mx;
            tmp.ind = n - 1;
            rez.push_back(tmp);
        }
    }
    else
    {
        for(int i = 0; i < 6; i++)
        {
            tmp.elm = mi;
            tmp.ind = 0;
            rez.push_back(tmp);
        }
    }
    while(sum() > s)
    {
        for(it = rez.begin(); it != rez.end(); it++)
        {
            if(sum() <= s) break;
            if((*it).elm == mi) continue;
            (*it).elm = v[--(*it).ind];
        }
    }
    while(sum() < s)
    {
        for(it = rez.begin(); it != rez.end(); it++)
        {
            if(sum() >= s) break;
            if((*it).elm == mx) continue;
            (*it).elm = v[++(*it).ind];
        }
    }
    if(sum() == s)
    {
        sort(rez.begin(), rez.end(), cmp);
        for(it = rez.begin(); it != rez.end(); it++)
        {
            printf("%d ", (*it).elm);
        }
    }
    else
    {
        printf("-1");
    }
    return 0;
}