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(1 <= K <= 16)个硬币,面值的范围是1..100,000,000。

约翰想按顺序买 N个物品(1 <= N <= 100,000),第i个物品需要花费c(i)块钱,(1 <= c(i) <= 10,000)。

在依次进行的购买N个物品的过程中,约翰可以随时停下来付款,每次付款只用一个硬币,支付购买的内容是从上一次支付后开始到现在的这些所有物品(前提是该硬币足以支付这些物品的费用)。不幸的是,商场的收银机坏了,如果约翰支付的硬币面值大于所需的费用,他不会得到任何找零。

请计算出在购买完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

Sample Output

12

HINT

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.

题解

考虑硬币数很少我们将其状压。

令$f[i]$表示状态为$i$时最多能够付款多少。

然后枚举没用过的硬币,考虑用二分查找到最右端位置。

 //It is made by Awson on 2017.10.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Abs(x) ((x) < 0 ? (-(x)) : (x))
#define count COUNT
using namespace std;
const int N = ;
const int SIZE = <<;
int st[]; int k, n;
int ans = -;
int f[SIZE+], a[], c[N+]; int dev(int aim, int l) {
int L = l, R = n, ans = l;
while (L <= R) {
int mid = (L+R)>>;
if (c[mid]-c[l] <= aim) L = mid+, ans = mid;
else R = mid-;
}
return ans;
}
int count(int bit) {
int ans = ;
for (int i = ; i < k; i++, bit >>= ) ans += a[i]*(!(bit&));
return ans;
}
void work() {
st[] = ; for (int i = ; i <= ; i++) st[i] = st[i-]<<;
scanf("%d%d", &k, &n);
for (int i = ; i < k; i++) scanf("%d", &a[i]);
for (int i = ; i <= n; i++) scanf("%d", &c[i]), c[i] += c[i-];
for (int i = ; i < st[k]; i++)
for (int j = ; j < k; j++) if (!(i&st[j])) {
int to = dev(a[j], f[i]);
f[i|st[j]] = Max(f[i|st[j]], to);
if (to == n) ans = Max(ans, count(i|st[j]));
}
printf("%d\n", ans);
}
int main() {
work();
return ;
}

[USACO 13NOV]No Change的更多相关文章

  1. BZOJ3312:[USACO]No Change(状压DP)

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  2. usaco No Change, 2013 Nov 不找零(二分查找+状压dp)

    Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci 元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身 ...

  3. [题解]USACO 1.3 Ski Course Design

    Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...

  4. 【USACO】Transformations(模拟)

    Transformations A square pattern of size N x N (1 <= N <= 10) black and white square tiles is ...

  5. Codevs 1690 开关灯 USACO

    1690 开关灯 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description YYX家门前的街上有N(2<=N& ...

  6. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  7. USACO 1.3 Ski Course Design - 暴力

    Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...

  8. USACO 6.5 Betsy's Tour (插头dp)

    Betsy's TourDon Piele A square township has been divided up into N2 square plots (1 <= N <= 7) ...

  9. [USACO] 2017 DEC Bronze&Silver

    link:http://www.usaco.org/index.php?page=dec17results Problem A(Bronze) 这是一道非常简单的判断重叠面积的题目,但第一次提交仍会出 ...

随机推荐

  1. Java 自定义实现链表

    自定义实现链表很简单,只需要明白链表是什么样子的数据结构. 下图表示一种单向列表.其中指针first指向队头,last指向队尾,curr指向当前读的数据. 下面是我的实现代码,很简单,明白上述结构后, ...

  2. python web——Django架构

    环境:windows/linux/OS 需要的软件:Firefox 浏览器(别的也可以 不过firfox和python的webdriver兼容性好) git版本控制系统(使用前要配置 用户 编辑器可以 ...

  3. JavaScript(第二十六天)【表单处理】

    为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面.   一.表单介绍 在HTML中,表单是由<form>元素来表示的,而在JavaS ...

  4. 【Alpha】阶段总结报告

    团队成员 陈家权 031502107 赖晓连 031502118 雷晶 031502119 林巧娜 031502125 庄加鑫 031502147 一.项目预期计划及现实进展 项目预期计划 现实进展 ...

  5. C语言——第六周作业

    题目 题目一:高速公路超速处罚 1.实验代码 #include <stdio.h> int main() { int speed,maxspeed; double x; scanf(&qu ...

  6. django + nginx + uwsgi + websocket

    最近使用django框架做了一个简单的聊天机器人demo, 开发的过程中使用了django自带的websocket模块,当使用django框架自带的wsgi服务去启动的话,没有什么问题.如果要使用uw ...

  7. vue 内联样式style中的background

    在我们使用vue开发的时候   有很多时候我们需要用到背景图 这个时候会直接使用 内联样式 直接把你拿到的数据拼接上去 注意  在vue中直接使用style时 花括号一定别忘记 还有就是你的url一定 ...

  8. linux下面的打包压缩命令

    tar命令 tar [-cxtzjvfpPN] 文件与目录 ....linux下面压缩之前要把一堆文件打个包再压缩,即使只有一个文件也需要打个包.例子:tar czvf 1.tar.gz hello. ...

  9. Lock(三)查看是谁把表给锁了

    查看是谁把表给锁了 select se1.inst_id as 被阻塞的会话节点, se2.inst_id as 罪魁祸首节点, se1.sid as 被阻塞的会话ID, ob.object_name ...

  10. xftp上传文件失败,执行程序发现磁盘满了:No space left on device

    参考链接 No space left on device 解决Linux系统磁盘空间满的办法http://www.cnblogs.com/aspirant/p/3604801.html如何解决linu ...