Cod sursa(job #664494)

Utilizator toniobFMI - Barbalau Antonio toniob Data 20 ianuarie 2012 10:19:34
Problema Lapte Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream in ("lapte.in");
ofstream out ("lapte.out");

struct viteza {
	int a,b;
}v[102],rez[102],kkk[102];
int n,l;

bool comp (viteza x,viteza y){
	return x.b > y.b;
}

void mergesort (int l,int r){
	if (l == r){
		return;
	}
	int m = (l + r) / 2;
	mergesort (l,m);
	mergesort (m + 1, r);
	viteza aux[r+2];
	for (int i = 1,k=1,j=m+1;j <= r || i <= m; ){
		if (j > r || (i <= m && v[i].b > v[j].b)){
			aux[k++]=v[i++];
			continue;
		}
		aux[k++]=v[j++];
	}
	for (int i = l; i <= r; ++i) {
		v[i] = aux[i];
	}
}

void citire (){
	in >> n >> l;
	for (int i = 1; i <= n; ++i) {
		in >> v[i].a >> v[i].b;
		kkk[i].a=i,kkk[i].b=i;
	}
	mergesort (1,n);
}

bool ok (int time){
	for (int i = 1; i <= n; ++i){
		rez[i].a = rez[i].b=0;
	}
	int laptea = l,lapteb=l,qqq;
	for (int i = 1; i <= n;++i){
		qqq = time/v[i].a;
		if (qqq >= laptea){
			rez[i].a = laptea;
			laptea = 0;
			break;
		}
		rez[i].a = qqq;
		laptea -= qqq;
	}
	if (laptea) {
		return false;
	}
	for (int i = n; i >= 1;--i){
		int tim = time - rez[i].a * v[i].a;
		qqq = tim/v[i].b;
		if (qqq >= lapteb){
			rez[i].b = lapteb;
			lapteb = 0;
			break;
		}
		rez[i].b = qqq;
		lapteb -= qqq;
	}
	if (lapteb){
		return false;
	}
	return true;
}

int bs () {
	int step = 1 << 7,i=0;
	for (; step; step >>=1){
		if (!ok(i+step)){
			i += step;
		}
	}
	return i + 1;
}

int main (){
	citire ();
	
	int udfhuiowefh = bs ();
	out << udfhuiowefh << '\n';
	ok (udfhuiowefh);
	
	for (int i = 1; i <= n; ++i) {
		out << rez[i].a << ' ' << rez[i].b << '\n';
	}
	
	return 0;
}