Cod sursa(job #810088)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 9 noiembrie 2012 17:13:43
Problema Lapte Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <stdio.h>
#include <string.h>
#define dim 150

using namespace std;

int n,i,j,sol[dim][dim],Z[dim][dim],Sol,L;
int k;
struct timp{
	int a;
	int b;
}V[150];

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

void read(){
	f>>n>>L;
	for(i=1;i<=n;i++)
		f>>V[i].a>>V[i].b;
}

int solutie(int x){
	memset(Z,-1,sizeof(Z));
	
	Z[0][0]=0;
	
	for(i=1;i<=n;i++){
		for(j=0;j<=L;j++){
			if(Z[i-1][j]>=0)
				for(k=0;k*V[i].a<=x;k++){
					if(Z[i][j+k]<Z[i-1][j]+(x-k*V[i].a)/V[i].b)
						Z[i][j+k]=Z[i-1][j]+(x-k*V[i].a)/V[i].b;
				}
		}
	}
	if(Z[n][L]>=L)
		return 1;
	return 0;
}

void cautare_bin(){
	int low=0;
	int high=100;
	int mid=(low+high)/2;
	
	while(low<=high){
		if(solutie(mid)){
			high=mid-1;
			Sol=mid;
		}
		else
			low=mid+1;
		mid=(low+high)/2;
	}
	
	g<<Sol<<"\n";
	
}

void recurenta(int x, int y){
	if(x==0)
		return;
	recurenta(x-1,y-sol[x][y]);
	g<<sol[x][y]<<" "<<(Sol-sol[x][y]*V[x].a)/V[x].b<<"\n";
}

int main(){
	
	read();
	
	cautare_bin();
	
	memset(Z,-1,sizeof(Z));
	Z[0][0]=0;
	
	int x=Sol;
	
	for(i=1;i<=n;i++){
		for(j=0;j<=L;j++){
			if(Z[i-1][j]>=0)
				for(k=0;k*V[i].a<=x;k++){
					if(Z[i][j+k]<Z[i-1][j]+(x-k*V[i].a)/V[i].b){
						Z[i][j+k]=Z[i-1][j]+(x-k*V[i].a)/V[i].b;
						sol[i][j+k]=k;
					}
				}
		}
	}
	
	recurenta(n,L);
	
	return 0;
}