BZOJ3312:[USACO]No Change(状压DP)
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.
Solution
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#define N (100000+10)
using namespace std; int pay[][N],a[N],c[N],num[N],f[][N];
int n,m; int main()
{
scanf("%d%d",&n,&m);
for (int i=; i<=n; ++i)
scanf("%d",&a[i]);
for (int i=; i<=m; ++i)
scanf("%d",&c[i]);
for (int i=; i<=(<<n)-; ++i)
{
int x=i,cnt=;
while (x){if (x&) cnt++; x>>=;}
num[i]=cnt;
} int p=;
for (int i=; i<=n; ++i)
{
int sum=,l=;
for (int j=; j<=m; ++j)
{
sum-=c[j-];
while (sum+c[l]<=a[i] && l<=m)
sum+=c[l++];
pay[i][j]=l-;
}
} for (int i=; i<=n; ++i)//当前硬币
for (int j=; j<=(<<n)-; ++j)//上一个的状态
if (num[j]==i-)
for (int k=; k<=n; ++k)
if ((j|(<<k-))!=j)
f[i][j|(<<k-)]=max(f[i][j|(<<k-)],pay[k][f[i-][j]+]); int ans=-;
for (int i=; i<=n; ++i)
for (int j=; j<=(<<n)-; ++j)
if (f[i][j]==m)
{
int sum=;
for (int k=; k<=n; ++k)
if (!(j&(<<k-)))
sum+=a[k];
ans=max(ans,sum);
}
printf("%d",ans);
}
BZOJ3312:[USACO]No Change(状压DP)的更多相关文章
- 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分
[BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...
- 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 .., ...
- [BZOJ3312][USACO]不找零(状压DP)
Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身上 ...
- [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)
传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...
- poj3254 Corn Fields (状压DP)
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- HDUOJ Clear All of Them I 状压DP
Clear All of Them I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 122768/62768 K (Java/Oth ...
- 状压DP总结
状态压缩就是将一行的状态压成一个二进制数,这个数的二进制形式反映了这一行的情况 比如0100111的意义为:这一排的第一个数没被使用,第二个被占用了,第三四个没被占用,第五六七个被占用 我们知道位运算 ...
- 【思维题 状压dp】APC001F - XOR Tree
可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...
随机推荐
- Delphi下OpenGL2d绘图(02)-画点
一.前言 图形的绘制可以使用glBegin().glEnd()之间完成,绘制的框架代码可以使用 Delphi下OpenGL2d绘图(01)-初始化 中的代码.修改的部份为 Draw 函数的内容. 二. ...
- PHP学习10——Cookie和Session技术
主要内容: Cookie技术 创建cookie 查看cookie 读取cookie 用cookie记录访问时间和次数 删除cookie cookie的生命周期 Session技术 session工作原 ...
- CSS之APP开发比较实用的CSS属性
简介:本人刚入前端没多久,在做APP的开发的时候,经常遇到一些奇怪的问题,本人经验少,会使用js来解决css上的问题,但,却不知道其实有些css已经帮我们解决了. 1,white-space: now ...
- 前端(三):JavaScript基础
JavaScript是一种属于网络的脚本语言,常用来为网页添加各式各样的动态功能,是一种动态类型.弱类型.基于原型的语言.它包括三个部分:ECMAScript.BOM和DOM.ECMAScript描述 ...
- mac通过wine运行windows程序(将文件关联到windows程序notepad++)
windows程序功能强大且已经习惯使用了,用mac总感觉不给力,例如记事本工具Notepad++就非常优秀.下面介绍如何在mac系统下通过wine来安装使用notepadd++程序. 1.安装win ...
- BestCoder Round #92
这里是逢比赛必挂的智障选手ysf…… 不知道是因为自己菜还是心态不好……也许是后者吧,毕竟每次打比赛的时候都会很着急.lrd说我打比赛的功利性太强,想想确实是这样. 昨天打完之后自觉身败名裂没敢写出来 ...
- easy html+css tree 简单的HTML+css导航树
code: show:
- mockjs
首先还是那句话,进来的GodBoy and GoodGirl 不妨看完再离开. 一个走在路上的前端攻城狮-along 一.mock的由来 mock有“愚弄.欺骗”之意,在前端领域,mock可以理解为 ...
- 用 State Pattern 来实现一个简单的 状态机
首先要理解 State Pattern 模式. http://www.dofactory.com/net/state-design-pattern Definition Allow an object ...
- Android 友盟统计的集成与使用(包含多渠道打包配置)
前言 app上线后,一般公司都希望跟踪app在市场上的使用情况.包括新增用户.活跃用户.渠道信息.错误信息等,还有例如商城类的app,需要跟踪用户最喜欢浏览哪种类型的店铺或商品.这些都可以通过集成友盟 ...