Cod sursa(job #2398731)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 5 aprilie 2019 23:32:16
Problema Lapte Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>

using namespace std;

ifstream fin("lapte.in");
ofstream fout("lapte.out");

const int NMax = 100, oo = 1e9;

int N, L, A[NMax + 5], B[NMax + 5], DP[NMax + 5][NMax + 5], TT[NMax + 5][NMax + 5];

bool Check(int T)
{
    for(int i = 0; i <= N; i++)
        for(int j = 0; j <= L; j++)
            DP[i][j] = -oo;

    DP[0][0] = 0;

    for(int i = 1; i <= N; i++)
        for(int b = 0; b <= L; b++)
            for(int a = 0; a <= L && a * A[i] <= T; a++)
            {
                int b2 = max(0, b - (T - a * A[i]) / B[i]);

                if(DP[i][b] < DP[i - 1][b2] + a)
                    DP[i][b] = DP[i - 1][b2] + a, TT[i][b] = b2;
            }
    return (DP[N][L] >= L);
}

int Solve()
{
    int Ans = (1 << 7) - 1;

    for(int step = (1 << 10); step > 0; step >>= 1)
        if(Ans - step > 0)
            Ans -= Check(Ans - step) * step;

    return Ans;
}

void Way(int i, int j)
{
    if(i > 1) Way(i - 1, TT[i][j]);

    if(i >= 1)
        fout << DP[i][j] - DP[i - 1][TT[i][j]] << " " << j - TT[i][j] << '\n';
}

int main()
{
    fin >> N >> L;

    for(int i = 1; i <= N; i++)
        fin >> A[i] >> B[i];

    fout << Solve() << '\n';

    Way(N, L);

    fin.close();
    fout.close();

    return 0;
}