Pagini recente » Cod sursa (job #1142612) | Cod sursa (job #3200878) | Cod sursa (job #1968669) | Cod sursa (job #1125039) | Cod sursa (job #549710)
Cod sursa(job #549710)
# include <fstream>
# include <iostream>
# define DIM 128
using namespace std;
int n, l, a[DIM], b[DIM], bst[DIM][DIM], sol=DIM, d[DIM][DIM], s[DIM][2];
void read ()
{
ifstream fin ("lapte.in");
fin>>n>>l;
for(int i=1;i<=n;++i)
fin>>a[i]>>b[i];
}
int ok (int t)
{
for(int i=1;i<=n;++i)
for(int j=0;j<=l;++j)
bst[i][j]=-1;
for(int i=0;i<=l;++i)
if (i*a[1]<=t)
bst[1][i]=(t-a[1]*i)/b[1];
for(int i=1;i<n;++i)
for(int j=0;j<=l;++j)
if (bst[i][j]>-1)
for(int k=0;k*a[i+1]<=t && k+j<=l;++k)
if (bst[i+1][j+k]<bst[i][j]+(t-k*a[i+1])/b[i+1])
{
bst[i+1][j+k]=bst[i][j]+(t-k*a[i+1])/b[i+1];
d[i+1][j+k]=k;
}
if (bst[n][l]>=l)
{
for(int i=n, j=l;i;--i)
{
s[i][0]=d[i][j];
s[i][1]=bst[i][j]-bst[i-1][j-d[i][j]];
j=j-d[i][j];
}
return 1;
}
return 0;
}
void cauta (int st, int dr)
{
if (st==dr)
{
if (st<sol && ok(st))
sol=st;
return;
}
int mij=(st+dr)/2;
if (ok(mij))
{
sol=mij;
cauta (st,mij);
}
else
cauta (mij+1, dr);
}
void write ()
{
ofstream fout ("lapte.out");
fout<<sol<<endl;
for(int i=1;i<=n;++i)
fout<<s[i][0]<<" "<<s[i][1]<<endl;
}
int main()
{
read ();
cauta(1, 100);
write ();
return 0;
}