题目描述

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.

K个硬币,要买N个物品。

给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的。现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1

输入

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.

样例输入

3 6
12
15
10
6
3
3
2
3
7

样例输出

12


题解

状压dp+二分

f[i]表示硬币使用状态为i时最多能购买的物品

那么有f[i]=k (sum[k]-sum[f[i^(1<<j)]]≤v[j])。

然后二分查找求出k即可。

还是注意数组从1开始的问题。

#include <cstdio>
#include <algorithm>
using namespace std;
int f[70000] , v[20] , a[100010] , sum[100010] , cost[70000] , n , base[70000];
int search(int z , int c)
{
int l = z , r = n , mid , ans = -1;
while(l <= r)
{
mid = (l + r) >> 1;
if(sum[mid] - sum[z] <= c)
ans = mid , l = mid + 1;
else r = mid - 1;
}
return ans;
}
int main()
{
int k , i , j , tmp , ans = -1 , sn = 0;
scanf("%d%d" , &k , &n);
for(i = 1 ; i <= k ; i ++ )
scanf("%d" , &v[i]) , sn += v[i] , base[1 << (i - 1)] = i;
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &a[i]) , sum[i] = sum[i - 1] + a[i];
for(i = 1 ; i < (1 << k) ; i ++ )
{
for(j = 1 ; j <= k ; j ++ )
{
if((1 << (j - 1)) & i)
{
tmp = search(f[i ^ (1 << (j - 1))] , v[j]);
if(tmp != -1)
f[i] = max(f[i] , tmp);
}
}
}
for(i = 1 ; i < (1 << k) ; i ++ )
{
cost[i] = cost[i - (i & (-i))] + v[base[i & (-i)]];
if(f[i] == n) ans = max(ans , sn - cost[i]);
}
printf("%d\n" , ans);
return 0;
}

【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分的更多相关文章

  1. 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分

    [BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...

  2. bzoj3312: [Usaco2013 Nov]No Change

    题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...

  3. BFS+状态压缩DP+二分枚举+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others)   ...

  4. 【bzoj3886】[Usaco2015 Jan]Moovie Mooving 状态压缩dp+二分

    题目描述 Bessie is out at the movies. Being mischievous as always, she has decided to hide from Farmer J ...

  5. 【bzoj1231】[Usaco2008 Nov]mixup2 混乱的奶牛 状态压缩dp

    题目描述 混乱的奶牛[Don Piele, 2007]Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= ...

  6. 【bzoj1725】[USACO2006 Nov]Corn Fields牧场的安排 状态压缩dp

    题目描述 Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧场上的某几格土 ...

  7. bzoj 3312: [Usaco2013 Nov]No Change

    3312: [Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for his ...

  8. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

  9. HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

    Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...

随机推荐

  1. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

  2. WebRTC中Android Demo中的摄像头从采集到预览流程

    APPRTC-Demo调用流程 1.CallActivity#onCreate 执行startCall开始连接或创建房间 2.WebSocketClient#connectToRoom 请求一次服务器 ...

  3. python通过mongoengine中connect函数连接多个数据库

    mongoengine支持程序同时连接多个数据库,这些数据库可以位于一个或多个mongo之中,通过alias名称区分不同的连接即可. 可以通过switch_db切换到不同的数据库,进行读写操作,swi ...

  4. 【BZOJ3991】寻宝游戏(动态规划)

    [BZOJ3991]寻宝游戏(动态规划) 题面 BZOJ 题解 很明显,从任意一个有宝藏的点开始,每次走到相邻的\(dfs\)的节点就行了. 证明? 类似把一棵树上的关键点全部标记出来 显然是要走一个 ...

  5. 【Hadoop】Seondary NameNode不是备份NameNode!!

    昨天和舍友聊天时无意中提起Secondary NameNode,他说这是备用NameNode.我当时就有点疑惑..之后查阅了相关资料和博客,算是基本理解了什么是Secondary NameNode. ...

  6. linux_fdisk命令详解,关于分区的详解

    这篇文章写的十分详细,特别的好 fdisk -l 可以列出所有的分区,包括没有挂上的分区和usb设备.我一般用这个来查找需要挂载的分区的位置,比如挂上u盘. 实例解说Linux中fdisk分区使用方法 ...

  7. HashMap在并发场景下踩过的坑

    本文来自网易云社区 作者:张伟 关于HashMap在并发场景下的问题有很多人,很多公司遇到过!也很多人总结过,我们很多时候都认为这样都坑距离自己很远,自己一定不会掉入这样都坑.可是我们随时都有就遇到了 ...

  8. 40套PSD欧美扁平化网页模板,可二次编辑开发,精品

    40套PSD欧美扁平化网页模板,可二次编辑开发,绝对精品,下载地址:百度网盘, https://pan.baidu.com/s/1uMF4MM_3UC2Q6mbyNomLfQ 模板内容预览:   小

  9. hdu2199Can you solve this equation?(解方程+二分)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  10. 209. First Unique Character in a String

    Description Find the first unique character in a given string. You can assume that there is at least ...