P3092 [USACO13NOV]没有找零No Change 状压dp
2024-09-07 13:30:45
这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了.
题干:
题目描述
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 ;
}
最新文章
- get和post的区别
- lua 代码风格
- 推荐几个的chorme的扩展程序
- viewmodel
- iOS学习笔记—ViewController/生命周期
- UVALive 7269	Snake Carpet (构造)
- iOS 使用两个tableview的瀑布流
- Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
- Java调用R(三)_系统命令调用
- Unity3D之MeleeWeaponTrail武器轨迹插件的使用
- MFC调用c#的dll
- 横瓜从WP7与WP8不兼容之处预测微软公司在未来20年将会倒闭
- Python 学习笔记8
- 关于SVM数学细节逻辑的个人理解(一)
- CentOS_关机与重启命令详解
- c#+CAD动态移动效果
- Docker孵化的5个开源项目
- scala-LinkedList
- 10.4-uC/OS-III内部任务(空闲任务OS_IdleTask())
- python桌面端开发手记(序列化、压缩包、加密、图形界面GUI)