Cod sursa(job #328319)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 1 iulie 2009 17:56:44
Problema Garaj Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <stdio.h>
#define Nmax 100005
#define LIM 5500

long c[Nmax],t[Nmax],v[Nmax];
long n,m,rez,i,drum,sum;

void citire(){
	long i;
   freopen("garaj.in","r",stdin);
   freopen("garaj.out","w",stdout);
   scanf("%ld%ld",&n,&m);
   for(i=1;i<=n;++i) scanf("%ld%ld",&c[i],&t[i]);
}

int bun(long timp){
	// timp este timpul in care un camion duce cate sticle poate
   long i,drum,total=0;
   for(i=1;i<=n;++i){
   	drum = timp / (2*t[i]);
      total+= drum*c[i];
   }
   if(total >=m) return 1;
   return 0;
}

void sort(long l,long r){
	long i,j,x,y;
   i=l; j=r; x=v[l+(r-l)/2];
   do{
   	while(v[i]<x) ++i;
      while(x<v[j]) --j;
      if(i<=j){
      	y=v[i]; v[i]=v[j]; v[j]=y;
         ++i; --j;
      }
   } while(i<=j);
   if(i<r) sort(i,r);
   if(l<j) sort(l,j);
}

long caut_bin(long l,long r){
	long m,ret=-1;
   while(l<=r){
   	m=l+(r-l)/2;
      if(bun(m)){
      	ret=m;
         r=m-1;
      }
      else l=m+1;
   }
   return ret;
}

int main(){
	citire();
	rez=caut_bin(1,LIM);

   for(i=1;i<=n;++i){
   	drum= rez/ (2*t[i]);
      v[i]=drum*c[i];
   }
   sort(1,n);

   for(sum=0,i=n;i>=1 && sum<m;sum+=v[i],--i);

   printf("%ld %ld\n",rez,n-i);
   fclose(stdin); fclose(stdout);
   return 0;
}