Luogu3092:[USACO13NOV]No Change

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu3092:[USACO13NOV]No Change相关的知识,希望对你有一定的参考价值。

题面

传送门

Sol

状压一下\(k\)\(f[S]\)表示用过的硬币集合为\(S\)能买到的物品个数

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(100005);

IL ll Input(){
    RG ll x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int k, coin[_], n, cost[_], sum[_], f[65536], ans = -1, mi[20];

int main(RG int argc, RG char* argv[]){
    k = Input(); n = Input();
    for(RG int i = 1; i <= k; ++i) coin[i] = Input();
    for(RG int i = 1; i <= n; ++i){
        cost[i] = Input();
        sum[i] = sum[i - 1] + cost[i];
    }
    mi[1] = 1;
    for(RG int i = 2; i <= k; ++i) mi[i] = mi[i - 1] << 1;
    RG int S = 1 << k;
    for(RG int i = 0; i < S; ++i)
        for(RG int j = 1; j <= k; ++j){
            if(i & mi[j]) continue;
            RG int pos = lower_bound(sum + 1, sum + n + 1, coin[j] + sum[f[i]]) - sum;
            if(pos > n || sum[pos] > coin[j] + sum[f[i]]) --pos;
            f[i | mi[j]] = max(f[i | mi[j]], pos);
        }
    for(RG int i = 0; i < S; ++i)
        if(f[i] == n){
            RG int cnt = 0;
            for(RG int j = 1; j <= k; ++j)
                if(~i & mi[j]) cnt += coin[j];
            ans = max(ans, cnt);
        }
    printf("%d\n", ans);
    return 0;
}

以上是关于Luogu3092:[USACO13NOV]No Change的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 P3092 [USACO13NOV]没有找零No Change

LG3092 「USACO2013NOV」No Change 状压DP

[USACO13NOV]没有找零No Change [TPLY]

P3092 [USACO13NOV]没有找零No Change

P3092 [USACO13NOV]没有找零No Change

[luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)