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. linux新建用户切换后显示-bash-4.1$(转载)

    今天新建了一个MQM的用户 , [plain] view plaincopy useradd -g mqm -d /var/mqm passwd mqm 终端中显示 -bash-4.1$而不是 [mq ...

  2. replaceAll的一个bug

    String replaceAll(regex, replacement)函数 , 由于第一个参数支持正则表达式,replacement中出现“$”,会按照$1$2的分组模式进行匹配,当编译器发现“$ ...

  3. 对Mybatis的初步认识

    1.认识Mybatis MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyBat ...

  4. ueditor PHP版本使用方法

    1.ueditor是百度很好用的一款文本编辑器,第一次使用,在此记录使用方法. 2.从http://ueditor.baidu.com/website/download.html#ueditor下载e ...

  5. ajax提交手机号去数据库验证并返回状态值

    <script type="text/javascript"> $(function(){ $('.agree_regi').click(function(){ var ...

  6. nodejs繁琐地自建路由

    一.繁琐的自建路由 app.js var server = require('./server'); server.startServer(); server.js var http = requir ...

  7. 移动端meta标签的使用和设置

    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale= ...

  8. es6的一些基本语法

    首先说一下什么是es6: ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准; let 和 const 命令 let的基本用法: 上面代码在代码块之中,分别用l ...

  9. 006服务监控看板Hystrix Dashboard

    1.POM配置 和普通Spring Boot工程相比,仅仅添加了Hystrix Dashboard和Spring Boot Starter Actuator依赖 <dependencies> ...

  10. arcgis api for javascript - 最基本的地图加载

    为大家贴贴最基本的地图加载: 一. API 根据Dom树上节点的 ID 确定 Map 的显示位置; 二. setBasemap 方法可得到一些ArcGIS制作好的底图,例如: "street ...