All pastes #250899 Raw Edit

Unnamed

public cpp v1 · immutable
#250899 ·published 2006-11-17 14:47 UTC
rendered paste body
#include <iostream>#include <memory.h>using namespace std;#define REP(i,n) for(int i=0;i<n;++i)#define FOR(i,a,b) for(int i=a;i<=b;++i)#define FORD(i,a,b) for(int i=a;i>=b;--i)struct inp_type {	int p, m, q;};int main(){	int N, A, B;	int max_profit[105][10005];	//first row and column is 0	inp_type input[105];	scanf("%d %d %d", &N, &A, &B);	FORD(i,N,1) scanf("%d %d %d", &input[i].p, &input[i].m, &input[i].q);	//initialization of max_profit, first column to 0 rest to -1	memset(max_profit, 0xFF, sizeof(max_profit));	FOR(i,0,N) max_profit[i][0]=0;	REP(i,N) {		//run through the i th row, thus trying to find out if 		//the i+1 th row can be used efficiently		FOR(j,0,B) {			if(max_profit[i][j]!=-1) {	//if it's possible				max_profit[i+1][j]=max_profit[i][j];				FOR(k,1,input[i+1].q) {	//use i+1 th elem k times					int curr_mass = j+input[i+1].m*k;					if(curr_mass > B) break;					int profit = 						max_profit[i][j]+input[i+1].p*k;					max_profit[i+1][curr_mass] 						>?= profit;				}			}		}	}	int max_index=-1;	int max_so_far=-1;	FOR(i,A,B) if(max_profit[N][i] > max_so_far) {		max_so_far=max_profit[N][i];		max_index=i;	}	printf("%d", max_so_far); 	if(max_index != -1) {		FORD(i,N,1) {			//find number of times this was taken			FOR(j,0,input[i].q) {				int prev_mass=max_index-input[i].m*j;				if(max_profit[i-1][prev_mass]==					max_profit[i][max_index]-input[i].p*j) {					printf("\n%d", j);					max_index=prev_mass;					break;				}			}		}	}	return 0;}