Cod sursa(job #3142172)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 19 iulie 2023 23:28:44
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <fstream>

using namespace std;

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

const int maxN = 105, inf = (1 << 30);
int n, L;
struct bautor {
    int a, b;
}v[maxN], sol[maxN];
int dp[maxN][maxN];

bool check(int val) {
    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 lA = 0; lA <= j; lA++) {
                int tA = lA * v[i].a;
                int tB = val - tA;
                if(tB < 0)
                    break;
                int lB = tB / v[i].b;
                dp[i][j] = max(dp[i][j], dp[i - 1][j - lA] + lB);
            }
        }
    }
    return (dp[n][L] >= L);
}

int main()
{
    fin >> n >> L;
    for (int i = 1; i <= n; i++)
        fin >> v[i].a >> v[i].b;
    int ans = 0;
    for (int i = (1 << 20); i; i >>= 1) {
        if(!check(ans + i))
            ans += i;
    }
    ans++;
    fout << ans << '\n';
    check(ans);
    int currA = L, currB = L;
    for(int i = n; i >= 1; i--) {
        for(int lA = 0; lA <= currA; lA++) {
            int tA = lA * v[i].a;
            int tB = ans - tA;
            int lB = tB / v[i].b;
            if(dp[i][currA] == (dp[i - 1][currA - lA] + lB)) {
                sol[i].a = lA;
                sol[i].b = lB;
                currA -= lA;
                currB -= lB;
                break;
            }
        }
    }
    for(int i = 1; i <= n; i++)
        fout << sol[i].a << ' ' << sol[i].b << '\n';
    return 0;
}