传送门:https://vjudge.net/problem/20465/origin

题意:你有n种钞票,面值为c[i],数量为v[i],便利店老板有无数张面值为c[i]的钞票,问你买一个价值为T的物品,最少需要经手多少张钞票,老板找零的钞票数也算经手的钞票数

题解:因为我的钞票是有限的,所以将自己看作一个多重背包,老板的钞票是无限的,所以将老板的钞票看做一个完全背包,定义状态dp[i]最少花费多少张钞票可以买价值为i的物品

边界:dp[0]=0;

目的:ans=min(dp1[i]+dp2[i-v])   i>=v;

定义状态dp[i]最少花费多少张钞票可以买价值为i的物品
边界:dp1[0]=dp2[0]=0;
目的:ans=min(dp1[i]+dp2[i-v])\quad i>=v;
转移
dp1为完全背包的转移,表示自己凑成面值为i的物品需要的最少钞票张数
dp2为多重背包转移,表示老板凑成面值为i的物品需要的最少的钞票张数
那么买面值为T的物品所需要的最少的钞票数就是

代码:

/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
// ███████████
// ███╬╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬████╬╬╬╬╬╬███
// ███████████ ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
// █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
// ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
// ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
// ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
// █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
// ██████████████ ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
// ███████ █████ ███████████████████
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
LL 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;
}
const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int dp1[maxn];//体积为i时的物品个数
int dp2[maxn];
int v[maxn];
int w[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, V;
while(~scanf("%d%d", &n, &V)) {
memset(v, , sizeof(v));
memset(w, , sizeof(w));
for(int i = ; i <= n; i++) {
scanf("%d", &v[i]);
}
for(int i = ; i <= n; i++) {
scanf("%d", &w[i]);
}
for(int i = ; i <= ; i++)
dp1[i] = dp2[i] = INF;
dp2[] = ;
for(int i = ; i <= n; i++) {
for(int j = v[i]; j <= ; j++) {
dp2[j] = min(dp2[j], dp2[j - v[i]] + );//计算自己得到面额为j的物品需要的最少的钞票数。因为他有一个上界,可能用大额度的钞票会比用小额度的钞票用的钞票数少
}
}
dp1[] = ;
for(int i = ; i <= n; i++)
for(int j = ; j >= v[i]; j--)
for(int k = ; k <= w[i] && j >= k * v[i]; k++)
dp1[j] = min(dp1[j], dp1[j - k * v[i]] + k);//计算商店老板得到面额为j的物品需要的最少的钞票数
int minn = dp1[V];
// cout << minn << endl;
for(int i = V + ; i <= ; i++) {
if(minn > dp1[i] + dp2[i - V]) minn = dp1[i] + dp2[i - V];
}
if(minn != INF ) printf("%d\n", minn);
else printf("-1\n");
}
return ;
}

最新文章

  1. Nginx 访问日志轮询切割
  2. 不注册Tomcat服务,运行Tomcat不弹出JAVA控制台窗口
  3. TN035: Using Multiple Resource Files and Header Files with Visual C++
  4. 让div中的table居中
  5. Textbox像百度一下实现下拉显示 z
  6. OC2_数组操作
  7. Windows下Redis的安装使用[转]
  8. Android 手动显示和隐藏软键盘
  9. Linux安装配置php
  10. Guacamole 介绍
  11. 常量指针(const X*)和指针常量(X* const)
  12. Spring util-namespace下标签相关操作
  13. c# 复制dataset中table结构跟数据
  14. import cv2出现“ImportError: DLL load failed: 找不到指定的模块”
  15. 正则匹配ts的引用
  16. Java核心技术及面试指南:视频列表
  17. PowerDesigner连接SqlServer数据库导出表结构
  18. templates的语法
  19. windows下安装mingw-w64
  20. Android开之在非UI线程中更新UI

热门文章

  1. Ubuntu16.04使用Tarball安装ntp
  2. Python多重赋值
  3. 剑指Offer66题的总结、目录
  4. Selenium WebDriver 下 plugin container for firefox has stopped working
  5. java 乐观锁 vs 悲观锁
  6. 学习GIT 你只要这一篇(转)
  7. C++ 根据图片url 批量 下载图片
  8. Python 并行分布式框架:Celery 超详细介绍
  9. 如何理解IPD+CMMI+Scrum一体化研发管理解决方案之IPD篇
  10. 团队开发——软件需求分析报告(Hello World 团队)