Cod sursa(job #337636)

Utilizator ilincaSorescu Ilinca ilinca Data 4 august 2009 13:29:28
Problema Garaj Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <stdio.h> 
#include <values.h> 

#define nmax 100000
#define mmax 1000000000
#define inf INT_MAX


struct ceva
{
	int c, t;
};

int n, m;
ceva cam [nmax+5];

void scan ()
{
	int i;
	scanf ("%d%d", &n, &m);
	for (i=1; i <= n; ++i) 
		scanf ("%d%d", &cam [i].c, &cam [i].t);
}

bool ok (int x)
{
	int i, ns=m;
	for (i=1; i <= n; ++i)
	{
		ns -= (x/cam [i].t)*cam [i].c;
		if (ns < 1) 
			return true;
	}	
	return false;
}

int timpmin ()
{
	int i, s;
	//fprintf (stderr, "%d\n", inf);
	s=1<<30;
	for (i=0; s ; s >>= 1) 
		if ((unsigned int)i+s <= inf && !ok (i+s)) 
			i += s;
	return ++i;	
}

int partitie (int st, int dr)
{
	int i=st-1, j=dr+1;
	ceva piv=cam [(i+j)>>1], aux;
	while (1)
	{
		do {++i;} while ((long long)cam [i].c*piv.t > (long long)cam [i].t*piv.c);
		do {--j;} while ((long long)cam [j].c*piv.t < (long long)cam [j].t*piv.c);
		if (i < j)
		{
			aux=cam [i];
			cam [i]=cam [j];
			cam [j]=aux;
		}
		else
			return j;	
	}	
}

void sort (int st, int dr)
{
	if (st < dr)
	{
		int p=partitie (st, dr);
		sort (st, p);
		sort (p+1, dr);
	}	
}

int cammin (int x)
{
	sort (1, n);
	int i, ns=m;
//	for (i=1; i <= n; ++i) 
//		fprintf(stderr, "%d\n", cam [i].c); 
	for (i=1; ns >= 0 ; ++i)
	//{	
		ns -= (x/cam [i].t) * cam [i].c;
	//	fprintf(stderr, "ns=%d\n", ns); 
	//}
	return --i;	
}

int main ()
{
	freopen ("garaj.in", "r", stdin);
	freopen ("garaj.out", "w", stdout);
	scan ();
	int x=timpmin ();
	//fprintf(stderr, "%d\n", x); 
	printf ("%d %d\n", x<<1, cammin (x));
	return 0;
}