Cod sursa(job #140573)

Utilizator mihai0110Bivol Mihai mihai0110 Data 21 februarie 2008 23:22:52
Problema Garaj Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<stdio.h>
long i,j,n,m,tmax,S,li,ls;
long cost[100001];
int cant[100001],t[100001];
long poz(int li,int ls)
	{
	long i=li,j=ls,t=1;
	long aux;
	while(i<j)
		{
		if(cost[i]<cost[j])
			{
			aux=cost[i];
			cost[i]=cost[j];
			cost[j]=aux;
			t=1-t;
			}
		if(t)
		 i++;
		else
		 j--;
		}
	return i;
	}
void qsort(int li,int ls)
	{
	long p;
	if(li<ls)
		{
		p=poz(li,ls);
		qsort(li,p-1);
		qsort(p+1,ls);
		}
	}

int main(void)
	{
	freopen("garaj.in","r",stdin);
	freopen("garaj.out","w",stdout);
	scanf("%ld%ld",&n,&m);
	for(i=1;i<=n;i++)
		scanf("%d%d",&cant[i],&t[i]);
	li=0;
	ls=32000;
	S=0;
	while(li<ls)
		{
		S=0;
		tmax=(li+ls)/2;
		for(i=1;i<=n;i++)
			{
			cost[i]=tmax/(2*t[i])*cant[i];
			S+=cost[i];
			}
		if(S<m)
			li=tmax+1;
		else
			ls=tmax-1;
		}
	qsort(1,n);
	S=0;
	i=0;
	while(S<m)
		{
		i++;
		S+=cost[i];
		}
	printf("%ld %ld\n",tmax,i);
	return 0;
	}