题目链接

题目

题目描述

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的更多相关文章

  1. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  2. NC25025 [USACO 2007 Nov G]Sunscreen

    NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...

  3. USACO 2013 Nov Silver Pogo-Cow

    最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题.被完虐啊 TAT 貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目? Pogo-Cow Descri ...

  4. usaco No Change, 2013 Nov 不找零(二分查找+状压dp)

    Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci 元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身 ...

  5. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  6. USACO翻译:USACO 2013 JAN三题(1)

    USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...

  7. 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 ...

  8. [USACO 2011 Nov Gold] Cow Steeplechase【二分图】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...

  9. Day Tip:SharePoint 2013 *.ascx.g.cs文件

    在开发SharePoint2013的WebPart时,会产生一个*.ascx.g.cs文件.如果用TFS管理源代码经常遇到这个文件丢失.这让人很困扰,如果丢失了请在如下图中添加如下代码:       ...

  10. 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)

    Graph Reconstruction Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Let there ...

随机推荐

  1. AvaloniaUI 取消标题栏,无边框无最大最小化,

    AvaloniaUI 取消标题栏,无边框无最大最小化, 创建一个Window控件 并且在Window中添加以下代码 ExtendClientAreaToDecorationsHint="Tr ...

  2. Oracle数据库如何解决创建用户名开头必须要C##问题?

    1.问题 我们在创建用户,概要文件等时,由于使用的是容器数据库,其文件名必须以C##开头. 我们在学习过程中暂时不需要对齐进行区分,所以如何修改这个设定呢? 2.解决 参考链接如何解决创建用户名开头必 ...

  3. ONVIF网络摄像头(IPC)客户端开发—RTSP RTCP RTP加载H264视频流

    前言: RTSP,RTCP,RTP一般是一起使用,在FFmpeg和live555这些库中,它们为了更好的适用性,所以实现起来非常复杂,直接查看FFmpeg和Live555源代码来熟悉这些协议非常吃力, ...

  4. 【Mysql系列】(二)日志系统:一条更新语句是如何执行的

    有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 这篇文章是从Github ReadMe拷贝的,内容实践下载是没问题的,能够正 ...

  5. [转帖]线上一个隐匿 Bug 的复盘

    前言 之前负责的一个项目上线好久了,最近突然爆出一 Bug,最后评估影响范围将 Bug 升级成了故障,只因为影响的数据量有 10000 条左右,对业务方造成了一定的影响. 但因为不涉及到资金损失,Bu ...

  6. 【转帖】MySQL索引

    数据表如何用索引快速查找 索引是 排好序的快速查找的数据结构 索引存储在文件系统中 索引的文件存储形式与存储引擎有关 索引数据结构:可以是二叉树.红黑树.Hash表.B-Tree.B+Tree 1.二 ...

  7. [转帖]ElasticSearch 最全详细使用教程

    https://zhuanlan.zhihu.com/p/449555826?utm_source=weibo&utm_medium=social&utm_oi=27124941455 ...

  8. [转帖]拜托!面试请不要再问我Spring Cloud底层原理

    https://www.cnblogs.com/jajian/p/9973555.html 概述# 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大 ...

  9. Nginx 系列 | (转)Nginx 上传文件:client_max_body_size 、client_body_buffer_size

    原文:http://php-note.com/article/detail/488 client_max_body_size client_max_body_size 默认 1M,表示 客户端请求服务 ...

  10. 自己想的一些判断存储长度的sql

    create table zhaobsh (t1 date ,t2 TIMESTAMP) insert into zhaobsh values (CURRENT_DATE,CURRENT_TIMEST ...