Cod sursa(job #2631773)

Utilizator stefan.popescuPopescu Stefan stefan.popescu Data 30 iunie 2020 23:09:53
Problema Lapte Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.59 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in ("lapte.in");
ofstream out("lapte.out");
const int oo=2e9;
struct tip
{
    int t1, t2;
};
int dp[102][102];

vector<tip> a;
int n, x, y, c;

inline bool check(int timp)
{
    for(int i=0; i<=n; i++)
        for(int j=0; j<=c; j++)
            dp[i][j]=-oo;
    dp[0][0]=0;
    for(int i=1; i<=n; i++)
        for(int j=0; j<=c; j++)
        {
            int val=-oo;
            for(int q=0; q*a[i].t1<=timp&&q<=j; q++)
            {
                if(val<dp[i-1][j-q]+(timp-a[i].t1*q)/a[i].t2&&dp[i-1][j-q]!=-oo)
                    val=dp[i-1][j-q]+(timp-a[i].t1*q)/a[i].t2;
            }
            dp[i][j]=val;
        }
    return dp[n][c]>=c;
}
void getResult(int i, int j, int & timp)
{
    if(i==0) return;
    for(int q=0; q*a[i].t1<=timp&&q<=j; q++)
    {
        if(dp[i][j]==dp[i-1][j-q]+(timp-a[i].t1*q)/a[i].t2&&dp[i-1][j-q]!=-oo)
        {
            getResult(i-1, j-q, timp);
            out<<q<<" "<<(timp-a[i].t1*q)/a[i].t2<<"\n";
            break;
        }
    }
}
int main()
{
    in>>n>>c;
    a.push_back({0, 0});
    for(int i=1; i<=n; i++)
    {
        in>>x>>y;
        a.push_back({x, y});
    }
    int st=1, dr=1000000;
    int rez=1;
    while(st<=dr)
    {
        int mij=(st+dr)/2;
        //cout<<mij<<"\n";
        if(check(mij))
        {
            rez=mij;
            dr=mij-1;
        }
        else st=mij+1;
    }
    out<<rez<<"\n";
    check(rez);
    getResult(n, c, rez);
    return 0;
}