Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example,

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each.

Call cash the requested amount of cash the machine should
deliver and write a program that computes the maximum amount of cash
less than or equal to cash that can be effectively delivered according
to the available bill supply of the machine.

Notes:

@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc.

Input

The program input is from standard input. Each data set in the
input stands for a particular transaction and has the format:

cash N n1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash
requested, 0 <=N <= 10 is the number of bill denominations and 0
<= nk <= 1000 is the number of available bills for the Dk
denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur
freely between the numbers in the input. The input data are correct.

Output

For each set of data the program prints the result to the
standard output on a separate line as shown in the examples below.

Sample Input

735 3  4 125  6 5  3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10

Sample Output

735
630
0
0

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash.

In the second case the bill supply of the machine does not
fit the exact amount of cash requested. The maximum cash that can be
delivered is @630. Notice that there can be several possibilities to
combine the bills in the machine for matching the delivered cash.

In the third case the machine is empty and no cash is
delivered. In the fourth case the amount of cash requested is @0 and,
therefore, the machine delivers no cash.

Source

多重背包 单调队列优化

 /*By SilverN*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int lim,n;
int m[mxn],v[mxn];
bool f[];
int q[];
int main(){
int i,j;
while(scanf("%d%d",&lim,&n)!=EOF){
memset(f,,sizeof f);
for(i=;i<=n;++i){m[i]=read();v[i]=read();}
f[]=;
for(i=;i<=n;++i){
if(m[i]==){
for(j=lim;j>=v[i];j--)
f[j]=f[j]||f[j-v[i]];
}
else{
for(int k=;k<v[i];++k){
if(k>lim)break;
int hd=,tl=;int smm=;
for(j=k;j<=lim;j+=v[i]){
q[++tl]=f[j];
smm+=f[j];
if(tl-hd>m[i])smm-=q[hd++];
f[j]=f[j]||smm;
}
}
}
}
for(i=lim;i>=;--i)if(f[i]){
printf("%d\n",i);break;
}
}
}

POJ1276 Cash Machine的更多相关文章

  1. POJ-1276 Cash Machine 多重背包 二进制优化

    题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...

  2. POJ1276 - Cash Machine(多重背包)

    题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...

  3. POJ1276:Cash Machine(多重背包)

    题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...

  4. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  5. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  6. POJ 1276 Cash Machine

    Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...

  7. PKU 1276 Cash Machine

    <span style="color:#000099;">/* Cash Machine Time Limit: 1000MS Memory Limit: 10000K ...

  8. Cash Machine(多重背包二进制转换)

    个人心得:多重背包,自己根据转换方程写总是TLE,后面去网上看了二进制转换,不太理解: 后面仔细想了下,用自己的思想理解下把,就是将对应number,cash总和用二进制拆分, 然后全部装入到一个数组 ...

  9. POJ 1276 Cash Machine(单调队列优化多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

随机推荐

  1. 后缀数组 (Suffix Array) 学习笔记

    \(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...

  2. nodejs idea 创建项目 (一)

    1.在工作空间创建module file->new module next next 项目的目录结构: bin:跟业务无关的公共部分 node_modules :默认的模块 public:公共模 ...

  3. elasticsearch.yml 配置说明

    cluster.name: 指定node所属的cluster. node.name: 本机的hostname. node.master: 是否可以被选举为master节点.(true or false ...

  4. dll、lib(动态链接库、静态链接库)的区别

    1.dll:dynamic link library: lib:static link library. 2.windows系统中,许多app并不是仅由一个完整的exe构成,而是按功能分成了若干部分, ...

  5. 解剖嵌入式设备开发时以SD卡启动时SD卡的存储结构(以三星exynos4412为例)

    目前面对高性能产品的嵌入式开发中,用SD卡来代替以往的JLINK显得备受大家喜欢,而且MCU厂家也对以SD卡启动的支持度越来越大,反而对JLINK不在那么重视(不过依旧保留着).一些以开发开发板的公司 ...

  6. 习水医院12C RAC 数据库安装文档

        环境介绍 OS: Oracle Enterprise Linux 6.4 (For RAC Nodes) DB: GI and Database 12.1.0.2 所需介质 p17694377 ...

  7. 用Go向MySQL导入.csv文件

    今天来更新一个很少碰到,但碰到了又让人十分蛋疼的问题——Go语言中执行MySQL的load data local infile语句报local file 'xxx' is not registered ...

  8. shell高级用法

    参考链接: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=218853&page=7#pid1628522

  9. MFC线程获取主窗口句柄

    CWnd* h_q = AfxGetApp()->GetMainWnd(); //获取主窗口的句柄

  10. Spring Boot 打包分离依赖 JAR 和配置文件

    <properties> <java.version>1.8</java.version> <project.build.sourceEncoding> ...