1. 程式人生 > >Luogu3092:[USACO13NOV]No Change

Luogu3092:[USACO13NOV]No Change

www. ont ref blog spa () pri c++ ace

題面

傳送門

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