码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 91. 存钱罐


    题目链接

    点此跳转

    思路

    完全背包问题。

    令 f [ i ] [ j ] f[i][j] f[i][j] 为从前 i i i 个物品中选且总体积等于 j j j 的最小价值。

    转移方程: f [ i ] [ j ] = m i n ( f [ i − 1 ] [ j ] , f [ i − 1 ] [ j − v [ i ] ] + w [ i ] ) f[i][j] = min(f[i-1][j],f[i-1][j-v[i]]+w[i]) f[i][j]=min(f[i−1][j],f[i−1][j−v[i]]+w[i])

    边界条件:

    f [ 0 ] = 0 f[0]=0 f[0]=0

    f [ i ] = I N F ( 1 ≤ i ≤ n ) f[i]=INF(1 \le i \le n) f[i]=INF(1≤i≤n)

    代码
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #define LL long long
    #define mem(a, b) memset(a, b, sizeof a)
    #define lowbit(x) (-x&x)
    #define IOS ios::sync_with_stdio(false),cin.tie(0)
    #define endl '\n'
    #define rev(x) reverse(x.begin(), x.end())
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int N = 10010;
    
    int w[N], v[N];
    int n, m;
    int f[N];
    
    void solve() {
    	int tt;
    	cin >> tt;
    	while (tt -- ) {
    		int t1, t2;
    		cin >> t1 >> t2;
    		m = t2 - t1;
    		
    		cin >> n;
    		for (int i = 1; i <= n; i ++ ) cin >> w[i] >> v[i];
    		
    		mem(f, 0x3f);
    		
    		f[0] = 0;
    		for (int i = 1; i <= n; i ++ ) {
    			for (int j = v[i]; j <= m; j ++ ) {
    				f[j] = min(f[j], f[j - v[i]] + w[i]);
    			}
    		}
    		
    		if (f[m] != INF)
    			printf("The minimum amount of money in the piggy-bank is %d.\n", f[m]);
    		else puts("This is impossible.");
    	}
    }
    
    int main() {
    	IOS;
    	
    	solve();
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
  • 相关阅读:
    【云原生之k8s】k8s之持久化存储PV、PVC
    【校招VIP】去年招770人,今年竟然只招50人??24届秋招真的好难。。。
    人工智能 知识图谱
    一个合格的中级前端工程师需要掌握的 28 个 JavaScript 技巧
    Maestro实践
    【Hexo博客】将静态博客部署到服务器
    【python科研绘图】多分类多字段箱型图或者小提琴图绘制
    element-ui el-table-column 宽度不能动态设置问题
    @wufengteam/core 统一中心注册器
    鸿蒙开发套件全面升级,助力鸿蒙生态蓬勃发展
  • 原文地址:https://blog.csdn.net/weixin_60484917/article/details/127606017
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号