ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC24416 [USACO 2013 Nov G]No Change

2022-09-04 03:00:51  阅读:181  来源: 互联网

标签:purchases his No int money USACO 12 FJ 2013


题目链接

题目

题目描述

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;
}

标签:purchases,his,No,int,money,USACO,12,FJ,2013
来源: https://www.cnblogs.com/BlankYang/p/16654154.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有