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 ...
随机推荐
- com.alibaba.fastjson.JSONException: create instance error
很早之前在使用FashJson进行实体类转化的时候,如果json参数是多层都是一层对应一个单独的实体类, 今天在项目中想,使用内部类是不是也可以实现,且使用内部类封装性更好.当将json串使用fast ...
- Cortex M3 - NVIC(中断向量控制器)
NVIC-概述 nested vector interrupt control - 内嵌向量中断控制器 传统ARM中断控制在Core的外部,软件接收到中断之后,需要查中断的编号,然后启动相应的中断处理 ...
- css : object-fit 兼容 ie 的解决方案
通过 github 搜索 object-fit ie , 借鉴大佬兼容 ie 的经验. 下载解压到文件夹 , 打开测试目录 , 查看 demo 使用 ie 打开demo , 查看显示效果 : 代码 ...
- java - 数组降序输出
package array; import java.util.Arrays; /** * 降序 */ public class Reverse { public static void main(S ...
- 常见的docker hub mirror镜像仓库
阿里云(杭州) https://registry.cn-hangzhou.aliyuncs.com 阿里云(上海) https://registry.cn-shanghai.aliyuncs.com ...
- Linux上面Shell简单进行数值计算的办法
1. 自己简单写了一个脚本 来计算 一个服务进程启动的耗时, 精度要求不高 10秒上下就可以. 在程序执行之前和之后的处理 在最前面设置一句time1=`date +%s`在末尾一句设置time2=` ...
- Linux 开启防火墙 避免非干系人误操作的处理
公司里面进行系统集成测试. 不想让开发能够更改我的服务器信息, 但是改密码又太麻烦了. 想了想还是用 防火墙好一些. 第一步 开启防火墙 systemctl enable firewalld syst ...
- HTTPD 搭建正向代理 使无网络访问权限的服务器能够访问互联网服务的快捷办法
背景 公司有保密要求比较高,数据安全要求比较高的企业客户,要求核心业务服务器部允许直接访问互联网,但是因为我们有一些OCR识别以及发票查验等的场景需要连接云端的服务才可以正常使用, 所以这里面就存在安 ...
- Python设计模式:你的代码真的够优雅吗?
当涉及到代码优化时,Python作为一种高级编程语言,具有广泛的应用领域和强大的功能.在软件开发中,设计模式是一种被广泛采用的解决问题的方案,它提供了一种在特定情境中重复使用的可行方案.在Python ...
- zookeeper的Leader选举源码解析
作者:京东物流 梁吉超 zookeeper是一个分布式服务框架,主要解决分布式应用中常见的多种数据问题,例如集群管理,状态同步等.为解决这些问题zookeeper需要Leader选举进行保障数据的强一 ...