Cod sursa(job #2133388)

Utilizator amarghescuAnton Marghescu amarghescu Data 16 februarie 2018 21:26:24
Problema Lapte Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include<cstdio>
#include<algorithm>
using namespace std;
int a[105],b[105],n,l,cost[105][105],dp[105][105],cost2[105][105];
pair<int,int>sol[105];
int calc(int t){
int i,j,e;
for(j=0;j<=l && j*a[1]<=t;j++)
dp[1][j]=(t-j*a[1])/b[1];
for(i=2;i<=n;i++)
for(j=0;j<=l;j++)
for(e=0;e<=j && e*a[i]<=t;e++)
if (dp[i][j]<dp[i-1][j-e]+(t-e*a[i])/b[i])
dp[i][j]=dp[i-1][j-e]+(t-e*a[i])/b[i],cost[i][j]=e;}
int transpose(){
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=l;j++)
cost2[i][j]=cost[i][j];}
int main(){
freopen("lapte.in","r",stdin);
freopen("lapte.out","w",stdout);
int i,st,dr,mij,last,j;
scanf("%d%d",&n,&l);
for(i=1;i<=n;i++)
scanf("%d%d",&a[i],&b[i]);
st=1;
dr=100;
while(st<=dr){
mij=(st+dr)/2;
for(i=1;i<=n;i++)
for(j=0;j<=l;j++)
dp[i][j]=0;
calc(mij);
if (dp[n][l]>=l){
last=mij;
transpose();
dr=mij-1;}
else
st=mij+1;}
printf("%d\n",last);
int nr1=n,nr2=l,u=0;
while(nr1>1){
sol[++u].first=cost2[nr1][nr2];
sol[u].second=(last-cost2[nr1][nr2]*a[nr1])/b[nr1];
nr2=nr2-cost2[nr1][nr2];
nr1--;}
sol[++u].first=nr2;
sol[u].second=(last-nr2*a[1])/b[1];
for(i=u;i>=1;i--)
printf("%d %d\n",sol[i].first,sol[i].second);
return 0;}