bzoj 3312: [Usaco2013 Nov]No Change
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
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
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.
#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的更多相关文章
- bzoj3312: [Usaco2013 Nov]No Change
题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...
- 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分
[BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...
- 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 ...
- BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )
从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...
- 【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 ...
- [Usaco2013 Nov]No Change
Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...
- BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...
- BZOJ3315: [Usaco2013 Nov]Pogo-Cow
3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 143 Solved: 79[Submit] ...
- BZOJ3314: [Usaco2013 Nov]Crowded Cows
3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 86 Solved: 61[Subm ...
随机推荐
- fs.createReadStream(filepath).pipe(response);这句是什么意思?
'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require(' ...
- Angular2.0 基础: 环境搭建
最近在学习Angular2的使用,其实看过Angular2 文档的都知道,相比于之前的Angular1,Angular2 的改动还是挺大的. 而对于‘angular2 的本地开发环境的搭建的中,我们首 ...
- Python代码这样写更优雅(转)
1.变量交换 大部分编程语言中交换两个变量的值时,不得不引入一个临时变量: >>> a = 1>>> b = 2>>> tmp = a>&g ...
- Caffe学习笔记3
Caffe学习笔记3 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和h ...
- 【openjudge】C15C Rabbit's Festival CDQ分治+并查集
题目链接:http://poj.openjudge.cn/practice/C15C/ 题意:n 点 m 边 k 天.每条边在某一天会消失(仅仅那一天消失).问每一天有多少对点可以相互到达. 解法:开 ...
- Phoenix批量修改数据
很简单的一个东西,查了挺久的,浪费了很多的时间 直接用Upsert Into Select就可以了 例:把tables表中cloumn2列等于bbb的都改成aaa Upsert Into Table ...
- [hadoop][会装]zookeeper安装
1.简介 分布式场景下的各个进程间的协调运作离不开zookeeper, zookeeper已经是大数据领域提供分布式协调服务的事实标准. 本文只介绍zookeeper的安装方法. 2. 节点规划如下: ...
- 解析jsp的 tomcat 、resin
一.tomcat 1. 安装JDK [root@localhost ~]# cd /usr/local/src/ [root@localhost src]# wget http://www.lishi ...
- Network——物理层-练习题与解答
1. 无线电天线通常在其直径等于无线电波的波长的情况下工作效果最好.合理的天线直径的范围是从1厘米到5米.问所覆盖的频率范围是怎样的? 解答: λf = c , c=3x108 (m/s) 对于λ=1 ...
- 微擎系统BUG漏洞解决方法汇总(原创)
微擎微赞系统BUG漏洞解决方法汇总 弄了微擎系统来玩玩,发觉这个系统BUG还不少,阿里云的提醒都一大堆,主要是没有针对SQL注入做预防,处理的办法基本都是用转义函数. 汇总: 1. 漏洞名称: 微擎任 ...