[pat]1068 Find More Coins
满背包问题,把体积和价值看成相等的。用滚动数组优化,然后额外开辟一个choice数组来记录每次的选择,然后回溯打印。因为要按字典序,先把价值进行排序。假如选最小的商品能装满m的话,那就把判断条件改成大于等于,然后最后来
- #include<bits/stdc++.h>
- using namespace std;
- int dp[];
- int w[];
- bool flag[];
- bool choice[][];
- int n, m;
- bool cmp(int a, int b)
- {
- return a > b;
- }
- int main()
- {
- memset(flag, false, sizeof(flag));
- fill(choice[], choice[] +, false);
- fill(dp, dp +, );
- scanf("%d %d", &n, &m);
- int i, j;
- for (i = ; i <= n; i++)
- {
- scanf("%d", &w[i]);
- }
- sort(w+, w + n+,cmp);
- for (i = ; i <= n; i++)
- {
- for (j = m; j >= w[i]; j--)
- {
- if (dp[j-w[i]]+w[i]>=dp[j])
- {
- dp[j] = dp[j - w[i]] + w[i];
- choice[i][j] = true;
- }
- }
- }
- int x=n, y=m;
- int num = ;
- if (dp[m] != m)
- {
- printf("No Solution\n");
- }
- else
- {
- while (x >= )
- {
- if (choice[x][y] == true)
- {
- flag[x] = true;//下标
- num++;
- y -= w[x];
- }
- x--;
- }
- for (i = n; i >= ; i--)
- {
- if (flag[i] == true)
- {
- if (num - > )
- {
- printf("%d ", w[i]);
- num--;
- }
- else
- printf("%d\n", w[i]);
- }
- }
- }
- }
选择最小的那个。
[pat]1068 Find More Coins的更多相关文章
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- PAT 甲级 1068 Find More Coins(0,1背包)
1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...
- 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 ...
- PAT甲题题解-1068. Find More Coins (30)-dp,01背包
一开始没多想,虽然注意到数据N<=10^4的范围,想PAT的应该不会超时吧,就理所当然地用dfs做了,结果最后一组真的超时了.剪枝啥的还是过不了,就意识到肯定不是用dfs做了.直到看到别人说用0 ...
- PAT甲级1068 Find More Coins【01背包】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...
- PAT 甲级 1068 Find More Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 Eva loves to collect c ...
- PAT (Advanced Level) 1068. Find More Coins (30)
01背包路径输出. 保证字典序最小:从大到小做背包. #include<cstdio> #include<cstring> #include<cmath> #inc ...
- 【PAT甲级】1068 Find More Coins (30 分)(背包/DP)
题意: 输入两个正整数N和M(N<=10000,M<=10000),接着输入N个正整数.输出最小的序列满足序列和为M. AAAAAccepted code: #define HAVE_ST ...
- 1068. Find More Coins (30)
题目如下: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
随机推荐
- Python生成随机字符串
利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, cho ...
- MobaXterm v10.9破解
去官网下载个人版 Exeinfo查壳发现无壳 载入OD,右键,字符串智能搜索. Ctrl+F搜索关键词About,找到到FormAbout处,即关于窗体的创建和显示的位置.双击查看汇编代码 程序在窗体 ...
- 【iCore4 双核心板_uC/OS-II】例程一:认识 uC/OS-II
一.实验说明: 本例程移值入uC/OS-II,建立三个任务,红色和绿色LED分别以固定频率闪烁,并且打开串口工具, 输出浮点数据. 二.源代码下载链接: 链接:https://pan.baidu.co ...
- C#字符串、字节数组和内存流间的相互转换
定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串=>比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes(&q ...
- Paxos 实现日志复制同步(Basic Paxos)
Paxos 实现日志复制同步 本篇文章以 John Ousterhout(斯坦福大学教授) 和 Diego Ongaro(斯坦福大学获得博士学位,Raft算法发明人) 在 Youtube 上的讲解视频 ...
- SpringMvc自动任务调度之task实现项目源码,@Scheduled
1.Xml配置 Spring-job.xml 并在 Spring-Application.xml中Import <?xml version="1.0" encoding=&q ...
- COUNT分组条件去重的sql统计语句示例(mysql)
常规情况下的sql分组统计为: ) from 表 where 条件 group by 字段; 但是有时往往需要添加不同的条件已经去重的统计以上语句就不能满足需求. 解决方案为: 1.添加条件的统计方案 ...
- error MSB3073: 命令“copy /y
编译VC程序时候报错:error MSB3073: 命令“copy /y 查看: 项目的属性->配置属性->生成事件->后期生成事件->命令行: copy /y "$ ...
- How to write threats to validity?
Paper reference Threats to construct validity are concerned with the relationship between theory and ...
- 我永远无法学会的dp
起源:在codeforceround518之后我发现别人都会div1A我根本写不出来,所以我决定退役 咕咕咕咕