1068 Find More Coins (30分)

news/2024/9/23 9:21:08/

文章目录

  • 1 题目
  • 2 解析
    • 2.1 题意
    • 2.2 思路
  • 3 参考代码

1 题目

1068 Find More Coins (30分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤10​4​​ , the total number of coins) and M (≤10​2​​ , the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print in one line the face values V​1≤V2 ≤⋯≤V​k
​​ such that V1​​ +V​2​​ +⋯+V​k =M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output “No Solution” instead.

Note: sequence {A[1], A[2], …} is said to be “smaller” than sequence {B[1], B[2], …} if there exists k≥1 such that A[i]=B[i] for all i<k, and A[k] < B[k].

Sample Input 1:
8 9
5 9 8 7 2 3 4 1Sample Output 1:
1 3 5Sample Input 2:
4 8
7 2 4 3Sample Output 2:
No Solution

2 解析

2.1 题意

给n个价值的硬币,求这个n硬币中总价值恰为m的硬币序列(按价值从小到大排序),如果有多种方案,按字典需最小的输出

2.2 思路

0-1背包问题,价值c[i]和质量w[i]等价,

  • 1 因为要按最小字典序输出,首先把输入的w[]数组按从大到小排序,让货币价值小的序号较大,
  • 2 开一个二维数组choice[i][v](i表示货币的序号,v表示总价值),来记录dp[i][v]选择了那种策略,转移选择dp[i-1][v]状态,choice[i][v]=0(不放第i件物品),转移选择dp[i-1][v-w[i]]+w[i]状态,choice[i][v]=1(放第i件物品)
  • 3 建立是否输出该货币的flag数组,求输出序列时,i从n开始递减枚举choice[i][v],如果choice==1,则v-w[i],flag[i] = true,继续循环,直到i小于0为止
  • 4 i从n递减枚举,如果flag[i] == true,则输出

3 参考代码

#include <cstdio>
#include <algorithm>
#include <cstring>using std::sort;const int MAXN = 10010;
int w[MAXN];//钱币的价值
int dp[MAXN];
int choice[MAXN][MAXN];
bool flag[MAXN];bool cmp(int a, int b){return a > b;
}int main(int argc, char const *argv[])
{memset(flag, false, sizeof(flag));int n, m;scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i){scanf("%d", &w[i]);}sort(w + 1, w + n + 1, cmp);//从大到小排序,来满足价值从小到大的字典序顺序输出//数字小的占大序号for (int v = 0; v <= m; ++v){dp[v] = 0;}for (int i = 1; i <= n; ++i){for (int v = m; v >= w[i]; --v){int temp = dp[v - w[i]] + w[i];if(dp[v] <= temp){//选择放入第i件物件dp[v] = temp;choice[i][v] = 1;//选择货币价值大的沉底(序号较小)}else{choice[i][v] = 0;}}}if(dp[m] != m){//无解,不满足恰好能付清价值为m的货币printf("No Solution\n");}else{//记录最优路径int num = 0, index = n, v = m;//个数、序号(从大序号开始)、目前价值while(index >= 0){if(choice[index][v] == 1){//优先选择大序号的,即货币价值小的flag[index] = true;v -= w[index];num++;}else if(choice[index][v] == 0){flag[index] = false;}index--;}for (int i = n; i >= 0; --i){if(flag[i] == true){printf("%d", w[i]);num--;if(num > 0) printf(" ");}}}return 0;
}

http://www.ppmy.cn/news/289320.html

相关文章

听音室-HIFI入门之400多张发烧碟中选出的精品

折腾这些年&#xff0c;也算对这个Hi-Fi,即所谓的音响发烧有些认识&#xff0c;这里大谈得比较多的是音响器材&#xff0c;Hi-end器材动辄几十万&#xff0c;有的上百万&#xff0c;为什么我们花这么多精力和金钱的投入&#xff1f;是因为高保真的音乐才是我们追求的本质&#…

pcm5102a解码芯片音质评测_WHAT HIFI推荐2020年最值得购买解码器:11款器材上榜

下面是《What Hi-Fi》推荐的2020年最值得购买解码器,来看看有哪些上榜器材吧! 一、和弦Chord Qutest便携式解码器 查看 和弦Chord Qutest便携式解码器 评测>> 目前该产品有“HIFI说影音城”商家在售,点击查看>> 口碑评说: “听完了上述曲目,我可以明显感受到Q…

[zz] 高端HIFI发烧音频DAC解码芯片排名

音频“解码器”中最核心、重要的器件&#xff0c;无非就是“解码”&#xff08;DAC&#xff0c;数模转换&#xff09;芯片了&#xff0c;大家常常很关注音频DAC芯片的选用&#xff0c;也热衷于对其优劣的讨论。 本文尝试对当前最优秀的高端音频DAC芯片的结构、技术和性能等做简…

真无线蓝牙耳机也能享受HIFI音质,2021五大高人气TWS蓝牙耳机推荐

当代生活&#xff0c;智能手机的耳机孔已经变成了奢侈品。而相对的&#xff0c;有线耳机的市场也被无线耳机不断蚕食。 22 日&#xff0c;光大证券发布了一个有关于目前真无线耳机的行业调查报告。从有一根线连着的颈戴式耳机&#xff0c;再到现在主流的真无线式耳机。这个行业…

STM32H7做的HIFI DSD音乐播放器

历时多年&#xff0c;真是艰辛&#xff0c;幸好做出来了。目前支持的音乐格式如下&#xff1a; WAV&#xff1a;16位/24位/32位 - 8K / 11.025K / 16K / 22.05K / 24K / 32K / 44.1K / 48K / 88.2K / 96K/176.4K / 192K / 352.8K / 384K/705.6K/768K PCM 5.1声道 WAV&#xf…

UBT11:ubuntu安装IDEA2020.1

11.1 简介 ​ linux上的IDEA并不需要安装&#xff0c;只要解压即可运行&#xff0c;这就好像win上面的绿色软件。所以&#xff0c;我们需要把idea解压到一个合适的位置&#xff0c;然后创建桌面快捷方式&#xff0c;即可完成安装。此方法应该适用于整个JetBrains的软件。 11.…

protocol is disabled or cipher suites are inappropriate异常处理

程序运行时候出现异常陷入死循环。 2022-01-03 10:04:14.537 ERROR 7572 --- [eate-1522974919] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://localhost:3306/shiro?useUnicodetrue&characterEncodingutf8&allow…

UBT5:ubuntu安装GIMP

5.1 简介 ​ 在使用Linux的过程中&#xff0c;难免会使用到图片处理。在win上使用的是PS&#xff0c;但是Linux却没有相应的软件提供。虽然现在也已经有在线PS网站的存在&#xff0c;但是还是打算尝试GIMP。 5.2 环境 ​ 日期&#xff1a; 2021.10.13 ​ 版本&#xff1a; …