3312: [Usaco2013 Nov]No Change

Description

Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 <= K <= 16), each with value in the range 1..100,000,000. FJ would like to make a sequence of N purchases (1 <= N <= 100,000), where the ith purchase costs c(i) units of money (1 <= c(i) <= 10,000). 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 -1 if it is impossible for FJ to make all of his purchases.

K个硬币,要买N个物品。

给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的。现希望买完所需要的东西后,留下的钱越多越好,如果不能完成购买任务,输出-1

Input

Line 1: Two integers, K and N.

* Lines 2..1+K: Each line contains the amount of money of one of FJ's coins.

* Lines 2+K..1+N+K: These N lines contain the costs of FJ's intended purchases.

Output

* Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.

Sample Input

3 6
12
15
10
6
3
3
2
3
7

INPUT DETAILS: FJ has 3 coins of values 12, 15, and 10. He must make purchases in sequence of value 6, 3, 3, 2, 3, and 7.

Sample Output

12
OUTPUT DETAILS: FJ spends his 10-unit coin on the first two purchases, then the 15-unit coin on the remaining purchases. This leaves him with the 12-unit coin.
题解:
k<=16,很快可以想到是状压DP,设f[i]为i状态下最多可以买的个数,当f[i]=m时计算剩余价值(注意一下循环的范围)。。
#include<stdio.h>
#include<iostream>
using namespace std;
const int M=;
int n,m,i,j,s,ans,p,a[],sum[M],f[(<<)+];
inline int erfen(int x,int v,int l,int r)
{
if(l>r) return r;
int mid=(l+r)>>;
if(sum[mid]-sum[x-]<=v) return erfen(x,v,mid+,r);else return erfen(x,v,l,mid-);
}
inline void read(int &v){
char ch,fu=;
for(ch='*'; (ch<''||ch>'')&&ch!='-'; ch=getchar());
if(ch=='-') fu=, ch=getchar();
for(v=; ch>=''&&ch<=''; ch=getchar()) v=v*+ch-'';
if(fu) v=-v;
}
int main()
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
read(a[i]);
for(i=;i<=m;i++)
read(p),sum[i]=sum[i-]+p;
ans=-;
for(i=;i<=(<<n)-;i++)
{
s=;
for(j=;j<n;j++)
if(i&(<<j)) f[i]=max(f[i],erfen(f[i^(<<j)]+,a[j+],f[i^(<<j)]+,m));else
s+=a[j+];
if(f[i]==m) ans=max(ans,s);
}
cout<<ans;
return ;
}

bzoj 3312: [Usaco2013 Nov]No Change的更多相关文章

  1. bzoj3312: [Usaco2013 Nov]No Change

    题意: K个硬币,要买N个物品.K<=16,N<=1e5 给定买的顺序,即按顺序必须是一路买过去,当选定买的东西物品序列后,付出钱后,货主是不会找零钱的.现希望买完所需要的东西后,留下的钱 ...

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

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

  3. BZOJ 3315: [Usaco2013 Nov]Pogo-Cow( dp )

    我真想吐槽USACO的数据弱..= = O(n^3)都能A....上面一个是O(n²), 一个是O(n^3) O(n^3)做法, 先排序, dp(i, j) = max{ dp(j, p) } + w ...

  4. BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

    从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...

  5. 【bzoj3312】[Usaco2013 Nov]No Change 状态压缩dp+二分

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

  6. [Usaco2013 Nov]No Change

    Description Farmer John is at the market to purchase supplies for his farm. He has in his pocket K c ...

  7. BZOJ 3314 [Usaco2013 Nov]Crowded Cows:单调队列

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3314 题意: N头牛在一个坐标轴上,每头牛有个高度.现给出一个距离值D. 如果某头牛在它的 ...

  8. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit] ...

  9. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

随机推荐

  1. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  2. Java多线程学习(五)线程间通信知识点补充

    系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...

  3. 一个python拖库字段的小脚本

    import requests import re all_column = dict() all_db = "db_zf,dg_activity,dg_activity_log,dg_ad ...

  4. Bit banging

    Bit banging Bit banging is a technique for serial communications using software instead of dedicated ...

  5. monkey测试===如何获取android app的Activity

    方法一(推荐): 手机连接adb,手机界面在需要取得activity的界面. 推荐使用该命令: adb shell dumpsys activity top | findstr ACTIVITY 获取 ...

  6. Open WATCOM指南 - 哦这样的孤单 你冷若冰霜

    https://my.oschina.net/GIIoOS/blog/126701 WATCOM的历史可以追溯到1965年 加拿大的学生Waterloo的团队开发了叫WATFOR的Fortran编译器 ...

  7. (转)opencv 代替caffe.io.load_image

    self.net.blobs[, , self.image_resize, self.image_resize) #image = caffe.io.load_image(image_file) im ...

  8. 微信小程序使用canvas绘制图片的注意事项

    1.单位换算问题,canvas的尺寸单位是px,所以单位需要做个换算,可以通过wx.getSystemInfo获取屏幕宽高(单位是px),微信小程序无论什么机型,屏幕宽度都是750rpx,因此可以做个 ...

  9. C/C++——[01] 程序的基本框架

    我们以HelloWorld这个简单程序为例,该程序在终端打印一行文本: Hello World! 代码如下: #include <stdio.h> int main(){ printf(& ...

  10. python库-urllib

    urllib库提供了一系列操作url的功能,是python处理爬虫的入门级工具,网上的学习资料也很多.我做爬虫是一开始就用了Scrapy框架,并不是一步步从urllib开始的,反而是在后来解决一些小问 ...