Cod sursa(job #597824)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 23 iunie 2011 14:27:36
Problema Lapte Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <iostream>
#include <fstream>

#define Infinit 1000000000

using namespace std;

int T, N, L, Best[105][105], A[105], B[105], J[105][105];
ofstream fout ("lapte.out");

void Read ()
{
    ifstream fin ("lapte.in");
    int i;
    fin >> N >> L;
    for (i=1; i<=N; ++i)
    {
        fin >> A[i] >> B[i];
    }
    fin.close ();
}

void Init ()
{
    int i, j;
    for (i=0; i<=100; ++i)
    {
        for (j=0; j<=100; ++j)
        {
            Best[i][j]=-Infinit;
        }
    }
    Best[0][0]=0;
}

void Type (int i, int l)
{
    if (i==0)
    {
        return;
    }
    Type (i-1, l-J[i][l]);
    fout << J[i][l] << " " << (T-J[i][l]*A[i])/B[i] << "\n";
}

inline int Max (int a, int b)
{
    if (a>b)
    {
        return a;
    }
    return b;
}

int main()
{
    int i, j, l;
    Read ();
    for (T=1; T<=100; ++T)
    {
        Init ();
        for (i=1; i<=N; ++i)
        {
            for (l=0; l<=L; ++l)
            {
                for (j=0; j<=T/A[i] && j<=l; ++j)
                {
                    if (Best[i][l]<Best[i-1][l-j]+(T-j*A[i])/B[i])
                    {
                        J[i][l]=j;
                        Best[i][l]=Best[i-1][l-j]+(T-j*A[i])/B[i];
                    }
                }
            }
        }
        if (Best[N][L]>=L)
        {
            fout << T << "\n";
            Type (N, L);
            fout.close ();
            return 0;;
        }
    }
    fout << T << "\n";
    Type (N, L);
    fout.close ();
    return 0;
}