Cod sursa(job #3293357)

Utilizator Andrei1209Andrei Mircea Andrei1209 Data 11 aprilie 2025 15:34:42
Problema Lapte Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <bits/stdc++.h>

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

int L, n, t1[105], t2[105], a[105][105];
vector <vector <int>> dp;
pair <int, int> tata[105][105], solution[105];
/// dp[i][j] - cantitatea maxima de lapte 2 pe care o beau primii i copii, a.i sa bea SIGUR j litri de lapte 1
bool verif( int T )
{
    int i, j;
    dp.assign(n+1,vector <int>(L+1,-1));
    dp[0][0]=0;
    for ( i = 1; i <= n; ++i )
        for ( j = 0; j <= L; ++j )
        {
            dp[i][j] = 0;
            int x, y;/// x, y - cantitatea de lapte de tip 1 respectiv 2 pe care o bea copilul i
            for ( x = 0; x <= j && x * t1[i] <= T; ++x )
            {
                if ( dp[i - 1][j - x] >= 0 )
                {

                    y = (T - x * t1[i]) / t2[i];
                    if ( dp[i][j] < dp[i - 1][j - x] + y )
                    {
                        dp[i][j] = dp[i - 1][j - x] + y;
                        a[i][j] = x;
                    }
                }
            }
        }
    if ( dp[n][L] >= L )
        return true;
    return false;
}
void afis()
{
    for ( int i = 1; i <= n; ++i, fout << endl )
        for ( int j = 1; j <= L; ++j )
            fout << dp[i][j] << " ";
}
int main()
{
    fin >> n >> L;
    int i;
    for ( i = 1; i <= n; ++i )
        fin >> t1[i] >> t2[i];

    int st = 1, dr = 1000005, mij, sol = -1;
    while ( st <= dr )
    {
        mij = (st + dr) / 2;
        if ( verif(mij) == true )
        {
            sol = mij;
            dr = mij - 1;
        }
        else
            st = mij + 1;
    }
    fout << sol << '\n';
    bool ceva = verif(sol);
   // afis();
   // fout << "----------------------------------------\n";
    int s = L;
    for ( i = n; i >= 1; --i )
    {
        solution[i] = {a[i][s], (sol - a[i][s] * t1[i])/t2[i]};
        s -= a[i][s];
    }
    //fout << "-----------------------\n";
    for ( i = 1; i <= n; ++i )
        fout << solution[i].first << " " << solution[i].second << "\n";
    return 0;
}