808E. Selling Souvenirs***

本文介绍了一种解决复杂背包问题的算法实现,通过定义结构体dp来存储价值和数量信息,并利用动态规划的方法来求得最大价值。文章通过具体代码展示了如何对不同类型的物品进行排序和累积价值计算,最终输出最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<list>
#include<stack>
#include<cstdio>
#include<fstream>
#include<numeric>
#include<functional>
#include<utility>
#include<memory>
#include<iomanip>
using namespace std;

typedef long long int LL;

LL n, m;

typedef struct dp{
	LL val;
	LL number1, number2;
}dp;


int main(){

	while (cin >> n >> m){
		LL value[4][300010];
		LL total[4][300010];
		vector<LL> index(4, 0);
		for (LL i = 0; i < n; i++){
			LL w, c;
			cin >> w >> c;
			index[w]++;
			value[w][index[w]] = c;
		}
		for (LL i = 1; i <= 3; i++){
			sort(value[i] + 1, value[i] + index[i] + 1, greater<LL>());
			total[i][0] = 0;
			for (LL j = 1; j <= index[i]; j++) total[i][j] = total[i][j - 1] + value[i][j];
		}
		dp result[300010];
		result[0].val = 0;
		result[0].number1 = 0;
		result[0].number2 = 0;
		for (LL i = 1; i <= m; i++){
			result[i] = result[i - 1];
			if (result[i - 1].val + value[1][result[i - 1].number1 + 1]>result[i].val){
				result[i].val = result[i - 1].val + value[1][result[i - 1].number1 + 1];
				result[i].number2 = result[i - 1].number2;
				result[i].number1 = result[i - 1].number1 + 1;
			}
			if (i >= 2 && result[i - 2].val + value[2][result[i - 2].number2 + 1] > result[i].val){
				result[i].val = result[i - 2].val + value[2][result[i - 2].number2 + 1];
				result[i].number1 = result[i - 2].number1;
				result[i].number2 = result[i - 2].number2 + 1;
			}
		}
		LL res = 0;
		for (LL i = 0; i <= index[3]; i++){
			if (m >= i * 3){
				res = max(res, total[3][i] + result[m - 3 * i].val);
			}
		}
		cout << res << endl;
	}
	return 0;
}

CPU 1 Unable to handle kernel paging request at virtual address 00000004, epc == 8009627c, ra == 800961cc [14105.700932] Oops[#1]: [14105.703286] CPU: 1 PID: 0 Comm: swapper/1 Not tainted 4.4.94 #210 [14105.709584] task: 8407a720 ti: 84082000 task.ti: 84082000 [14105.715163] $ 0 : 00000000 808e0004 00000cd4 002a03c0 [14105.720578] $ 4 : 00000001 00000000 00000018 00000cd4 [14105.725992] $ 8 : 80860000 00000000 ffffffff 0000004e [14105.731406] $12 : 002a03c0 00000000 00000000 00000000 [14105.736811] $16 : 808ed200 00000000 808f0000 81135b38 [14105.742216] $20 : c4653600 002a03c0 81135ad4 81135b58 [14105.747621] $24 : 00000000 8001dcd8 [14105.753027] $28 : 84082000 8400ded8 4f1e8abd 800961cc [14105.758434] Hi : 00011c9e [14105.761402] Lo : a1579c3c [14105.764384] epc : 8009627c ktime_get_update_offsets_now+0x190/0x1cc [14105.771035] ra : 800961cc ktime_get_update_offsets_now+0xe0/0x1cc [14105.777590] Status: 14000c02 KERNEL EXL [14105.781648] Cause : 0880000c (ExcCode 03) [14105.785784] BadVA : 00000004 [14105.788754] PrId : 00132000 (Ingenic XBurst@II.V2) [14105.793784] Modules linked in: usb_f_mass_storage libcomposite dwc2 udc_core usb_storage usbcore usb_common [14105.803871] Process swapper/1 (pid: 0, threadinfo=84082000, task=8407a720, tls=00000000) [14105.812222] Stack : 4f1e8400 81135ac0 00000003 8009cd6c 00000cd4 00000000 809060a0 b2100100 7fffffff ffffffff 81135ac0 8085870c 81135b38 81135b58 81135b78 8008dda8 80848640 8007f2a8 00000001 81137760 81135b78 8005b480 4f1e8400 00000cd4 81135ad4 00000cd4 808ed200 1c890656 809060a0 b2100100 80906188 84029c80 00000004 8085870c 00000000 80858640 00000001 8040de5c 8088daa0 84029c80
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值