NC24416 [USACO 2013 Nov G]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.
输入描述
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
输出
12
说明
INPUT DETAILS:
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.
OUTPUT DETAILS:
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,二分。
先前缀和货物价值,方便查找加能到达的不大于上一次价值加上硬币价值的最大货物价值,之后就是个TSP解法。
时间复杂度 \(O(m2^m\log n)\)
空间复杂度 \(O(n+2^m)\)
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int c[20], a[100007], dp[(1 << 16) + 7];
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int m, n;
cin >> m >> n;
for (int i = 1;i <= m;i++) cin >> c[i];
for (int i = 1;i <= n;i++) cin >> a[i], a[i] += a[i - 1];
ll ans = -1;
for (int i = 0;i < (1 << m);i++) {
ll sum = 0;
for (int j = 1;j <= m;j++) {
if (!(i & (1 << (j - 1)))) {
sum += c[j];
continue;
}
int k = upper_bound(a + 1, a + n + 1, a[dp[i ^ (1 << (j - 1))]] + c[j]) - a - 1;
dp[i] = max(dp[i], k);
}
if (dp[i] == n) ans = max(ans, sum);
}
cout << ans << '\n';
return 0;
}
NC24416 [USACO 2013 Nov G]No Change的更多相关文章
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- NC25025 [USACO 2007 Nov G]Sunscreen
NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...
- USACO 2013 Nov Silver Pogo-Cow
最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题.被完虐啊 TAT 貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目? Pogo-Cow Descri ...
- usaco No Change, 2013 Nov 不找零(二分查找+状压dp)
Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci 元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身 ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2013 JAN三题(1)
USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem G
Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...
- [USACO 2011 Nov Gold] Cow Steeplechase【二分图】
传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...
- Day Tip:SharePoint 2013 *.ascx.g.cs文件
在开发SharePoint2013的WebPart时,会产生一个*.ascx.g.cs文件.如果用TFS管理源代码经常遇到这个文件丢失.这让人很困扰,如果丢失了请在如下图中添加如下代码: ...
- 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
Graph Reconstruction Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge Let there ...
随机推荐
- iview 表单有值却校验失败
转载请注明出处: iview 表单校验数值的时候,表单有值,却在提交的时候,提示表单校验失败: 解决方案: 1. IviewUI的文档里查到了rules规则里面有个校验类型的属性字段type rule ...
- NSSCTF Round#11 Basic 密码个人赛复盘
[NSSRound#11 Basic]ez_enc ABAABBBAABABAABBABABAABBABAAAABBABABABAAABAAABBAABBBBABBABBABBABABABAABBAA ...
- http-长连接
1. 短链接 http1.0 -- 1个请求-响应过程会创建且1个新的连接 2. 长连接 http1.1 -- 同域下可以创建1个tcp连接,多个请求在同一个tcp上串行处理请求 http2.0 -- ...
- 【秘籍揭秘】如何高速下载游戏、Switch资源?省时省力一网打尽!
百度云盘SVIP合租啦亲爱的考研党和游戏玩家们,我今天要分享的是一份独家秘籍!你是不是常常为下载游戏或Switch资源而烦恼?是不是经常遇到下载速度慢.限速等问题,让你等待无尽?别担心,我有一个绝密的 ...
- [转帖]003、体系结构之TiKV持久化
TiKV架构和作用 数据持久化 分布式一致性 MVCC 分布式事务 Coprocessor coprocessor : 协同处理器. 可以将一些SQL计算交给TiKV处理.不需要将TiKV所有数据通过 ...
- ext4 磁盘扩容
目录 ext4文件系统磁盘扩容 目标 途径 操作步骤 改变前的现状 操作和改变后的状态 ext4文件系统磁盘扩容 一个磁盘有多个分区,分别创建了物理卷.卷组.逻辑卷.通过虚拟机软件对虚拟机的磁盘/de ...
- [转帖] 在Linux上查看活跃线程数与连接数
https://www.cnblogs.com/codelogs/p/17178675.html 原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,非公众号转载保留此声明. 简介# 现 ...
- nginx 进行目录浏览的简单配置
1. 公司网络安全不让用vsftpd的匿名网络访问了, 没办法 只能够使用 nginx 通过http协议来处理. 2. 最简单的办法就是另外开一个nginx进程简单设置一下nginx的配置文件 wor ...
- top的简单学习
获取当前进程的全部线程 jps 获取jvm的进程信息. top -Hp $pid -bn 1 > 1.txt 可以获取当前特定进程的所有子进程. 注意linux与Windows的不太一样. li ...
- 【如何提高IT运维效率】深度解读京东云基于NLP的运维日志异常检测AIOps落地实践
作者:京东科技 张宪波.张静.李东江 基于NLP技术对运维日志聚类,从日志角度快速发现线上业务问题 日志在IT行业中被广泛使用,日志的异常检测对于识别系统的运行状态至关重要.解决这一问题的传统方法需 ...