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 ...
随机推荐
- 梦想CAD控件COM接口光栅图处理
在CAD操作过程中,我们在设计绘图时,光栅图像也就是我们常说的图片,应用非常广泛,在CAD中可以直接插入光栅图像,并且可以对光栅图像进行裁剪.透明度调整等一些操作,在网页可以快速实现我们所需功能. 一 ...
- 洛谷——P2659 美丽的序列
P2659 美丽的序列 单调栈维护区间最小值,单调递增栈维护区间最小值, 考虑当前数对答案的贡献,不断加入数,如果加入的数$>$栈顶,说明栈顶的元素对当前数所在区间是有贡献的,同时加入当前的数. ...
- 洛谷——P3946 ことりのおやつ(小鸟的点心)
P3946 ことりのおやつ(小鸟的点心) 题目太长,请去链接里看吧 注意细节:特判终点(即使困住又能怎样,到达就好了),特判高度 #include<bits/stdc++.h> #defi ...
- 服务器做ssh免秘钥登陆
集群内服务器做非root用户免秘钥登陆:1.node1新建用户abc1,制作公钥.私钥(一路回车键即可)ssh-keygen –t rsa将自动在/home/abc1/.ssh/目录下创建公私钥文件如 ...
- FTP配置
常用的匿名FTP配置项 anonymous_enable=YES 是否允许匿名用户访问 anon_umask=022 匿名用户所上传文件的权限 ...
- How to read and write multiple files in Python?
Goal: I want to write a program for this: In a folder I have =n= number of files; first read one fil ...
- BZOJ 1468 Tree 【模板】树上点分治
#include<cstdio> #include<algorithm> #define N 50010 #define M 500010 #define rg registe ...
- Hdu 4864(Task 贪心)(Java实现)
Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...
- 【OpenCV, MFC, DIP】向图像中加入各种噪声
1.椒盐噪声 Mat dstImage = srcImage.clone(); ; k < n; k++) { //随机取值行列 int i = rand() % dstImage.rows; ...
- [luoguP2983] [USACO10FEB]购买巧克力Chocolate Buying(贪心)
传送门 按价格排序后贪心 ——代码 #include <cstdio> #include <iostream> #include <algorithm> int n ...