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 作用域链&内存回收&变量&闭包

    闭包主要涉及到js的几个其他的特性:作用域链,垃圾(内存)回收机制,函数嵌套,等等 一.作用域链:函数在定义的时候创建的,用于寻找使用到的变量的值的一个索引,而他内部的规则是,把函数自身的本地变量放在 ...

  2. 1.0 docker介绍

    简介: 一种虚拟化的方案 将应用程序自动部署到容器   特点: 轻量 环境的一直性 提高开发生命周期 使用面向服务的架构   场景: 开发.测试.部署 创建隔离的运行环境 集群测试环境 云计算应用   ...

  3. CreateProcess中的部分参数理解

    函数原型,这里写Unicode版本 WINBASEAPIBOOLWINAPICreateProcessW( _In_opt_ LPCWSTR lpApplicationName, //可执行文件名字 ...

  4. oracle to_char 返回毫秒级

    select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ssxff') time1, 关键在 systimestamp

  5. 【hihocoder】sam1-基本概念

    这题有毒…… 原本只是想复习下sam,于是写…… 后来发现自己傻了不知道怎么维护endpos…… 一气之下直接kmp拉倒,mdzz UPD:现在我好像会维护endpos了…… #include< ...

  6. C基础 一个可以改变linux的函数getch

    引言  -  getch简述 引用老的TC版本getch说明. (文章介绍点有点窄,  应用点都是一些恐龙游戏时代的开发细节) #include <conio.h> /* * 立即从客户端 ...

  7. 在Xcode中使用自定义的代码片段提高效率

    拖动代码的时候按住option键,很难拖,注意方法:< 引用于:http://www.2cto.com/kf/201409/336245.html

  8. [How to] 使用Xib来创建view

    1.简介 代码库 正如之前博客介绍的,xib可定义页面的某个部分,特别当此部分区域的view集中并且还有一些相互关联性(如隐藏等)是i特别适合使用xib来进行封装. 本文为[How to]使用自定义c ...

  9. golang fmt格式占位符

    golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. # 定义示例类型和变量 type Human struct { Name string } var peo ...

  10. transition结合:after,:before实现动画

    div代码 <div class='div'> hover </div> css代码 .div{ width:200px; height:100px; line-height: ...