Cod sursa(job #2797343)

Utilizator domistnSatnoianu Dominic Ioan domistn Data 9 noiembrie 2021 19:06:14
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.95 kb
/*
        _,'|             _.-''``-...___..--';)
       /_ \'.      __..-' ,      ,--...--'''
      <\    .`--'''       `     /'
       `-';'               ;   ; ;
 __...--''     ___...--_..'  .;.'
(,__....----'''       (,..--'' pisica
*/

#include <iostream>
#include <stack>
#include <climits>

#define NMAX 105

using namespace std;

int n, l, a[NMAX], b[NMAX],
    dp[NMAX][NMAX], ansa[NMAX][NMAX], ansb[NMAX][NMAX];

/*
    dp[i][j] = Nr maxim de B ce le bea persoana i
               daca bea j litri de A (in timpul TIMP)
    Solutie: dp[n][L]

    Cu ansa/ansb se gaseste solutia
*/

inline bool bem(const int TIMP) {
    for(int i = 0; i <= n; ++i)
        for(int j = 0; j <= l; ++j)
            dp[i][j] = INT_MIN;
    dp[0][0] = 0;

    for(int i = 1; i <= n; ++i)
        for(int la = 0; a[i] * la <= TIMP; ++la) {
            const int lb = (TIMP - a[i] * la) / b[i];
            for(int j = 0; j <= l - la; ++j) {
                if(dp[i - 1][j] + lb > dp[i][j + la]) {
                    dp[i][j + la] = dp[i - 1][j] + lb;
                    ansa[i][j + la] = la;
                    ansb[i][j + la] = lb;
                }
            }
        }
    return dp[n][l] >= l;
}

int cb() {
    int st = 1, dr = 10000, mij, ans;
    while(st <= dr) {
        mij = (st + dr) / 2;
        if(bem(mij)) ans = mij, dr = mij - 1;
        else st = mij + 1;
    }
    return ans;
}

int main()
{
    freopen("lapte.in", "r", stdin);
    freopen("lapte.out", "w", stdout);
    scanf("%d%d", &n, &l);
    for(int i = 1; i <= n; ++i)
        scanf("%d%d", &a[i], &b[i]);

    const int lapte = cb();
    printf("%d\n", lapte);
    bem(lapte);

    stack<pair<int, int> > ans;
    for(int i = n, j = l; i; --i) {
        ans.push({ansa[i][j], ansb[i][j]});
        j -= ansa[i][j];
    }
    while(!ans.empty())
        printf("%d %d\n", ans.top().first, ans.top().second),
        ans.pop();
    return 0;
}