[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. ...
随机推荐
- MySQL字符集详解
Reference: https://www.cnblogs.com/wcwen1990/p/6917109.html MySQL字符集详解 一.字符集和校验规则 字符集是一套符合和编码,校验规 ...
- linux c调用 mysql代码
代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/ ...
- Docker(一)Linux开启你的Docker之旅
前言 Docker容器最早受到RHEL完善的支持是从最近的CentOS 7.0开始的,官方说明是只能运行于64位架构平台, 内核版本为2.6.32-431及以上(即 >= CentOS 6.5, ...
- session 和 cookie (转)
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- excel函数vloopup使用方法
邮件处理,查找null手机号码 G1=VLOOKUP(F1,A:B,2,FALSE) H1=VLOOKUP(F1,A:F,7,FALSE)参数1是:查找列,参数2是:范围,参数3是:查找的 ...
- rabbitmq消费端加入精确控频。
控制频率之前用的是线程池的数量来控制,很难控制.因为做一键事情,做一万次,并不是每次消耗的时间都相同,所以很难推测出到底多少线程并发才刚好不超过指定的频率. 现在在框架中加入控频功能,即使开200线程 ...
- MySQL-[SIGNAL/RESIGNAL/GET DIAGNOSTICS]的使用
最近在做 SQL Server 到 MySQL 的迁移(migration),相较于对表和数据的迁移,最令人犯难的还是在功能性存储过程脚本的改写转换(convert),虽说 MySQL 如今是蓬勃发展 ...
- 公开的免费WebService接口分享
天气预报Web服务,数据来源于中国气象局 Endpoint Disco WSDL IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据) Endpoint Disco WSDL 随机英文 ...
- kafka---->kafka connect的使用(一)
这里面介绍一下kafka connect的一些使用. kafka connect的使用 一.在config目录下面复制一个file-srouce.properties并且修改内容 huhx@gohuh ...
- 【Zookeeper系列】Zookeeper简单介绍(转)
原文链接:https://www.cnblogs.com/sunddenly/p/4033574.html 一.分布式协调技术 在给大家介绍ZooKeeper之前先来给大家介绍一种技术——分布式协调技 ...