码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • HDU 1712:ACboy needs your help ← 分组背包问题


    【题目来源】
    http://acm.hdu.edu.cn/showproblem.php?pid=1712

    【题目描述】
    ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he will gain from different course depending on the days he spend on it.How to arrange the M days for the N courses to maximize the profit?

    【输入格式】
    The input consists of multiple data sets. A data set starts with a line containing two positive integers N and M, N is the number of courses, M is the days ACboy has.
    Next follow a matrix A[i][j], (1<=i<=N<=100,1<=j<=M<=100).A[i][j] indicates if ACboy spend j days on ith course he will get profit of value A[i][j].
    N = 0 and M = 0 ends the input.

    【输出格式】
    For each data set, your program should output a line which contains the number of the max profit ACboy will gain.


    【算法分析】
    题目大意:一个学生用 M 天的时间复习 N 门课程。已知花费在课程上的天数不同,将会有不同的收益。但是根据样例,并不是花费在课程上的天数越大,收益就越大。所以,请问如何安排这 M 天,可使得收益最大?
    解题思路:本题本质上是一个分组背包问题。可
    将每门课看成一个分组,每门课选择花费的不同天数看成是分组中的不同物品。显然,每组只能选择一个天数。这是因为花费在某门课上的天数,不可能即是 X 天,同时又是 Y 天,只可能是单选。本题中组数为 N,背包容量为M。

    分组背包问题求解的详细分析可参见:https://blog.csdn.net/hnjzsyjyj/article/details/126202821

    【算法代码】

    1. #include
    2. using namespace std;
    3. const int maxn=105;
    4. int a[maxn][maxn];
    5. int c[maxn];
    6. int main() {
    7. int n,V;
    8. while(cin>>n>>V) {
    9. memset(c,0,sizeof(c));
    10. if(n==0&&V==0)break;
    11. for(int i=1; i<=n; i++) {
    12. for(int j=1; j<=V; j++) {
    13. cin>>a[i][j];
    14. }
    15. }
    16. for(int i=1; i<=n; i++) {
    17. for(int j=V; j>=1; j--) {
    18. for(int k=1; k<=j; k++) {
    19. if((c[j-k]+a[i][k])>c[j]) c[j]=c[j-k]+a[i][k];
    20. }
    21. }
    22. }
    23. cout<
    24. }
    25. return 0;
    26. }
    27. /*
    28. in:
    29. 2 2
    30. 1 2
    31. 1 3
    32. 2 2
    33. 2 1
    34. 2 1
    35. 2 3
    36. 3 2 1
    37. 3 2 1
    38. 0 0
    39. out:
    40. 3
    41. 4
    42. 6
    43. */

    【参考文献】
    http://acm.hdu.edu.cn/showproblem.php?pid=1712


    https://blog.csdn.net/Runner__1/article/details/49849625
    https://blog.csdn.net/hnjzsyjyj/article/details/126202821

     

  • 相关阅读:
    2022SDNU-ACM结训赛题解
    C语言 指针
    【学习笔记17】JavaScript作用域
    自动驾驶行业观察之2023上海车展-----智驾供应链(3)
    JavaSE - 递归求解汉诺塔问题
    Java web—访问http://localhost:8080/xx/xx.jsp报404错误问题
    maui 开发AMD CPU踩的坑。
    2023-2028中国稻谷行业市场 国稻种芯:未来趋势预测报告
    SSM项目源码基于SSM实现的小说网站含前后台
    Python:Python简介
  • 原文地址:https://blog.csdn.net/hnjzsyjyj/article/details/126206428
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号