【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分
【BZOJ3312】[Usaco2013 Nov]No Change
Description
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
Input
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.
Output
* Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.
Sample Input
12
15
10
6
3
3
2
3
7
INPUT DETAILS: FJ has 3 coins of values 12, 15, and 10. He must make purchases in sequence of value 6, 3, 3, 2, 3, and 7.
Sample Output
OUTPUT DETAILS: FJ spends his 10-unit coin on the first two purchases, then the 15-unit coin on the remaining purchases. This leaves him with the 12-unit coin.
题解:一开始我心血来潮想用单调队列做,结果发现枚举硬币的顺序会对结果产生影响,所以必须用二维的单调队列,默默放弃~
本题用状压DP是裸题,用f[i]表示状态为i是最多买了几件物品,对于每个状态二分查找每个硬币最多能卖连续的几件物品,就没什么了
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,ans,sum;
int w[20],s[100010],c[100010],f[1<<17],rem[1<<17];
int main()
{
scanf("%d%d",&n,&m);
int i,j,k,t,l,r,mid;
for(i=1;i<=n;i++) scanf("%d",&w[i]),rem[0]+=w[i];
for(i=1;i<=m;i++) scanf("%d",&c[i]),s[i]=s[i-1]+c[i];
ans=-1;
for(i=1;i<(1<<n);i++)
{
for(j=1;j<=n;j++)
{
if(((1<<j-1)&i)==(1<<j-1))
{
t=i^(1<<j-1);
rem[i]=rem[t]-w[j];
l=f[t]+1,r=m+1;
while(l<r)
{
mid=l+r>>1;
if(s[mid]-s[f[t]]<=w[j]) l=mid+1;
else r=mid;
}
f[i]=max(f[i],l-1);
if(f[i]==m) ans=max(ans,rem[i]);
}
}
}
printf("%d",ans);
return 0;
}
【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分的更多相关文章
- 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- bzoj3312: [Usaco2013 Nov]No Change
题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...
- [BZOJ3312][USACO]不找零(状压DP)
Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身上 ...
- [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)
传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...
- HDU-3681-Prison Break(BFS+状压DP+二分)
Problem Description Rompire is a robot kingdom and a lot of robots live there peacefully. But one da ...
- BZOJ3312:[USACO]No Change(状压DP)
Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...
- LG3092 「USACO2013NOV」No Change 状压DP
问题描述 https://www.luogu.org/problem/P3092 题解 观察到 \(k \le 16\) ,自然想到对 \(k\) 状压. 设 \(opt[i]\) 代表使用硬币状况为 ...
- P3092 [USACO13NOV]没有找零No Change 状压dp
这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了. 题干: 题目描述 Farmer John <= K <= ), each with value .., ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
随机推荐
- StarRTC , AndroidThings , 树莓派小车,公网环境,视频遥控(一)准备工作
原文地址:http://blog.starrtc.com/?p=48 啥也不说,先来个视频看看效果 视频播放器 00:00 00:54 概述为了体现StarRTC的实时音视频传输能 ...
- 05 Oracle process
本章提要----------------------------------------------所有的 process 都是在 PGA 内(memory)server process: 与 cli ...
- JQuery _ 定时器(jQuery Timers) 学习
jQuery Timers插件地址: http://plugins.jquery.com/project/timers JQuery Timers应用知识 提供了三个函式 1. everyTime(时 ...
- nginx报错:No package erlang available
问题 yum install erlang 报错:No package erlang available 同样的,如果我们在安装nginx的时候出现"No package nginx ava ...
- Android ListView 笔记
ListView以列表的形式展示数据内容. 布局文件如下所示,添加一个ListView. activity_main.xml <?xml version="1.0" enco ...
- samba的使用
第一步:了解它 为了实现Window主机与Linux服务器之间的资源共享,Linux操作系统提供了 Samba服务,Samba服务为两种不同的操作系统架起了一座桥梁,使 Linux 系统和Window ...
- SyntaxError: inconsistent dedent
错误原因: 一般是拷贝别人的代码过来编辑,由于编辑器不同,出现 tab和 space 的差别. 解决方法: Window->Preferences->PyDev->Editor-&g ...
- 时间操作(struct tm、time_t)求指定日期 前n天的日期
1.在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFINED struct tm { int tm_sec; /* 秒–取值 ...
- 【转】IIS日志-网站运维的好帮手
对于一个需要长期维护的网站来说,如何让网站长久稳定运行是件很有意义的事情. 有些在开发阶段没有暴露的问题很有可能就在运维阶段出现了,这也是很正常的. 还有些时候,我们希望不断地优化网站,让网站更快速的 ...
- Spark-Hadoop、Hive、Spark 之间是什么关系?(转)
大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞生的.你可以把它比作一个厨房所以需要的各种工具.锅碗瓢盆,各有各的用处,互相之间又有重合.你可 ...