NC24416 [USACO 2013 Nov G]No Change
2022/9/4 6:22:54
本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11国产医疗级心电ECG采集处理模块
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南