Cod sursa(job #1823365)

Utilizator GoogalAbabei Daniel Googal Data 6 decembrie 2016 11:32:50
Problema Lapte Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>
#include <algorithm>
#include <cstring>
#define nmax 101

using namespace std;

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

struct milk
{
    int x;
    int y;
};

int n,k,st,dr,rez,h[nmax][nmax],drum[nmax][nmax];
milk a[nmax];

inline void read()
{
    fin>>n>>k;
    for(int i=1; i<=n; i++)
        fin>>a[i].x>>a[i].y;
}

bool ok(int x)
{
    memset(drum,0,sizeof(drum));
    memset(h,-1,sizeof(h));
    h[0][0]=0;
    for (int i=1; i<=n; i++)
    {
        for (int j=0; j<=k; j++)
            if (h[i-1][j]>=0)
            {
                for (int l=0; l+j<=k; l++)
                    if ((x-l*a[i].x)>=0)
                    {
                        if (h[i][j+l]<h[i-1][j]+(x-l*a[i].x)/a[i].y)
                        {
                            h[i][j+l]=h[i-1][j]+(x-l*a[i].x)/a[i].y;
                            drum[i][j+l]=j;
                        }
                    }
                    else break;
            }
    }
    return (h[n][k]>=k);
}

void road(int lv,int x)
{
    if (lv==0) return;
    else
    {
        road(lv-1,drum[lv][x]);
        int dif=x-drum[lv][x];
        fout<<dif<<" "<<h[lv][x]-h[lv-1][drum[lv][x]]<<"\n";
    }
}

inline void caut()
{
    st=1;
    dr=50000;
    while (st<=dr)
    {
        int m=(st+dr)/2;
        if (ok(m))
        {
            rez=m;
            dr=m-1;
        }
        else st=m+1;
    }
}

int main()
{
    read();
    caut();
    ok(rez);
    fout<<rez<<"\n";
    road(n,k);
    return 0;
}