Cod sursa(job #2919922)

Utilizator chiriacandrei25Chiriac Andrei chiriacandrei25 Data 20 august 2022 18:29:23
Problema Lapte Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f ( "lapte.in" );
ofstream g ( "lapte.out" );

const int nmax = 102;
const int inf = 0x3f3f3f3f;

int n, l, t, sol;
int dp[nmax][nmax], tA[nmax], tB[nmax];
int bestX[nmax][nmax]; // bestX[i][j] = x-ul ales pt copilul i ca sa maximizam dp[i][j]

bool verify () {

    int y;
    for ( int i = 0; i <= n; i++ ) 
        for ( int j = 0; j <= l; j++ ) 
            dp[i][j] = -inf;
    dp[0][0] = 0;
    for ( int i = 1; i <= n; i++ ) {
        for ( int j = 0; j <= l; j++ ) {
            for ( int x = 0; x * tA[i] <= t && x <= j; x++ ) {
                y = t - x * tA[i];
                if(dp[i - 1][j - x] + y / tB[i] > dp[i][j]) {
                    dp[i][j] = dp[i - 1][j - x] + y / tB[i];
                    bestX[i][j] = x;
                }
            }
        }
    }

    cout << t << "\n";
    for ( int i = 1; i <= n; i++ ) {
        for ( int j = 1; j <= l; j++ ) {
            cout << dp[i][j] << ' ';
        }
        cout << '\n';
    }
    cout << '\n';

    return ( dp[n][l] >= l );
}

void reconst(int i, int j) {
    if(i == 0) {
        return;
    }
    int x = bestX[i][j];
    int y = (sol - x * tA[i]) / tB[i];
    reconst(i - 1, j - x);
    g << x << " " << y <<"\n";
}

int main() {

    f >> n >> l;
    for ( int i = 1; i <= n; i++ )
        f >> tA[i] >> tB[i];
    
    int st, dr;
    st = 0;
    dr = 20000;
    while ( st <= dr ) {
        t = ( st + dr ) / 2;
        if ( verify () ) {
            sol = t;
            dr = t - 1;
        }
        else
            st = t + 1;
    }

    g << sol << '\n';
    reconst(n, l);

    f.close();
    g.close();
}