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. ADO.net增删改的使用

    添加数据 -------------------------------------------------- //让用户输入要添加的内容 Console.WriteLine("请输入要添加 ...

  2. 自学 iOS - 三十天三十个 Swift 项目 第二天

    继续做仿造着别人的第二个 1.首先下载 一些字体 网上搜索 "造字工房" 2.把下载的相应字体文件放到工程之中,就Ok了 不多说 效果如下 可以下面这个方法 检索项目里面所有的字体 ...

  3. 【C++】模板简述(二):函数模板

    我们上文讲了,模板的引入,我们发现在某种特殊的情况下,必须得通过模板才能完美的解决问题. 本文就来简述一下函数模板的基本使用. 一.函数模板格式 template<typename Param1 ...

  4. vue路由细节探讨

    1.使用router-link 不会让页面刷新,使用a标签会使页面刷新.2.router-link 里面的to="/路由地址" tag=""自定义标签" ...

  5. vue 写一个聊天工具

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. linux 配置Java、Mysql、Tomcat、Redis开发环境

    1.安装四个依赖 以下四个依赖必须按顺序联网安装:yum install glibc.i686yum -y install libaio.so.1 libgcc_s.so.1 libstdc++.so ...

  7. apm - 查询高级电源管理(APM) BIOS

    总览 apm [ - vVmsS ] 描述 apm 读取 /proc/apm 并用人能看懂的格式输出.因为提供了首要的电池状态,这个命令在有兼容的 APM BIOS 的笔记本电脑上非常有用. apm ...

  8. April Fools Day Contest 2019: editorial回顾补题

    A. Thanos Sort time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. JavaSE-16 集合框架

    学习要点 Java集合框架内容 ArrayList和LinkedList HashMap Iterator 泛型集合 Java的集合框架 1  概述 数据结构是以某种形式将数据组织在一起的集合,它不仅 ...

  10. pytorch笔记:09)Attention机制

    刚从图像处理的hole中攀爬出来,刚走一步竟掉到了另一个hole(fire in the hole*▽*) 1.RNN中的attentionpytorch官方教程:https://pytorch.or ...