这个题有点意思,其实不是特别难,但是不太好想...中间用二分找最大的可买长度就行了.

题干:

题目描述
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的更多相关文章

  1. 洛谷P3092 [USACO13NOV]没有找零No Change

    P3092 [USACO13NOV]没有找零No Change 题目描述 Farmer John is at the market to purchase supplies for his farm. ...

  2. P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  3. 洛谷 P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  4. Luogu P3092 [USACO13NOV]没有找零No Change【状压/二分】By cellur925

    题目传送门 可能是我退役/NOIP前做的最后一道状压... 题目大意:给你\(k\)个硬币,FJ想按顺序买\(n\)个物品,但是不能找零,问你最后最多剩下多少钱. 注意到\(k<=16\),提示 ...

  5. 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 ...

  6. [USACO13NOV]没有找零No Change [TPLY]

    [USACO13NOV]没有找零No Change 题目链接 https://www.luogu.org/problemnew/show/3092 做题背景 FJ不是一个合格的消费者,不知法懂法用法, ...

  7. [luoguP3092] [USACO13NOV]没有找零No Change(状压DP + 二分)

    传送门 先通过二分预处理出来,每个硬币在每个商品处最多能往后买多少个商品 直接状压DP即可 f[i]就为,所有比状态i少一个硬币j的状态所能达到的最远距离,在加上硬币j在当前位置所能达到的距离,所有的 ...

  8. [洛谷P3092]【[USACO13NOV]没有找零No Change】

    状压\(DP\) + 二分 考虑构成:\(k<=16\)所以根据\(k\)构造状压\(dp\),将所有硬币的使用情况进行状态压缩 考虑状态:数组\(dp[i]\)表示用\(i\)状态下的硬币可以 ...

  9. 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分

    [BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...

随机推荐

  1. MxCAD5.2 20181022更新

    下载地址: http://www.mxdraw.com/ndetail_10108.html 1. 开放VIP功能,无需购买即可使用 2. 修正一些图纸打开和保存出错的问题 3. 修改填充命令,对某些 ...

  2. php中 如何找到session 的保存位置

    [前言] 刚刚想测试FQ操作,需要删除session,这里记录分享下 [主体] (1)想要查看session保存的目录,需要先找到 php.ini配置文件 (2)在php.ini文件中查找 sessi ...

  3. ThinkPHP---TP功能类之附件下载

    [案例]实现公文中附件下载 (1)修改模板文件showList.html,展示列表文件信息 将数据表中的filename(原始文件名)展示到附件下 <td>{$vol.filename}& ...

  4. 网络编程 - socket实现多个连接处理

    #服务端import socket,osso_server=socket.socket()so_server.bind(("localhost",6969))so_server.l ...

  5. you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(255), sort integer not null

    you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...

  6. 总结struts2 iterator status的用法

    1:#status.odd 是否奇数行 2:#status.count 当前行数 3:#status.index 当前行的序号,从0开始『#status.count=#status.index+1』 ...

  7. 09js、MySQL相关

    09js.MySQL相关-2018/07/19 1.js的dom 理解一下文档对象模型:html文件加载到内存之后会形成一颗dom树,根据这些节点对象可以进行脚本代码的动态修改;在dom树当中 一切皆 ...

  8. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  9. 【06】AngularJS 控制器

    AngularJS 控制器 AngularJS 控制器 控制 AngularJS 应用程序的数据. AngularJS 控制器是常规的 JavaScript 对象. AngularJS 控制器 Ang ...

  10. 【Codeforces 348A】Mafia

    [链接] 我是链接,点我呀:) [题意] 每轮游戏都要有一个人当裁判,其余n-1个人当玩家 给出每个人想当玩家的次数ai 请你求出所需要最少的玩游戏的轮数 使得每个人都能满足他们当玩家的要求. [题解 ...