题目描述

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. 快速创建一个vue项目

    vue脚手架 用来创建vue项目的工具包 创建项目: npm install -g vue-cli vue init webpack VueDemo 开发环境运行: cd VueDemo npm in ...

  2. MySQL高级第二章——索引优化分析

    一.SQL性能下降原因 1.等待时间长?执行时间长? 可能原因: 查询语句写的不行 索引失效(单值索引.复合索引) CREATE INDEX index_user_name ON user(name) ...

  3. 如何使用GeoServer发布地图

    本文所采用的系统为Windows 10 64bit操作系统,使用FireFox浏览器 一.安装配置Java的SDK 1. 安装JavaDevelopment Kit (JDK) 8,java开发环境, ...

  4. 使用OpenLayers发布地图

    OpenLayers是用于制作交互式Web地图的开源客户端JavaScript类库,制作的地图几乎可以在所有的浏览器中查看.因为是客户端类库,它不需要特殊的服务器端软件或配置,甚至不用下载任何东西就可 ...

  5. Luogu P1802 5倍经验日_KEY

    题目传送门 ·背包 这可以说是一道背包的变形. 首先需要考虑到的是如何将ta转换为一个正常 的背包. 这些数据有一个让我们都十分不爽的地方就是有两个值. 所以我们就设置一个基准值,将失败的经验值当做基 ...

  6. 北京Uber优步司机奖励政策(10月19日~10月25日)

    用户组:优步北京人民优步A组(适用于10月19日-10月25日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万 ...

  7. Git笔记——01

    Git - 幕布 Git   教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...

  8. 第一阶段·Linux运维基础-第2章·Linux系统目录结构介绍

    01 变量与PS1 02 添加用户 03 关闭SELinux 04 关闭iptables 05 显示中文乱码排查过程 06 总结 07 目录结构课程内容 08 Linux目录结构特点 09 Linux ...

  9. lesson 23 one man's meat is another man's poison

    lesson 23 one man's meat is another man's poison delicacy n. 美味:佳肴: delicious adj. 美味的:可口的 关于虚拟语气: I ...

  10. 初学Direct X(9) ——文字的显示

    初学Direct X(9) --文字的显示 本次学习如何使用ID3DXFont创建字体,使得我们可以在任何安装了Windows系统中TrueType字体来打印文字,不过最好使用标准字体,这样文字在每一 ...