P3092 [USACO13NOV]没有找零No Change
题目描述
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(1 <= K <= 16)个硬币,面值的范围是1..100,000,000。
约翰想按顺序买 N个物品(1 <= N <= 100,000),第i个物品需要花费c(i)块钱,(1 <= c(i) <= 10,000)。
在依次进行的购买N个物品的过程中,约翰可以随时停下来付款,每次付款只用一个硬币,支付购买的内容是从上一次支付后开始到现在的这些所有物品(前提是该硬币足以支付这些物品的费用)。不幸的是,商场的收银机坏了,如果约翰支付的硬币面值大于所需的费用,他不会得到任何找零。
请计算出在购买完N个物品后,约翰最多剩下多少钱。如果无法完成购买,输出-1
输入输出格式
输入格式:
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.
输出格式:
- Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.
输入输出样例
说明
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.
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.
状压Dp:
设f[i]为花了i(base 2)状态下的硬币所能连续购买到的物品,状态转移为:
f[i]=max(f[i],f[i^(1<<k)]~n中最后一个满足( sum[cur]<=coin[k]+sum[f[i^(1<<k)]] )的下标cur.
coin[k]是第k个硬币的面额;
sum[cur]是1~cur的总花费;
具体实现(感谢FuTaimeng的启示):
#include <cstdio>
#include <cstring>
#include <algorithm>
#define llnt long long
using namespace std;
<<];
llnt ans,tot,coin[],sum[<<];
int main() {
scanf("%d%d",&m,&n);
;i<m;i++) {
scanf("%lld",&coin[i]);
tot+=coin[i];
}
;i<=n;i++) {
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
}
<<m)-;
;s<=ALL;s++) {
;i<m;i++) <<i)) {
<<i)];
x=upper_bound(sum+x,sum+n+,coin[i]+sum[x])-sum-;
f[s]=max(f[s],x);
}
}
ans=1ll<<;
;s<=ALL;s++) {
if(f[s]==n) {
llnt now=;
;i<m;i++) ) now+=coin[i];
ans=min(ans,now);
}
}
;
else ans=tot-ans;
printf("%lld\n",ans);
;
}
#define note "https:/**/www.luogu.org/problemnew/show/3092"
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
],c[];
][<<];
];
<<];
int main()
{
;
scanf("%d%d",&K,&n);
; i<K; i++) scanf("%d",&v[i]),tto+=v[i];
; i<=n; i++) scanf(]+c[i];
memset(f,-0x3f,sizeof f);
end=<<K;
,j=,x=; i<end; x=++i,j=) {
do {
) afd[i]+=v[j];
x>>=,j++;
} while(x);
afd[i]=tto-afd[i];
f[][i]=afd[i];
}
; j<end; j++) ; i<=n; i++) if(afd[j]>=sum[i]) //买得起
; k<K; k++) <<k))) //选了这枚硬币
; l<=i; l++) ]) //前面这一节
f[i][j]=max(f[i][j],f[i-][j^(<<k)]-v[k]); //更新
; i<=n; i++) {
; j<end; j++) printf(?-:f[i][j]);
printf("\n");
}
;
; i<end; i++) ans=max(ans,f[n][i]);
printf("%d\n",ans);
;
}
这是蒟蒻的垃圾思索,请勿解封
P3092 [USACO13NOV]没有找零No Change的更多相关文章
- 洛谷P3092 [USACO13NOV]没有找零No Change
P3092 [USACO13NOV]没有找零No Change 题目描述 Farmer John is at the market to purchase supplies for his farm. ...
- 洛谷 P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- luogu P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- P3092 [USACO13NOV]没有找零No Change 状压dp
这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了. 题干: 题目描述 Farmer John <= K <= ), each with value .., ...
- Luogu P3092 [USACO13NOV]没有找零No Change【状压/二分】By cellur925
题目传送门 可能是我退役/NOIP前做的最后一道状压... 题目大意:给你\(k\)个硬币,FJ想按顺序买\(n\)个物品,但是不能找零,问你最后最多剩下多少钱. 注意到\(k<=16\),提示 ...
- [USACO13NOV]没有找零No Change [TPLY]
[USACO13NOV]没有找零No Change 题目链接 https://www.luogu.org/problemnew/show/3092 做题背景 FJ不是一个合格的消费者,不知法懂法用法, ...
- [洛谷P3092]【[USACO13NOV]没有找零No Change】
状压\(DP\) + 二分 考虑构成:\(k<=16\)所以根据\(k\)构造状压\(dp\),将所有硬币的使用情况进行状态压缩 考虑状态:数组\(dp[i]\)表示用\(i\)状态下的硬币可以 ...
- 【[USACO13NOV]没有找零No Change】
其实我是点单调队列的标签进来的,之后看着题就懵逼了 于是就去题解里一翻,发现楼上楼下的题解说的都好有道理, f[j]表示一个再使用一个硬币就能到达i的某个之前状态,b[now]表示使用那个能使状态j变 ...
- [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)
传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...
随机推荐
- Opencv处理鼠标事件-OpenCV步步精深
在图片上双击过的位置绘制一个 圆圈 鼠标事件就是和鼠标有关的,比如左键按下,左键松开,右键按下,右键松开,双击右键等等. 我们可以通过鼠标事件获得与鼠标对应的图片上的坐标.我们通过以下函数来调用查看所 ...
- Pyhton编程(二)之变量、用户输入及条件语句
一:变量 变量定义的规则 1)只能由数字.字母.下划线组成(不能以数字开头) 2)不能使用关键字作为变量名 ['and', 'as', 'assert', 'break', 'class', 'con ...
- visio移动形状 上下左右键 移动滚动条
今天在用visio写作业,想微移visio的形状,于是按上下左右方向键,结果移动的是滚动条. 那么如何让visio按上下左右移动的是形状,其实按Scroll键就好了.
- Hadoop(十四)MapReduce原理分析
前言 上一篇我们分析了一个MapReduce在执行中的一些细节问题,这一篇分享的是MapReduce并行处理的基本过程和原理. Mapreduce是一个分布式运算程序的编程框架,是用户开发“基于had ...
- Debian GNU/Linux 8.4 (jessie)编译安装php.md
编译遇到的问题很多.网上的文章往往是记录遇到的报错,贴上对应的解决. 而实际的环境,如操作系统,安装的软件必然有差异,所以,更重要的是,如何找到解决方法(不担保按步骤做可以编译成功),并将过程自动化. ...
- C++函数重载和函数模板(04)
函数重载 函数重载可以使一个函数名具有多种功能,即具有“多种形态”,这种特性称为多态性. C++的多态性又被直观地称为“一个名字,多个函数”.源代码只指明函数调用,而不说明具体调用哪个函数.编译器的这 ...
- web 开发中的路由是什么意思
路由: 就是一个路径的解析,根据客户端提交的路径,将请求解析到相应的控制器上 从 URL 找到处理这个 URL 的类和函数
- python 使用标准库根据进程名获取进程的pid
有时候需要获取进程的pid,但又无法使用第三方库的时候. 方法适用linux平台. 方法1 使用subprocess 的check_output函数执行pidof命令 from subprocess ...
- DataTable数据修改,换列
增加列 DataTable table= new DataTable(); table.Columns.Add("ID", type ...
- 在VM12中安装 RedHat RHEL7.2 系统的详细步骤
一.开始安装 1)新建虚拟机 RHEL7.2 2)成功引导系统--开机出现此画面 Install Red Hat EnterpriseLinux 7.2 安装RHLE7.2 操作系统 Test th ...