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. MySQL 5.6内存占用过高解决方案

      距离MySQL 5.6正式发布已经有比较长的时间了,目前Oracle官网上的最新GA版本MySQL server也为5.6.但reizhi在安装配置后却发现其内存占用居高不下,无论如何调整cach ...

  2. [转] 多种方法查看Oracle SQL执行计划

    本文转自:http://falchion.iteye.com/blog/616234 一.在线查看执行计划表 如果PLAN_TABLE表不存在,执行$ORACLE_HOME/rdbms/admin/u ...

  3. MVC中FileResult 返回类型返回Excel

    公司中以前写的导出有问题.原来使用的XML格式字符串拼接然后转化成流输出 action public FileResult ExportJobFair() { try { string name = ...

  4. Tidb 离线Ansible方式部署实践

    1.最近浏览到一个比较新的分布式数据库Tidb,开源看起来比较牛的样子,一时手痒就动手试试部署 2.参考官方 Ansible 离线方式部署 :https://pingcap.com/docs-cn/o ...

  5. 快速搭建maven私服 Artifactory on Docker

    1.下载官方镜像 docker pull docker.bintray.io/jfrog/artifactory-oss:latest 2.启动容器 docker run --name artifac ...

  6. java下double相乘精度丢失问题

    比如 System.out.println(0.14*100); 输出: 14.000000000000002 解决方法: BigDecimal b = new BigDecimal(String.v ...

  7. Iphone各个型号机型的详细参数,尺寸和dpr以及像素

    1.iPhone尺寸规格 2.单位inch(英吋) 1 inch = 2.54cm = 25.4mm 3.iPhone手机宽高 上表中的宽高(width/height)为手机的物理尺寸,包括显示屏和边 ...

  8. 关于div设置display: inline-block之后盒子之间间距的处理

    当两个盒子都设置display: inline-block之后并且css也清除了默认样式 这时候会发现div盒子之间仍然存在间隙 将font-size清0间距就会取消

  9. linux 忘记密码

    密码保存在/etc/shadow文件中 1. root 密码忘记了 1.1 重启进入单人维护模式后, 系统会主动给予root权限的bash接口, 此时再以passwd修改密码即可: 1.2 以Live ...

  10. c# json数组动态字段名

    根据给定的列名动态生成json数组 List<string> cols = new List<string>() { "姓名","性别" ...