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

题干:

题目描述
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 ;
}

最新文章

  1. get和post的区别
  2. lua 代码风格
  3. 推荐几个的chorme的扩展程序
  4. viewmodel
  5. iOS学习笔记—ViewController/生命周期
  6. UVALive 7269 Snake Carpet (构造)
  7. iOS 使用两个tableview的瀑布流
  8. Json字符串转换为java对象的各种实现方法【json_lib框架、Gson、org.json】
  9. Java调用R(三)_系统命令调用
  10. Unity3D之MeleeWeaponTrail武器轨迹插件的使用
  11. MFC调用c#的dll
  12. 横瓜从WP7与WP8不兼容之处预测微软公司在未来20年将会倒闭
  13. Python 学习笔记8
  14. 关于SVM数学细节逻辑的个人理解(一)
  15. CentOS_关机与重启命令详解
  16. c#+CAD动态移动效果
  17. Docker孵化的5个开源项目
  18. scala-LinkedList
  19. 10.4-uC/OS-III内部任务(空闲任务OS_IdleTask())
  20. python桌面端开发手记(序列化、压缩包、加密、图形界面GUI)

热门文章

  1. Python学会之后,一般能拿到多少工资?
  2. telnet mysql3306端口失败
  3. springmvc视图解析
  4. &lt;MyBatis&gt;入门四 传入的参数处理
  5. layui 动态表格之合并单元格
  6. Shell脚本中非交互式修改密码的方法(转)
  7. vue中路由
  8. Python-函数和代码复用
  9. 表格头部header固定
  10. noip模拟赛 圆桌游戏