Cod sursa(job #214723)

Utilizator alexeiIacob Radu alexei Data 15 octombrie 2008 19:15:46
Problema Garaj Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include<stdio.h>
#include<algorithm>
#define NMAX 100024
using namespace std;

typedef struct{
    int C,T;
}q;
q V[NMAX];

inline bool cmp(q A,q B)
{
    return A.T<B.T;
}

int N,M;

int solve(const int time)
{
    int i,sum=0,nr=0;
    for(i=1; i<=N && sum<M; ++i)
        if( time>=V[i].T )
            sum+=(time/V[i].T)*V[i].C,
            ++nr;
        else
        break;
        
    if( sum<M )return 0;
    return nr;
}
   
int main()
{
    freopen("garaj.in","r",stdin);
    freopen("garaj.out","w",stdout);
   
    scanf("%d%d",&N,&M);
   
    int i,aux;
    int tmin=1,tmax=0,tmij;
    for(i=1; i<=N; ++i){
        scanf("%d%d",&V[i].C,&V[i].T);
        V[i].T<<=1;
        if( V[i].C < M )
            aux=(M/V[i].C)*V[i].T;
        else
            aux=V[i].T;
       
        if( !tmax || tmax<aux ) tmax=aux;
    
    }
   
    sort(V+1,V+N+1,cmp);
    
    int solt=tmax,solc=1; 
    
    while( tmin<=tmax )
    {
        tmij=(tmin+tmax)>>1;
        aux=solve(tmij);
        if( aux )
        {
            solt=tmij;
            solc=aux;
            tmax=tmij-1;
        }
        else
        {
            tmin=tmij+1;
        }
    }
   
    printf("%d %d\n",solt,solc);
   
    return 0;
}