题目描述

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.

装压dp,WA了很久,少写了=号,满状态没枚举到

#include<cstdio>
#include<algorithm>
const int maxn = ;
inline int read() {
int x=, f=;
char c=getchar() ;
while(c<''||c>''){ if(c=='-')f=-;c=getchar();};
while(c<=''&&c>='')x=x*+c-'',c=getchar();
return x*f;
}int n,k;
int moe[maxn],thi[maxn*];
int dp[<<maxn];//当前状态能够购买的最多物件数
int main() {
int tot=;
k=read(),n=read();
for(int i=;i<=k;++i) moe[i]=read(),tot+=moe[i];
for(int i=;i<=n;++i) thi[i]=read(),thi[i]+=thi[i-];
int kn=(<<k)-;
//printf("%d\n",tot);
//printf("%d\n",thi[n]);
int ans=-;
for(int i=;i<=kn;++i) {
for(int j=;j<=k;++j) {
if(i&(<<j-)) {
int popo=i^(<<j-);
int l=dp[popo],r=n,tt=-;
while(l<=r) {
int mid=(l+r)>>;
if(thi[mid]-thi[dp[popo]]<=moe[j]) tt=mid,l=mid+;
else r=mid-;
}
dp[i]=std::max(dp[i],tt);
if(dp[i]==n) {
int tmp=;
for(int q=;q<=k;++q) {
if(i&(<<q-))tmp+=moe[q];
}
ans=std::max(ans,tot-tmp);
}
}
}
} printf("%d\n",ans);
return ;
}

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

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

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

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

    P3092 [USACO13NOV]没有找零No Change 题目描述 Farmer John is at the market to purchase supplies for his farm. ...

  3. 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. 洛谷 P3092 [USACO13NOV]没有找零No Change

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

  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. Nodejs-模块化结构

    1.模块(一个文件就是一个模块) 获取当前脚本所在的路径 _ _dirname 文件路径 _ _filename (1)创建模块(module1.js) const fs=require('fs'); ...

  2. loj2058 「TJOI / HEOI2016」求和

    推柿子 第二类斯特林数的容斥表达 fft卡精度就用ntt吧qwq. #include <iostream> #include <cstdio> using namespace ...

  3. MongoDB快速入门学习笔记4 MongoDB的文档查询操作

    先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...

  4. Robotium测试报告的生成方法(下)

    7.4 测试报告优化 通过上面的三种方法,我们都可以得到一个Xml格式的测试报告,不过这不是我们想要的,因为这样的报告读起来很费劲,而且这样的报告发给领导们也是不行的.所以我们要美化一下才行,一般都是 ...

  5. Python学习-day16-DOM

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  6. CSU-1985 驱R符

    CSU-1985 驱R符 Description 阴阳师中有三中稀有度的式神R,SR,SSR,其中R的稀有度最低,每次抽符,会随机得到一种式神,然而子浩君对R式神已经深恶痛绝. 某天,子浩君突然发现, ...

  7. 快速排序(Quick Sort)及优化

    原理介绍 通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成 ...

  8. 【转】UGUI之用脚本动态的改变Button的背景图片 和 颜色

    http://blog.csdn.net/u014771617/article/details/45102701 public Button button;void Start(){ColorBloc ...

  9. web常见攻击总结

    1.Sql注入 攻击者把sql命令插入到web表单的输入域或页面请求的查询字符串, 欺骗服务器执行恶意的sql命令 防御措施 前端: 1.正则验证字符串格式 2.过滤字符串的非法字符 后端: 1.不要 ...

  10. webstorm不能使用stylus

    1.https://stackoverflow.com/questions/23583514/webstorm-8-showing-errors-for-a-correct-html-tag 2. & ...