BZOJ 3312 [Usaco2013 Nov]No Change

本文介绍了一种算法,旨在帮助农民John通过合理安排支付顺序,在有限的硬币条件下完成一系列购物任务的同时,最大化剩余资金。该算法考虑了硬币的特定价值范围及购物成本,并通过状态压缩等技巧优化了求解过程。

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

Description

Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 <= K <= 16), each with value in the range 1..100,000,000. FJ would like to make a sequence of N purchases (1 <= N <= 100,000), where the ith purchase costs c(i) units of money (1 <= c(i) <= 10,000). As he makes this sequence of purchases, he can periodically stop and pay, with a single coin, for all the purchases made since his last payment (of course, the single coin he uses must be large enough to pay for all of these). Unfortunately, the vendors at the market are completely out of change, so whenever FJ uses a coin that is larger than the amount of money he owes, he sadly receives no changes in return! Please compute the maximum amount of money FJ can end up with after making his N purchases in sequence. Output -1 if it is impossible for FJ to make all of his purchases.

K个硬币,要买N个物品。

给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的。现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1

Input

Line 1: Two integers, K and N.

* Lines 2..1+K: Each line contains the amount of money of one of FJ's coins.

* Lines 2+K..1+N+K: These N lines contain the costs of FJ's intended purchases. 

Output

* Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.

Sample Input

3 6
12
15
10
6
3
3
2
3
7

INPUT DETAILS: FJ has 3 coins of values 12, 15, and 10. He must make purchases in sequence of value 6, 3, 3, 2, 3, and 7.

Sample Output

12
OUTPUT DETAILS: FJ spends his 10-unit coin on the first two purchases, then the 15-unit coin on the remaining purchases. This leaves him with the 12-unit coin.

HINT

Source

窝想到了要状压硬币,再开一位表示剩下多少钱,但是mle。
苟且的看了题解,发现可以预处理第i个硬币从j开始拿最多可以拿多少个物品,记得要优化,时间复杂度可以从O(n^3)下降到O(n^2)。
#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005;
int n,m,ans=-1,sum,v[17],s[N],c[N],g[17][N],f[1<<16];
int main()
{
	scanf("%d%d",&m,&n);
	for(int i=0;i<m;i++)
	{
		scanf("%d",&v[i]);
		sum+=v[i];
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&c[i]);
		s[i]=s[i-1]+c[i];
	}
	for(int i=0;i<m;i++)
		for(int j=1;j<=n;j++)
		{
			int t=j+max(g[i][j-1]-2,0),lft=v[i]+s[j-1]-s[t-1];
			g[i][j]=max(0,g[i][j-1]-2);
			for(int k=t;k<=n;k++)
				if(lft>=c[k])
				{
					lft-=c[k];
					g[i][j]++;
				}
				else
					break;
		}
	int end=(1<<m)-1;
	for(int i=0;i<=end;i++)
		for(int j=0;j<m;j++)
			if(!(i&(1<<j)))
				f[i|(1<<j)]=max(f[i|(1<<j)],f[i]+g[j][f[i]+1]);
	for(int i=0;i<=end;i++)
		if(f[i]==n)
		{
			int t=0;
			for(int j=0;j<m;j++)
				if(i&(1<<j))
					t+=v[j];
			ans=max(ans,sum-t);
		}
	printf("%d\n",ans);
	return 0;
}


一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值