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. SQL Server 2008中的MERGE(数据同步)

    OK,就像标题呈现的一样,SQL Server 2008中的MERGE语句能做很多事情,它的功能是根据源表对目标表执行插入.更新或删除操作.最典型的应用就是进行两个表的同步. 下面通过一个简单示例来演 ...

  2. mongodb操作之mongoose

    /** * Created by chaozhou on 2015/10/6. */ var mongoose = require("mongoose"); var db = mo ...

  3. Hadoop学习笔记(4) ——搭建开发环境及编写Hello World

    Hadoop学习笔记(4) ——搭建开发环境及编写Hello World 整个Hadoop是基于Java开发的,所以要开发Hadoop相应的程序就得用JAVA.在linux下开发JAVA还数eclip ...

  4. mysql-connector/python使用示例

    1.下载安装connector/python 地址:https://dev.mysql.com/downloads/connector/python/ 下载的版本(mysql-connector-py ...

  5. asp.net 日期转换为大写汉字

    //年份转换为大写汉字 public static string numtoUpper(int num) { return "零壹贰叁肆伍陆柒捌玖"[num].ToString() ...

  6. html简介(自己理解和老师讲课)

    首先讲开发网页三种技术:html,css,javascript.html负责网页的结构,css站在没学角度对网页进行美化,javascript负责网页交互,站在用户体验角度设计网页交互效果 而我们所学 ...

  7. bat中实现代码拷贝到指定目录后启动命令行并更改默认路径

      ### window书写shell脚本,实现判断指定文件是否存在,存在就删除,然后复制新文件到此目录 ``` if exist "G:\test\test2\1.txt" (d ...

  8. OA电子表单设计-年假申请单-数据验证

    OA从年初上线到现在已经过去半年了,时光飞逝. 上月底,行政文员找到我,说最近有新来的部门文员填<年假申请单>时,有乱填的情况,让我想办法处理. 我一查还真是,这文员是个男的,同一天给同一 ...

  9. C++类继承--基类new和用派生类new的区别

    实际上无论是用基类还是派生类New, 结果是一样的: #include <stdio.h> class Base { public: int a; Base(){ a=0; } virtu ...

  10. python基础(二)--多值参数以及类

    1.多值参数函数 def 函数名(*args , **kwargs): ....... 多值参数函数顾名思义能够传入多个参数,args表示传入的元组,kwargs表示传入的字典 def functio ...