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

3 6
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

12
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

首先一看数据范围状压没跑了
那么复杂度一定带一个2^16也就是六万多
那DP肯定不能和N搞了……
所以我们就和K搞DP好了
这样就很容易定义f[i][S]表示当前用了i个硬币,硬币使用状态为S的时候最多买到哪个商品
再预处理pay[i][j]表示硬币i从j商品开始买能买到哪里
DP式子就很好想喽。
一开始没注意-1WA了一发……
话说我这算不算面向数据范围编程

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)的更多相关文章

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

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

  2. LG3092 「USACO2013NOV」No Change 状压DP

    问题描述 https://www.luogu.org/problem/P3092 题解 观察到 \(k \le 16\) ,自然想到对 \(k\) 状压. 设 \(opt[i]\) 代表使用硬币状况为 ...

  3. P3092 [USACO13NOV]没有找零No Change 状压dp

    这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了. 题干: 题目描述 Farmer John <= K <= ), each with value .., ...

  4. [BZOJ3312][USACO]不找零(状压DP)

    Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身上 ...

  5. [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)

    传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...

  6. poj3254 Corn Fields (状压DP)

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  7. 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 ...

  8. 状压DP总结

    状态压缩就是将一行的状态压成一个二进制数,这个数的二进制形式反映了这一行的情况 比如0100111的意义为:这一排的第一个数没被使用,第二个被占用了,第三四个没被占用,第五六七个被占用 我们知道位运算 ...

  9. 【思维题 状压dp】APC001F - XOR Tree

    可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...

随机推荐

  1. Java - 深拷贝技巧

    先让我描述一下问题:我在某Action(struts2.x)-A中写了一个功能P,以当前用户的某个标识F == 1时需要走这个功能,而且这个功能因某些原因已经侵入到了其他一些method中.顺便一提, ...

  2. Python下ImportError: DLL load failed: 找不到指定的模块

    环境:Anaconda3 Python3.7 scarpy1.5 版本似乎都能对的上.但是在cmd下报错 如下截图. 从以上错误来看,应该是lxml包有异常. pip uninstall lxml包. ...

  3. Java基础(9)——数组

    难点儿的已经过去啦,现在又开始基础了哈~ 之前讲变量的时候,变量是一个个的呀~,那我要搞一串变量该啷个办呢?Java给我们出了个好东西叫数组(*^▽^*) 数组呢,就是将变量一组一组的存起来,这个也是 ...

  4. 监听域对象创建和销毁的Listener

    1.什么是Servlet监听器? 先来看看什么是监听器.监听器是专门用于对其它对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时立即采取相应的行动.Servlet监听器是S ...

  5. js控制input text字符键入/字符长度限制/字母自动大写

    功能: 1.仅允许指定字符键入 2.限制长度 实现代码: <input type="text" style="width: 6em" name=" ...

  6. Codis3.2 安装部署

    转载请注明出处:https://www.cnblogs.com/format-ch/p/9323841.html 一.软件下载 下载 下载 zookeeper (Codis注册中心) http://m ...

  7. DOM Tree

    DOM Tree   什么是DOM树:网页的所有内容在内存当中,其实是以树形结构存储的 何时创建:当浏览器,读取html中内容的时候,会马上开始创建DOM树. 如何创建: 1.读到HTML的时候还没有 ...

  8. SPOJ QTREE5

    题意 一棵\(n\)个点的树,点从\(1\)到\(n\)编号.每个点可能有两种颜色:黑或白. 我们定义\(dist(a,b)\)为点\(a\)至点\(b\)路径上的边个数. 一开始所有的点都是黑色的. ...

  9. typeof的探讨

    console.log(typeof 'abc') // "string" console.log(typeof true )// "boolean" cons ...

  10. Apache服务器运维笔记(6)----目录 文件 网络容器的安全问题

    <Directory>.<Files>.<Location> 这三个容器的作用都很相似,都是以容器的形式来封装一组指令对访问进行控制,只是它们的区别在于作用于目录. ...