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 ...
随机推荐
- 【TouchGFX 】使用 CubeMX 创建 TouchGFX 工程时 LCD 死活不显示
生成的代码死活无法让LCD显示,经两个晚上的分析验证是LTDC_CLK引脚速度设置为低速导致,经测试中速.高速.超高速都正常,真是冤,聊以此以示纪念
- JMS微服务开发示例(七)使用 Serilog 作为日志提供者
nuget 引入: Serilog.Extensions.LoggingSerilog.Settings.ConfigurationSerilog.Sinks.ConsoleSerilog.Sinks ...
- [转帖]Oracle nvarchar2存储特殊字符乱码问题
https://www.cnblogs.com/PiscesCanon/p/15157506.html 这个问题研究了一天多,终于搞定了. 起因是业务需要存特殊字符'ø'到varchar2的字段中出现 ...
- [转帖]Linux内核线程kthread简介【最好的一篇!】
https://zhuanlan.zhihu.com/p/581587583 Linux内核可以看作一个服务进程(管理软硬件资源,响应用户进程的种种合理以及不合理的请求).内核需要多个执行流并行,为了 ...
- [转帖]【性能】中断绑定和查看|irqbalance 中断负载均衡|CPU瓶颈
常用命令 ``` # 查看当前运行情况 service irqbalance status # 终止服务 service irqbalance stop 取消开机启动: chkconfig irqba ...
- [转帖]skywalking配置nacos集群模式
版本: name version nacos 1.1.0 skywalking 6.2.0 elasticsearch 6.3.2 es集群管理工具 cerebro-0.8.3 https://git ...
- 计划任务方式定期获取jvm dump的方法
说明 产品最近有一些问题,想着能够每隔一段时间抓取一下dump文件. 需求 可以定期抓取, 需要注意磁盘空间的使用. 实现方法 定时任务使用 crontab 计划任务来做 预定义获取jvm dump的 ...
- Jumper Server 堡垒机搭建过程
Jumper Server 堡垒机搭建过程 背景说明 公司组织考核, 要对一套系统进行安全设置.有一个项目是使用堡垒机进行登录 堡垒机有多种用途,可以实现日志审计和安全设置等. 买商业设备的话太困难了 ...
- 文心一言 VS 讯飞星火 VS chatgpt (186)-- 算法导论14.1 3题
三.用go语言,写出OS-SELECT的非递归版本. 文心一言: 下面是使用Go语言编写的OS-SELECT的非递归版本: func OS_SELECT(T *Node, k int) *Node { ...
- docker 安装minio
1.拉取镜像 docker pull minio/minio 2.运行容器 docker run -d -p 9000:9000 --name=minio --restart=always -e &q ...