只需要对John的付款数做一次多重背包,对shopkeeper的找零钱数做一次完全背包即可。

最重要的是上界的处理。可以注意到,John的付款数最多为maxv*maxv+m,也就是24400元。同理,shopkeeper找钱最多的数目为maxv*maxv.

证明如下:

如果John的付款数大于了maxv*maxv+m,即付硬币的数目大于了maxv,根据鸽笼原理,至少有两个的和对maxv取模的值相等,也就是说,这部分硬币能够用更少的maxv来代替。证毕。

转自:http://www.cnblogs.com/Thispoet/archive/2011/09/20/2182022.html

另:Discuss里的数据不要信。。。。。

// by SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,t,v[105],c[105],num[25000],f[25000],g[25000],ans=0x3ffffff;
int main(){
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++)scanf("%d",&v[i]);
for(int i=1;i<=n;i++)scanf("%d",&c[i]);
memset(f,0x3f,sizeof(f)),f[0]=0;
memset(g,0x3f,sizeof(g)),g[0]=0;
for(int i=1;i<=n;i++){
memset(num,0,sizeof(num));
for(int j=0;j<=24500;j++)
if(f[j+v[i]]>f[j]+1&&num[j]<c[i])f[j+v[i]]=f[j]+1,num[j+v[i]]=num[j]+1;
}
for(int j=1;j<=n;j++)
for(int i=0;i<=24500;i++)
g[i+v[j]]=min(g[i+v[j]],g[i]+1);
for(int i=t;i<=24000;i++)ans=min(ans,f[i]+g[i-t]);
if(ans!=0x3ffffff)printf("%d",ans);
else puts("-1");
}

POJ 3260 DP的更多相关文章

  1. The Fewest Coins POJ - 3260

    The Fewest Coins POJ - 3260 完全背包+多重背包.基本思路是先通过背包分开求出"付出"指定数量钱和"找"指定数量钱时用的硬币数量最小值 ...

  2. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  3. POJ 3260 The Fewest Coins(完全背包+多重背包=混合背包)

    题目代号:POJ 3260 题目链接:http://poj.org/problem?id=3260 The Fewest Coins Time Limit: 2000MS Memory Limit: ...

  4. POJ 3260 The Fewest Coins(多重背包问题, 找零问题, 二次DP)

    Q: 既是多重背包, 还是找零问题, 怎么处理? A: 题意理解有误, 店主支付的硬币没有限制, 不占额度, 所以此题不比 1252 难多少 Description Farmer John has g ...

  5. (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)

    http://poj.org/problem?id=3260   Description Farmer John has gone to town to buy some farm supplies. ...

  6. POJ 3260 The Fewest Coins(背包问题)

    [题目链接] http://poj.org/problem?id=3260 [题目大意] 给出你拥有的货币种类和每种的数量,商店拥有的货币数量是无限的, 问你买一个价值为m的物品,最少的货币流通数量为 ...

  7. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  8. poj 1080 dp如同LCS问题

    题目链接:http://poj.org/problem?id=1080 #include<cstdio> #include<cstring> #include<algor ...

  9. poj 1609 dp

    题目链接:http://poj.org/problem?id=1609 #include <cstdio> #include <cstring> #include <io ...

随机推荐

  1. 第十课: - 读取/写入Excel/Json格式数据

    第 10 课 从DataFrame到Excel 从Excel到DataFrame 从DataFrame到JSON 从JSON到DataFrame In [1]: import pandas as pd ...

  2. bootstrap初用新得2

    ##具体实现 1. 宽度无限的背景和始终居中的主题内容:   首先是背景要用一个div1来做out-background,然后div1的兄弟元素div2来做container.对out-backgro ...

  3. Arguments Optional FreeCodeCamp

    function add() { if(typeof arguments[0] !== "number" || (arguments.length > 1 && ...

  4. centos7 redmine安装过程(转载)

    redmine 部署过程 redmin官方文档写的太烂加上不熟悉ruby搞了半天,回到家后觉得还是记录下好,希望可以帮助有需要的人,少走弯路. 版本说明 下面的版本很重要redmine 版本 3.3 ...

  5. 重置浏览器默认样式 normalize.css

    1 /*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ /* Document ========= ...

  6. linux ubuntu安装好后,开通远程登录

    1.设置root密码 ubuntu安装好后,用初始的第一个用户登录,然后修改root密码: sudo passwd root 2.检查sshd服务是否启动 (1)查看sshd状态 -----若没有ss ...

  7. Ubuntu中无法update的解决办法

    我输入 sudo apt-get update 出现错误: /etc/apt$ sudo apt-get update Err http://security.ubuntu.com precise-s ...

  8. 微信小程序 全局变量

    微信小程序里面有个app.js,我们可以在这个里面设置全局变量, App({ globalData:{ url:"http://xxx.xxx.xx:3000" } }) 在外面就 ...

  9. nodejs-EventEmitter

    addListener(event, listener)为指定事件添加一个监听器到监听器数组的尾部. on(event, listener)为指定事件注册一个监听器,接受一个字符串 event 和一个 ...

  10. codevs——T1576 最长严格上升子序列

    http://codevs.cn/problem/1576/  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...