满背包问题,把体积和价值看成相等的。用滚动数组优化,然后额外开辟一个choice数组来记录每次的选择,然后回溯打印。因为要按字典序,先把价值进行排序。假如选最小的商品能装满m的话,那就把判断条件改成大于等于,然后最后来

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int dp[];
  4. int w[];
  5. bool flag[];
  6. bool choice[][];
  7. int n, m;
  8. bool cmp(int a, int b)
  9. {
  10. return a > b;
  11. }
  12. int main()
  13. {
  14. memset(flag, false, sizeof(flag));
  15. fill(choice[], choice[] +, false);
  16. fill(dp, dp +, );
  17. scanf("%d %d", &n, &m);
  18. int i, j;
  19. for (i = ; i <= n; i++)
  20. {
  21. scanf("%d", &w[i]);
  22. }
  23. sort(w+, w + n+,cmp);
  24. for (i = ; i <= n; i++)
  25. {
  26. for (j = m; j >= w[i]; j--)
  27. {
  28. if (dp[j-w[i]]+w[i]>=dp[j])
  29. {
  30. dp[j] = dp[j - w[i]] + w[i];
  31. choice[i][j] = true;
  32. }
  33. }
  34. }
  35. int x=n, y=m;
  36. int num = ;
  37. if (dp[m] != m)
  38. {
  39. printf("No Solution\n");
  40. }
  41. else
  42. {
  43. while (x >= )
  44. {
  45. if (choice[x][y] == true)
  46. {
  47. flag[x] = true;//下标
  48. num++;
  49. y -= w[x];
  50. }
  51. x--;
  52. }
  53. for (i = n; i >= ; i--)
  54. {
  55. if (flag[i] == true)
  56. {
  57. if (num - > )
  58. {
  59. printf("%d ", w[i]);
  60. num--;
  61. }
  62. else
  63. printf("%d\n", w[i]);
  64. }
  65. }
  66. }
  67. }

选择最小的那个。

[pat]1068 Find More Coins的更多相关文章

  1. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  2. PAT 甲级 1068 Find More Coins(0,1背包)

    1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...

  3. PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

    1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some ...

  4. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

    一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...

  5. PAT甲级1068 Find More Coins【01背包】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...

  6. PAT 甲级 1068 Find More Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...

  7. PAT (Advanced Level) 1068. Find More Coins (30)

    01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...

  8. 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)

    题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...

  9. 1068. Find More Coins (30)

    题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

随机推荐

  1. Python生成随机字符串

    利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...

  2. MobaXterm v10.9破解

    去官网下载个人版 Exeinfo查壳发现无壳 载入OD,右键,字符串智能搜索. Ctrl+F搜索关键词About,找到到FormAbout处,即关于窗体的创建和显示的位置.双击查看汇编代码 程序在窗体 ...

  3. 【iCore4 双核心板_uC/OS-II】例程一:认识 uC/OS-II

    一.实验说明: 本例程移值入uC/OS-II,建立三个任务,红色和绿色LED分别以固定频率闪烁,并且打开串口工具, 输出浮点数据. 二.源代码下载链接: 链接:https://pan.baidu.co ...

  4. C#字符串、字节数组和内存流间的相互转换

    定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串=>比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes(&q ...

  5. Paxos 实现日志复制同步(Basic Paxos)

    Paxos 实现日志复制同步 本篇文章以 John Ousterhout(斯坦福大学教授) 和 Diego Ongaro(斯坦福大学获得博士学位,Raft算法发明人) 在 Youtube 上的讲解视频 ...

  6. SpringMvc自动任务调度之task实现项目源码,@Scheduled

    1.Xml配置 Spring-job.xml 并在 Spring-Application.xml中Import <?xml version="1.0" encoding=&q ...

  7. COUNT分组条件去重的sql统计语句示例(mysql)

    常规情况下的sql分组统计为: ) from 表 where 条件 group by 字段; 但是有时往往需要添加不同的条件已经去重的统计以上语句就不能满足需求. 解决方案为: 1.添加条件的统计方案 ...

  8. error MSB3073: 命令“copy /y

    编译VC程序时候报错:error MSB3073: 命令“copy /y 查看: 项目的属性->配置属性->生成事件->后期生成事件->命令行: copy /y "$ ...

  9. How to write threats to validity?

    Paper reference Threats to construct validity are concerned with the relationship between theory and ...

  10. 我永远无法学会的dp

    起源:在codeforceround518之后我发现别人都会div1A我根本写不出来,所以我决定退役 咕咕咕咕