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.

输入输出样例

输入样例#1:

3 6
12
15
10
6
3
3
2
3
7
输出样例#1:

12

说明

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.

/*
因为是顺序购买,所以状态s的答案有关的是使用顺序。之后就有状态f[s]表示用状态s最多可买多少物品。最后因为是金组的T3所以一定要有坑,在算状态最大值的时候要套一个二分。
*/
#include<iostream>
#include<cstdio>
using namespace std;
int k,n;//k个硬币,n个物品
int s[],a[],f[<<],ans=-;
int find(int l,int r,int x){
int p=l,res=l,mid;
while(l<=r){
mid=(l+r)>>;
if(s[mid]-s[p]<=a[x])res=mid,l=mid+;
else r=mid-;
}
return res;
}
int zhao_ling(int x){
int res=;
for(int i=;i<k;i++)
if((x&(<<i))==)res+=a[i+];
return res;
}
int main(){
freopen("Cola.txt","r",stdin);
scanf("%d%d",&k,&n);
for(int i=;i<=k;i++)scanf("%d",&a[i]);
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
s[i]=s[i-]+x;
}
for(int sta=;sta<(<<k);sta++){
int mx=;
for(int i=;i<=k;i++){
if(sta&(<<(i-))){
int tmp=f[sta-(<<(i-))];
mx=max(mx,find(tmp,n,i));
}
}
f[sta]=mx;
if(mx==n)ans=max(ans,zhao_ling(sta));
}
printf("%d",ans);
}

洛谷P3092 [USACO13NOV]没有找零No Change的更多相关文章

  1. 洛谷 P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  2. P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  3. 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 ...

  4. Luogu P3092 [USACO13NOV]没有找零No Change【状压/二分】By cellur925

    题目传送门 可能是我退役/NOIP前做的最后一道状压... 题目大意:给你\(k\)个硬币,FJ想按顺序买\(n\)个物品,但是不能找零,问你最后最多剩下多少钱. 注意到\(k<=16\),提示 ...

  5. P3092 [USACO13NOV]没有找零No Change 状压dp

    这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了. 题干: 题目描述 Farmer John <= K <= ), each with value .., ...

  6. [USACO13NOV]没有找零No Change [TPLY]

    [USACO13NOV]没有找零No Change 题目链接 https://www.luogu.org/problemnew/show/3092 做题背景 FJ不是一个合格的消费者,不知法懂法用法, ...

  7. [洛谷P3092]【[USACO13NOV]没有找零No Change】

    状压\(DP\) + 二分 考虑构成:\(k<=16\)所以根据\(k\)构造状压\(dp\),将所有硬币的使用情况进行状态压缩 考虑状态:数组\(dp[i]\)表示用\(i\)状态下的硬币可以 ...

  8. 【[USACO13NOV]没有找零No Change】

    其实我是点单调队列的标签进来的,之后看着题就懵逼了 于是就去题解里一翻,发现楼上楼下的题解说的都好有道理, f[j]表示一个再使用一个硬币就能到达i的某个之前状态,b[now]表示使用那个能使状态j变 ...

  9. [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)

    传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...

随机推荐

  1. Java8中聚合操作collect、reduce方法详解

    Stream的基本概念 Stream和集合的区别: Stream不会自己存储元素.元素储存在底层集合或者根据需要产生.Stream操作符不会改变源对象.相反,它会返回一个持有结果的新的Stream.3 ...

  2. nodejs 解析 base64 文本

    使用Buffer对象,在创建Buffer,指定源的编码方式 Buffer.from(data, 'base64').toString(); 当然如果是解析图片或者二进制数据的话,就不需要toStrin ...

  3. JavaWEB - 请求的转发和重定向

    JavaWEB - Servlet

  4. CRtmpServer

    1.  前言 crtmpserver是一个由C++语言编写的开源的RTMP流媒体服务器,官方网站是www.rtmpd.com 2.   CRtmpServer编译   2.1.  Win7+Vs201 ...

  5. java将白色背景图片转换成透明图片

    package evecom.image; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buffe ...

  6. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  7. 让Django支持数据库长连接(可以提高不少性能哦)

    书接上回 上回我们说到:<在生产系统使用Tornado WebServer来代替FastCGI加速你的Django应用> 那么现在很流行用一些高性能的nonblock的app server ...

  8. MySQL 和 InnoDB

    发现一篇总结的很不错的文章,转一下 (原文作者:Draveness   原文链接:https://draveness.me/mysql-innodb) 作为一名开发人员,在日常的工作中会难以避免地接触 ...

  9. BZOJ1217:[HNOI2003]消防局的设立

    我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...

  10. HDOJ5438(图的各个连通分量遍历)

    #include<cstdio> #include<cstring> using namespace std; ; template<class T> struct ...