Cod sursa(job #137679)

Utilizator slayer4uVictor Popescu slayer4u Data 17 februarie 2008 12:55:32
Problema Garaj Scor 20
Compilator cpp Status done
Runda preONI 2008, Runda 4, Clasa a 9-a Marime 1.21 kb
#include <stdio.h>
#include <algorithm>
using namespace std;

long long i, j, k, n, m, timp;

struct lol
{
	long long q, t, p, r;
};
lol c[100001];


int cmpf(lol a, lol b)
{
	return a.p > b.p;
}

long long realizabil(long long timp)
{
	long long mc = m;

	for (i = 1; i <= n && mc > 0; i ++)
	{
		mc = mc - (timp / (c[i].t * 2) * c[i].q);
	}
	if (mc <= 0)
		return i - 1;
	return -1;
}

void cautbin(long long st, long long dr)
{
	long long m = (st + dr) / 2;
	long long k = realizabil(m);
	if (st >= dr || dr - st == 1) 
	{
		if (k != -1)
			timp = timp > m ? m : timp;
		k = realizabil(m + 1);
		if (k != -1)
			timp = timp > (m + 1) ? (m + 1) : timp;
		return ;
	}
	if (k != -1)
	{
		timp = timp > m ? m : timp;
		cautbin(st, m);
	}
	else
		cautbin(m, dr);
}

int main()
{
	freopen ("garaj.in", "rt", stdin);
	freopen ("garaj.out", "wt", stdout);

	scanf("%lld %lld", &n, &m);
	for (i = 1; i <= n; i ++)
	{
		scanf("%lld %lld", &c[i].q, &c[i].t);
		c[i].p = c[i].q / (2 * c[i].t);

	}

	sort(c + 1, c + n + 1, cmpf);

	timp = 1337000000000000LL;
	cautbin(1, (m / c[1].q + m % c[1].q) * 2 * c[1].t);

	printf("%lld %lld", timp, realizabil(timp));

	return 0;
}