3312: [Usaco2013 Nov]No Change

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.
题解:
k<=16,很快可以想到是状压DP,设f[i]为i状态下最多可以买的个数,当f[i]=m时计算剩余价值(注意一下循环的范围)。。
#include<stdio.h>
#include<iostream>
using namespace std;
const int M=;
int n,m,i,j,s,ans,p,a[],sum[M],f[(<<)+];
inline int erfen(int x,int v,int l,int r)
{
if(l>r) return r;
int mid=(l+r)>>;
if(sum[mid]-sum[x-]<=v) return erfen(x,v,mid+,r);else return erfen(x,v,l,mid-);
}
inline void read(int &v){
char ch,fu=;
for(ch='*'; (ch<''||ch>'')&&ch!='-'; ch=getchar());
if(ch=='-') fu=, ch=getchar();
for(v=; ch>=''&&ch<=''; ch=getchar()) v=v*+ch-'';
if(fu) v=-v;
}
int main()
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
read(a[i]);
for(i=;i<=m;i++)
read(p),sum[i]=sum[i-]+p;
ans=-;
for(i=;i<=(<<n)-;i++)
{
s=;
for(j=;j<n;j++)
if(i&(<<j)) f[i]=max(f[i],erfen(f[i^(<<j)]+,a[j+],f[i^(<<j)]+,m));else
s+=a[j+];
if(f[i]==m) ans=max(ans,s);
}
cout<<ans;
return ;
}

bzoj 3312: [Usaco2013 Nov]No Change的更多相关文章

  1. bzoj3312: [Usaco2013 Nov]No Change

    题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...

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

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

  3. BZOJ 3315: [Usaco2013 Nov]Pogo-Cow( dp )

    我真想吐槽USACO的数据弱..= = O(n^3)都能A....上面一个是O(n²), 一个是O(n^3) O(n^3)做法, 先排序, dp(i, j) = max{ dp(j, p) } + w ...

  4. BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

    从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...

  5. 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  6. [Usaco2013 Nov]No Change

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  7. BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...

  8. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit] ...

  9. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

随机推荐

  1. js按值及引用传递中遇到的小问题

    有人闲的蛋疼,非要在函数中使用如下方式传值,尼玛一下把我搞糊涂了.于是决定发挥打破沙锅问到底的精神搞清楚它. var a = 1,b = [], c = {}; function f(a, b, c) ...

  2. js/jq 键盘上下左右回车按键

    js判断上下左右回车按键: document.onkeydown=function(e){ e=window.event||e; switch(e.keyCode){ case 37: //左键 co ...

  3. 网络知识===《图解TCP/IP》学习笔记——网络的构成要素

    首先引入网络构成要素图 图片来自<图解TCP/IP--P37> 1.通信媒介与数据链路 计算机之间通过电缆相互连接,电缆可以分为多种,包括双绞线电缆,光纤电缆,同轴电缆,串行电缆等. 图片 ...

  4. Swift 特殊关键字 与符号

    #available() 函数来检查API函数的可用性 // 判断当前版本是否 iOS8.0+,OSX10.10+以及以其他平台 if #available(iOS 8.0, OSX 10.10, * ...

  5. JDBC数据源连接池(1)---DBCP

    何为数据源呢?也就是数据的来源.我在前面的一篇文章<JDBC原生数据库连接>中,采用了mysql数据库,数据来源于mysql,那么mysql就是一种数据源.在实际工作中,除了mysql,往 ...

  6. 数字签名(以ActiveXDemo为例)

    1.创建Install.inf文件和run.bat Install.inf: [version] signature="$CHICAGO$" AdvancedINF=2.0 [Se ...

  7. [ python ] 项目二:主机批量管理程序

    开发要求: 1. 对主机进行批量管理    2. 可对单台或主机组批量执行命令    3. 可上传文件到对应的主机或组    4. 使用多线程实现  程序: 1. README # 作者:hkey # ...

  8. 手机端GPS定位结合百度地图实现定位

    html页面: <!DOCTYPE html>  <html>  <head>      <meta http-equiv="Content-Typ ...

  9. artdialog自定义多个按钮

    在实际运用到的过程中artdialog弹出框下面的按钮不止一个 可以自己定义多个按钮 function view_show(cust_id){$.dialog({        id: 'view_c ...

  10. xshell 如何连接服务器

    https://jingyan.baidu.com/article/ab69b270b0ca3d2ca7189fdc.html 点击“新建”之后就会出现下面这样一个界面,“名称”根据自己的需求填写,“ ...