Cod sursa(job #186737)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 28 aprilie 2008 18:55:17
Problema Lapte Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <cstring>
#include <fstream>
using namespace std;
int n,L,i,j,k,t,ls,ld,ta[101],tb[101],a[101],b[101],c[101][200],p[101][200],s;
bool gasit;
ifstream f("lapte.in");
ofstream g("lapte.out");
void prel(){
     memset(c,0,sizeof(c));
     memset(p,0,sizeof(p));
     for (j=0;j*ta[1]<=t;++j) c[1][j]=(t-ta[1]*j)/tb[1];
     s=t/ta[1];
     for (i=2;i<=n;++i){
      s+=t/ta[i]; 
      for (j=1;j<=s;++j)
         {int aux=0,poz,w;
          for (k=0;k<=j && k*ta[i]<=t;++k)
            if ((w=c[i-1][j-k]+(t-ta[i]*k)/tb[i]) >aux) {
                      aux=w;
                      poz=k;}
          c[i][j]=aux;
          p[i][j]=poz;}
          }
    }
int main(){
    int tt;
    f>>n>>L;
    for (i=1;i<=n;++i) f>>ta[i]>>tb[i];
    ls=1;ld=100;
    while (ls<=ld){
          t=(ls+ld)/2;
          prel();
          for (i=L,gasit=false;i<=s && !gasit;++i) 
             if (c[n][i]>=L) gasit=true;
          if (gasit) {tt=t;
                      ld=t-1;}
               else ls=t+1;
          }
    t=tt;
    prel();
    for (i=L,gasit=false;i<=s && !gasit;++i) 
             if (c[n][i]>=L) {gasit=true;
                              j=i;}
    a[n]=p[n][j];
    b[n]=(t-a[n]*ta[n])/tb[n];
    g<<t<<'\n';
    for (i=n-1;i>0;i--){
        j=j-a[i+1]*ta[i+1];
        a[i]=p[i][j];
        b[i]=(t-a[i]*ta[i])/tb[i];
        } 
    for (i=1;i<=n;++i)
       g<<a[i]<<' '<<b[i]<<'\n';
    g.close();
    }