Cod sursa(job #3038503)

Utilizator emazareMazare Emanuel emazare Data 27 martie 2023 14:22:54
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.89 kb
#include <fstream>

using namespace std;

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

const int Nmax = 100;
int v[Nmax+5],w[Nmax+5];
pair<int, int> lapte[Nmax+5],last[Nmax+5][Nmax+5][Nmax+5],init,solut[Nmax+5];

int main()
{
    int N,L,T,i,j,st,dr,sol,lmax,x,y;
    bool ok;
    fin >> N >> L;
    for(i=1;i<=N;i++)
        fin >> lapte[i].first >> lapte[i].second;
    sol = 0; st = 0; dr = Nmax;
    while(st<=dr){
        T = (st+dr)/2;
        v[0] = 0;
        for(i=1;i<=Nmax;i++)
            v[i] = -1;
        lmax = 0;
        for(i=1;i<=N;i++){
            w[0] = v[0];
            for(j=1;j<=Nmax;j++)
                w[j] = v[j];
            for(x=0;x<=(T/lapte[i].first);x++){
                y = (T-x*lapte[i].first)/lapte[i].second;
                for(j=0;j<=lmax;j++){
                    if(j+x<=Nmax)
                        w[j+x] = max(w[j+x], v[j]+y);
                    else
                        w[Nmax] = max(w[Nmax], v[j]+y);
                }
            }
            lmax+=(T/lapte[i].first);
            lmax = min(Nmax, lmax);
            for(j=0;j<=Nmax;j++)
                v[j] = w[j];
        }
        ok = 0;
        for(i=L;i<=Nmax;i++){
            if(v[i]>=L)
                ok = 1;
        }
        if(ok){
            sol = T;
            dr = T-1;
        }
        else
            st = T+1;
    }
    T = sol;
    fout << T << '\n';
    v[0] = 0;
    for(i=1;i<=Nmax;i++)
        v[i] = -1;
    lmax = 0;
    for(i=1;i<=N;i++){
        w[0] = v[0];
        for(j=1;j<=Nmax;j++)
            w[j] = v[j];
        for(x=0;x<=(T/lapte[i].first);x++){
            y = (T-x*lapte[i].first)/lapte[i].second;
            for(j=0;j<=lmax;j++){
                if(j+x<=Nmax){
                    if(v[j]+y>=w[j+x]){
                        w[j+x] = v[j]+y;
                        last[i][j+x][w[j+x]].first = x;
                        last[i][j+x][w[j+x]].second = y;
                    }
                }
                else{
                    if(v[j]+y>=w[Nmax]){
                        w[Nmax] = v[j]+y;
                        last[i][Nmax][w[Nmax]].first = x;
                        last[i][Nmax][w[Nmax]].second = y;
                    }
                }
            }
        }
        lmax+=(T/lapte[i].first);
        lmax = min(Nmax, lmax);
        for(j=0;j<=Nmax;j++)
            v[j] = w[j];
    }
    for(i=L;i<=Nmax;i++){
        if(v[i]>=L){
            init.first = i;
            init.second = v[i];
        }
    }
    for(i=N;i>0;i--){
        x = init.first;
        y = init.second;
        solut[i].first = last[i][x][y].first;
        solut[i].second = last[i][x][y].second;
        init.first-=last[i][x][y].first;
        init.second-=last[i][x][y].second;
    }
    for(i=1;i<=N;i++)
        fout << solut[i].first << " " << solut[i].second << '\n';
    return 0;
}