P3092 [USACO13NOV]没有找零No Change 状压dp
这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了.
题干:
题目描述
Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins ( <= K <= ), each with value in the range ..,,. FJ would like to make a sequence of N purchases ( <= N <= ,), where the ith purchase costs c(i) units of money ( <= c(i) <= ,). 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 - if it is impossible for FJ to make all of his purchases.
约翰到商场购物,他的钱包里有K( <= K <= )个硬币,面值的范围是1..,,。
约翰想按顺序买 N个物品( <= N <= ,),第i个物品需要花费c(i)块钱,( <= c(i) <= ,)。
在依次进行的购买N个物品的过程中,约翰可以随时停下来付款,每次付款只用一个硬币,支付购买的内容是从上一次支付后开始到现在的这些所有物品(前提是该硬币足以支付这些物品的费用)。不幸的是,商场的收银机坏了,如果约翰支付的硬币面值大于所需的费用,他不会得到任何找零。
请计算出在购买完N个物品后,约翰最多剩下多少钱。如果无法完成购买,输出-
输入输出格式
输入格式:
* Line : Two integers, K and N.
* Lines ..+K: Each line contains the amount of money of one of FJ's coins.
* Lines +K..+N+K: These N lines contain the costs of FJ's intended purchases.
输出格式:
* Line : The maximum amount of money FJ can end up with, or - if FJ cannot complete all of his purchases.
输入输出样例
输入样例#: 复制 输出样例#: 复制
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int k,n;
ll dp[ << ],tot = ;
ll coin[],cost[],sum[],ans = -;
ll h[];
ll find(ll lim)
{
int le = ,ri = n;
int pos = ;
while(le <= ri)
{
int mid = ((le + ri) >> );
if(sum[mid] <= lim)
le = mid + ,pos = mid;
else
ri = mid - ;
}
return pos;
}
int main()
{
read(k);read(n);
duke(i,,k)
read(coin[i]);
duke(i,,n)
read(cost[i]),sum[i] = sum[i - ] + cost[i];
h[] = ;
duke(i,,k)
h[i] = h[i - ] << ;
duke(p,,( << k) - )
{
duke(i,,k)
{
if(!(p & h[i]))
continue;
ll res = dp[p ^ h[i]];
ll pos = find(sum[res] + coin[i]);
dp[p] = max(dp[p],pos);
}
}
ll cnt = ;
duke(i,,( << k) - )
{
if(dp[i] == n)
{
cnt = ;
duke(j,,k)
{
if(!(i & h[j]))
cnt += coin[j];
}
ans = max(ans,cnt);
}
}
printf("%lld\n",ans);
return ;
}
P3092 [USACO13NOV]没有找零No Change 状压dp的更多相关文章
- 洛谷P3092 [USACO13NOV]没有找零No Change
P3092 [USACO13NOV]没有找零No Change 题目描述 Farmer John is at the market to purchase supplies for his farm. ...
- P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- 洛谷 P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- Luogu P3092 [USACO13NOV]没有找零No Change【状压/二分】By cellur925
题目传送门 可能是我退役/NOIP前做的最后一道状压... 题目大意:给你\(k\)个硬币,FJ想按顺序买\(n\)个物品,但是不能找零,问你最后最多剩下多少钱. 注意到\(k<=16\),提示 ...
- luogu P3092 [USACO13NOV]没有找零No Change
题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...
- [USACO13NOV]没有找零No Change [TPLY]
[USACO13NOV]没有找零No Change 题目链接 https://www.luogu.org/problemnew/show/3092 做题背景 FJ不是一个合格的消费者,不知法懂法用法, ...
- [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)
传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...
- [洛谷P3092]【[USACO13NOV]没有找零No Change】
状压\(DP\) + 二分 考虑构成:\(k<=16\)所以根据\(k\)构造状压\(dp\),将所有硬币的使用情况进行状态压缩 考虑状态:数组\(dp[i]\)表示用\(i\)状态下的硬币可以 ...
- 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分
[BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...
随机推荐
- oracle 安装准备
1.选择数据库 (官网查询支持的操作系统) 2.选择系统 (官网查询支持的硬件)(更新补丁) 3.选择硬件 (io性能测试--oracle 大量小文件读写) 4.oracle 升级(和打补丁) 5.o ...
- 汇编学习pushl, popl
- java中通用权限管理设计(转)
原文地址:http://www.cnblogs.com/a7345678/archive/2008/09/25/1298838.html 转自博客园暗夜精灵-鬼才阁 实现业务系统中的用户权限管理 B/ ...
- Servlet过滤器的使用
Servlet过滤器的使用 制作人:全心全意 Servlet过滤器:Servlet过滤器与Servlet十分相似,但它具有拦截客户端请求的功能,Servlet过滤器可以改变请求中的内容,来满足实际开发 ...
- config对象的使用及常用方法
config对象的使用及常用方法 制作人:全心全意 config对象主要用于取得服务器的配置信息.通过pageContext对象的getServletConfig()方法可以获取一个config对象. ...
- python学习,使用requests库来模拟登录github,post请求。
这次我们要模拟登录的页面是 https://github.com/login 首先我们先尝试着登陆一遍分析一下请求, 打开开发者工具下的network选项, 可以很清楚的看到这个会话session,而 ...
- [bzoj4591][Shoi2015][超能粒子炮·改] (lucas定理+组合计数)
Description 曾经发明了脑洞治疗仪&超能粒子炮的发明家SHTSC又公开了他的新发明:超能粒子炮·改--一种可以发射威力更加 强大的粒子流的神秘装置.超能粒子炮·改相比超能粒子炮,在威 ...
- 2014年武汉的IT行情好像不太好(续):20个月过后,再看当时面试过的几个公司--武汉财富基石-崩盘,辣妈萌宝-创业失败,朋友公司转交他人管理
2014年9月的时候,写过一篇面试的总结性质的文章,"2014年武汉的IT行情好像不太好". 原文地址:blog.csdn.net/fansunion/article/detai ...
- 常量Constant
常量通常指的是一个固定的值,例如:1.2.3.’a’.’b’.true.false.”helloWorld”等. 在Java语言中,主要是利用关键字final来定义一个常量. 常量一旦被初始化后不能再 ...
- 【04】AngularJS 表达式
AngularJS 表达式 AngularJS 使用 表达式 把数据绑定到 HTML. AngularJS 表达式 AngularJS 表达式写在双大括号内:{{ expression }}. Ang ...