码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu3092:[USACO13NOV]No Change

时间:2018-01-27 13:33:17      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:inpu   har   ret   gis   include   mat   www   getch   logs   

题面

传送门

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

标签:inpu   har   ret   gis   include   mat   www   getch   logs   

原文地址:https://www.cnblogs.com/cjoieryl/p/8365227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!