• 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
    ", ans);
    	return 0;
    }
    
    
  • 相关阅读:
    shell编程系列5--数学运算
    qperf测量网络带宽和延迟
    使用gprof对应用程序做性能评测
    [转]极不和谐的 fork 多线程程序
    Emacs显示光标在哪个函数
    Iterm2的一些好用法
    [转]最佳日志实践
    Deep Introduction to Go Interfaces.
    CGo中传递多维数组给C函数
    seaweedfs 源码笔记(一)
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8365227.html
Copyright © 2020-2023  润新知