Cod sursa(job #1889442)

Utilizator NicusorTelescu Nicolae Nicusor Data 22 februarie 2017 18:42:51
Problema Lapte Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <cstdio>
#include <algorithm>

using namespace std;

struct pereche
{
    int x1,x2,cat1,cat2,rest,indice;
}v[101];

int n;

bool cmp(const pereche &a,const pereche &b)
{
    return a.x2*b.x1<b.x2*a.x1;
}

bool cmp1(const pereche &a,const pereche &b)
{
    return a.indice<b.indice;
}

void afiseaza()
{
    sort(v+1,v+n+1,cmp1);
    for (int i=1;i<=n;++i)
        printf("%d %d\n",v[i].cat1,v[i].cat2);
}

int main()
{
    freopen("lapte.in","r",stdin);
    freopen("lapte.out","w",stdout);
    int L;
    scanf("%d %d",&n,&L);
    for (int i=1;i<=n;++i)
    {
        scanf("%d %d\n",&v[i].x1,&v[i].x2);
        v[i].indice=i;
    }
    sort(v+1,v+n+1,cmp);
    int sumx=0,sumy=0;
    for (int i=1;i<=100;++i)
    {
        sumx=0,sumy=0;
        for (int j=1;j<=n;++j)
        {
            v[j].cat1=i/v[j].x1;
            v[j].rest=i%v[j].x1;
            sumx+=v[j].cat1;
            v[j].cat2=0;
        }
        if (sumx>L)
        {
            for (int j=1;j<=n && sumx>sumy;++j)
            {
                while (sumx>sumy && sumy<L && (v[j].cat1 || v[j].rest>=v[j].x2))
                {
                    if (v[j].rest>=v[j].x2)
                    {
                        v[j].rest-=v[j].x2;
                        ++v[j].cat2;
                        ++sumy;
                    }
                    else if (v[j].rest<v[j].x2 && v[j].cat1)
                        v[j].rest+=v[j].x1, --v[j].cat1, --sumx;
                }
                if (sumy>=L && sumx>=L)
                    printf("%d\n",i), afiseaza(), i=102, j=102;
                if (sumy>=sumx && sumx<L)
                    j=n;
            }
        }
    }
}