[USACO 13NOV]No Change
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的更多相关文章
- 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 ...
- usaco No Change, 2013 Nov 不找零(二分查找+状压dp)
Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci 元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身 ...
- [题解]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 ...
- 【USACO】Transformations(模拟)
Transformations A square pattern of size N x N (1 <= N <= 10) black and white square tiles is ...
- Codevs 1690 开关灯 USACO
1690 开关灯 USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description YYX家门前的街上有N(2<=N& ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- 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 ...
- 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) ...
- [USACO] 2017 DEC Bronze&Silver
link:http://www.usaco.org/index.php?page=dec17results Problem A(Bronze) 这是一道非常简单的判断重叠面积的题目,但第一次提交仍会出 ...
随机推荐
- Java基础学习笔记二十六 JDBC
什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库,J ...
- 2017-2018-1 Java演绎法 小组成员贡献量汇总
[第一周]贡献量(31) [说明] 完成情况 是指 每次是否全部完成分配的任务,如果全部完成贡献量记为1,否则记为0,与贡献量(时间量)相加计算贡献比例,由于前十周有具体的任务分配,Alpha阶段(第 ...
- verilog学习笔记(1)_两个小module
第一个小module-ex_module module ex_module( input wire sclk,//声明模块的时候input变量一定是wire变量 input wire rst_n,// ...
- Fluent Interface(流式接口)
我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInt ...
- hdu 5274 Dylans loves tree
Dylans loves tree http://acm.hdu.edu.cn/showproblem.php?pid=5274 Time Limit: 2000/1000 MS (Java/Othe ...
- 在360、UC等浏览器,img不加载原因
问题:图片在360浏览器不被加载,在UC浏览器强制不显示. 前言不多说,直接上图. 360浏览器显示情况: UC浏览器显示情况: 由以上两张截图可以看到,在360浏览器,banner图片处根本没有加载 ...
- Vue filter介绍及详细使用
Vue filter介绍及其使用 VueJs 提供了强大的过滤器API,能够对数据进行各种过滤处理,返回需要的结果. Vue.js自带了一些默认过滤器例如: capitalize 首字母大写 uppe ...
- Linq SelectMany 交叉连接
class Student1 { public int Score { get; set; } public Student1(int score) { this.Score = score; } } ...
- 19届华为实习生笔试之判断iPv6地址类型
题二: 答案: #coding=utf-8 import re,sys str = sys.stdin.readline().strip() def regex(str): result = &quo ...
- java中数组、list、泛型集合的长度
1 java中的length属性是针对数组说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性. 2 java中的length()方法是针对字符串String说的,如果想看这 ...