CodeVS 1155 金明的预算方案 题解

分组背包问题

时间复杂度:O(M*N/10)

空间复杂度:O(N/10)

 

#include <cstdio>
#include <memory.h>
using namespace std;

int N, M;
int v, p, q;
int tmp, Sum, Max;

struct THING{
	bool Main;
	short int num;
	int Sum[4];
	int V[4];
}thing[60+2];

int f[3200+2];

int main() {
	scanf("%d %d", &N, &M);
	for(int i = 1; i<=M; ++i){
		scanf("%d %d %d", &v, &p, &q);
		Sum = v*p;
		v /= 10;
		if(q==0){
			thing[i].Main = true;
			for(int j = 0; j<4; ++j) thing[i].Sum[j] += Sum;
			for(int j = 0; j<4; ++j) thing[i].V[j] += v;
		}
		else{
			tmp = ++thing[q].num;
			thing[q].Sum[tmp] += Sum; thing[q].Sum[3] += Sum;
			thing[q].V[tmp] += v; thing[q].V[3] += v;
		}
	}

	for(int i = 1; i<=M; ++i){
		if(!thing[i].Main) continue;
		for(int j = N/10; j>=1; --j){
			Max = f[j];
			for(int k = 0; k<4; ++k){
				tmp = j-thing[i].V[k];
				if(tmp<0) continue;
				if((f[tmp]+thing[i].Sum[k])>Max) Max = f[tmp]+thing[i].Sum[k];
			}
			f[j] = Max;
		}
	}	
	
	printf("%d\n", f[N/10]);
    	return 0;
}	return 0;
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值