当前位置:网站首页>"Explanation" change exchange
"Explanation" change exchange
2022-07-24 08:20:00 【Beiqi kylin】
Give me your money!!1
「 My question making process 」:
step1: Observation surface , This allows us to understand the type of problem solving .
「 Write a function to calculate that you can Make up the total amount 」, It can be concluded that this is a knapsack DP.
「 Of each coin The number is infinite 」, It is further concluded that this is Tao Completely backpack .( Question type : Completely backpack )
「 The minimum number of coins 」, Prove that this is under the premise of backpacking , Find the minimum number of components .
「 Multiple groups Test data 」, Keep in mind that multiple sets of inputs ( On Wrong Answer With no multiple sets of inputs ).( Be careful : Multi group input )
step2: Think about the solution .
First step , reflection dp state :
d p i , j dp_{i,j} dpi,j: front i i i A coin makes a face value j j j Minimum number of coins .
For the current coin c o i n s i coins_{i} coinsi for , Only Take or not take Two kinds of state .
if take , Take the number of coins after as the first i − 1 i - 1 i−1 A coin makes a face value j − w i × k j-w_{i}\times k j−wi×k The total amount of currency plus the amount of currency required for the current category k k k.
if No , Then it means before i − 1 i - 1 i−1 These coins have been able to come up with a face value j j j, There's no need to take .
The second step , Think about the state transfer equation :
The state transition equation of the original complete knapsack is :
d p i , j = max { d p i − 1 , j , d p i − 1 , j − a i + a i } ( a i ≤ j ≤ a m o u n t ) dp_{i, j} = \max\{dp_{i - 1, j}, dp_{i - 1, j - a_{i}}+a_{i}\}\ (a_{i}\le j\le amount) dpi,j=max{ dpi−1,j,dpi−1,j−ai+ai} (ai≤j≤amount)
But here we are not looking for the maximum face value within the total amount , But for The minimum number of coins to make up the total amount , So there is :
d p i , j = min { d p i − 1 , j , d p i − 1 , j − a i + 1 } ( a i ≤ j ≤ a m o u n t ) dp_{i,j}=\min\{dp_{i - 1, j},\,dp_{i - 1, j - a_{i}} + 1\}\ (a_{i}\le j\le amount) dpi,j=min{ dpi−1,j,dpi−1,j−ai+1} (ai≤j≤amount)
Through observation, we found that , The above equation Can reduce dimension . Due to d p i dp_{i} dpi Only i − 1 i - 1 i−1, Therefore, the former dimension can be erased , But you need Guarantee d p i , j dp_{i,j} dpi,j Can be d p i , j − a i dp_{i, j - a_{i}} dpi,j−ai influence ( namely d p i , j dp_{i,j} dpi,j When calculated d p i , j − a i dp_{i, j - a_{i}} dpi,j−ai Has been worked out ), This is the equivalent of an object i i i It has been put into the backpack for many times , So enumerate the current face value j j j Time should be in positive order .
The third step , Type the code of the complete backpack , Change the state transition equation , So the algorithm part of this problem is completed :
for (int i = 1; i <= n; i++) {
for (int j = a[i]; j <= amount; j++) {
dp[j] = min(dp[j], dp[j - a[i]] + 1);
}
}
step3: Completion code :
Can be found through data range , The denomination of a coin can be larger than the total amount , So you can pre-processing shallow optimization ( Although there is no big effect ).
Because I was looking for Minimum number of coins , therefore dp Array to be initialized to Maximum , The former 0 0 0 Kinds of coins make up Face value 0 0 0 It only needs 0 0 0 Grow coins , It can be obtained. d p 0 = 0 dp_{0} = 0 dp0=0.
It is worth noting that ,「 If no combination of coins can make up the total amount , Output − 1 -1 −1」; In the code , It means 「 If d p a m o u n t dp_{amount} dpamount Not updated , The output − 1 -1 −1」, So we only need to judge the output d p a m o u n t dp_{amount} dpamount If it is still the initial value, output − 1 -1 −1.\
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5, A = 1e4 + 5, INF = 0x3f3f3f3f;
int n, amount, a[N], dp[A];
/* dp(i, j): front i Put together a coin j The minimum number of coins dp(i, j) = min(dp(i - 1, j - a[i]), dp(i - 1, j)); Take this coin or Don't take this coin */
int main() {
freopen("exchange.in", "r", stdin);
freopen("exchange.out", "w", stdout);
while (~scanf("%d %d", &n, &amount)) {
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
dp[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = a[i]; j <= amount; j++) {
dp[j] = min(dp[j - a[i]] + 1, dp[j]);
}
}
printf("%d\n", dp[amount] == INF ? -1 : dp[amount]); // You can use the trinocular operator to determine
}
return 0;
}
Accepted Give it to me !!1
Go and solve it 『 Change for 』 beep ~ ヾ(≧▽≦*)o
Bye bye!
边栏推荐
- mysql使用explain分析sql执行计划帮助查找性能瓶颈
- how to add square on screenshot
- EZDML reverse engineering import database analysis practical operation tutorial
- 【golang从入门到实践】学生成绩管理系统
- QT | string generation QR code function
- Hegong sky team vision training day4 - traditional vision, contour recognition
- P1135 奇怪的电梯题解
- The code is tired. Stop and enjoy the top color matching~
- Wechat official account configures custom menu jump applet and automatically replies to jump applet
- Saining Techtalk attack and defense drill: attack combination fist "stable, accurate and ruthless" penetration
猜你喜欢

Cososcreator upgrade gradle version
![[Google play access] payment server token acquisition](/img/c6/d095ea2b88a11bf6b4bdd80499932c.png)
[Google play access] payment server token acquisition

My six months at Microsoft

Markdown basic grammar learning

图新地球:如何导入修改了高程基准(椭球)的CAD文件

Install SQL Server database

Svg from entry to regret, why not learn it earlier (graphic version)

MySQL日期格式化

Android kotlin uses a coroutine instead of a callback function (suspendcoroutine usage)

Figure storage geabase
随机推荐
Do you know the private domain traffic in app?
The difference between online learning and offline learning
P1135 strange elevator problem solution
Adaptive problem of img aspect ratio scaling in flex layout in Safari
Poj3278 catch the cow
MySQL date formatting
[wechat applet development] (IV) uni app from getting started to giving up
[MySQL] 08: aggregate function
【MATLAB】(三)MATLAB在高等数学中的应用
国产“火箭心”人工心脏上市 不同人工心脏有什么区别?
【游戏合集】手机都要被塞爆了,6款优质Pygame游戏合集降临~(附源码)
Natural language processing Jieba
1005. Maximized array sum after K negations
[MySQL] installation tutorial and master-slave configuration
how to add square on screenshot
[Game Collection] mobile phones are about to burst, and a collection of six high-quality pyGame games is coming ~ (source code attached)
33 introduction to sparksql, dataframe and dataset
*Project recurrence * project implementation of thesis based on contextbasedemotionrecognitionusingematicdataset
A Knight‘s Journey题解
How to write your FAQ page?