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. Mysql5.7.17中文乱码问题

    写Java web调数据库,老是出现汉字乱码,一直没理睬,今天决定好好"整治"一下,却发现并没有那么简单.从网上找的方法,大部分都尝试了一遍都有一些问题. 有的改完了,数据库启动不 ...

  2. 201621123050 《Java程序设计》第2周学习总结

    1.本周学习总结 java的数据类型 基本数据类型:介绍了java特有的boolean 引用数据类型 String:不变性:需要频繁修改时使用StringBuilder 包装类:自动拆.装箱 数组 一 ...

  3. bzoj千题计划108:bzoj1018: [SHOI2008]堵塞的交通traffic

    http://www.lydsy.com/JudgeOnline/problem.php?id=1018 关键点在于只有两行 所以一个2*m矩形连通情况只有6种 编号即对应代码中的a数组 线段树维护 ...

  4. HAOI 2012 高速公路

    https://www.luogu.org/problem/show?pid=2221 题目描述 Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这 ...

  5. 关于移动web教程免费发布

    各位老铁大家好,最近经历了太多太多,精力一直不能集中做自己愿意做的事情. 移动Web课程一开始设置收费10块,其实本意是让大家感觉有支出,就会相对珍惜好好学习,但是发现收费把大部分人挡在门外,现在恢复 ...

  6. 看到一个对CAP简单的解释

    一个分布式系统里面,节点组成的网络本来应该是连通的.然而可能因为一些故障,使得有些节点之间不连通了,整个网络就分成了几块区域.数据就散布在了这些不连通的区域中.这就叫分区.当你一个数据项只在一个节点中 ...

  7. VMware-vCenter-Server-Appliance VCSA升级步骤

    1.下载ZIP升级文件并解压 2.打开HFS,把解压后的文件夹拖到"Virtual File System"下,在弹出的对话框中点击"Virtual folder&quo ...

  8. kubernetes 手绘画,先收藏一下

  9. 修改了SpringBoot的主类名称后,gradle build报错的解决办法

    Unable to find a single main class from the following candidates [*.*Application]

  10. 论文泛读·Adversarial Learning for Neural Dialogue Generation

    原文翻译 导读 这篇文章的主要工作在于应用了对抗训练(adversarial training)的思路来解决开放式对话生成(open-domain dialogue generation)这样一个无监 ...