Cod sursa(job #3325098)

Utilizator Grama2008Grama Andrei Teodor Grama2008 Data 24 noiembrie 2025 18:55:52
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <bits/stdc++.h>

using namespace std;

const int N=105;
pair<int,int>lapte[N];
pair<int,int>source[N][N];
int dp[N][N],n,l;

bool check(int t){
    memset(dp, -1, sizeof(dp));
    dp[0][0]=0;
    for (int i=1;i<=n;i++){
        int a=lapte[i].first;
        int b=lapte[i].second;
        for (int w=0;w*a<=t;w++){
            int p=(t-w*a)/b;
            for (int x=l;x>=w;x--){
                if (dp[i-1][x-w]==-1)continue;
                dp[i][x]=max(dp[i][x], dp[i-1][x-w]+p);
            }
        }
        if (a>t){
            int p=t/b;
            for (int x=0;x<=l;x++){
                if (dp[i-1][x]==-1)continue;
                dp[i][x]=max(dp[i][x], dp[i-1][x]+p);
            }
        }
    }
    return dp[n][l]>=l;
}

int main()
{
    freopen("lapte.in", "r", stdin);
    freopen("lapte.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin>>n>>l;
    for (int i=1;i<=n;i++){
        cin>>lapte[i].first>>lapte[i].second;
    }
    int left=0, right=N, ans=-1;
    while (left<=right){
        int mid=(left+right)/2;
        if (check(mid)){
            ans=mid;
            right=mid-1;
        }
        else{
            left=mid+1;
        }
    }
    cout<<ans<<'\n';
    int t=ans;
    memset(dp, -1, sizeof(dp));
    dp[0][0]=0;
    for (int i=1;i<=n;i++){
        int a=lapte[i].first;
        int b=lapte[i].second;
        for (int w=0;w*a<=t;w++){
            int p=(t-w*a)/b;
            for (int x=l;x>=w;x--){
                if (dp[i-1][x-w]==-1)continue;
                if (dp[i][x]<dp[i-1][x-w]+p){
                    dp[i][x]=dp[i-1][x-w]+p;
                    source[i][x]={w,p};
                }
            }
        }
        if (a>t){
            int p=t/b;
            for (int x=0;x<=l;x++){
                if (dp[i-1][x]==-1)continue;
                if (dp[i][x]<dp[i-1][x]+p){
                    dp[i][x]=dp[i-1][x]+p;
                    source[i][x]={0,p};
                }
            }
        }
    }
    vector<pair<int,int>>res;res.reserve(n);
    int i=n, x=l;
    while (i>0){
        res.push_back(source[i][x]);
        x-=source[i][x].first;
        i--;
    }
    for (i=n-1;i>=0;i--){
        cout<<res[i].first<<' '<<res[i].second<<'\n';
    }
    return 0;
}