Title link: https://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1166
Topic
n coins, face value ai, each type is unlimited. Then give you an s, how many coins can be used, so that the total face value is exactly equal to s. Output the minimum and maximum values of the number of coins.
Ideas
Choose the items, the quantity is unlimited, then it is a complete backpack .
dp1[i] both represent the number of coins with a face value of i. To make the total face value equal to j, there are two options: choose the current a[i], or not choose a[i].
If a[i] is selected, then only sa[i] is left, if dp1[ja[i]]==inf, or dp2[ja[i]]==-1, it means that the previous coins cannot be collected into ja [i], on the contrary, the number is dp[ja[i]]+1, where 1 is the current coin selected. dp1, dp2 can update the minimum and maximum values respectively.
ac code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e4 + 10;
const int inf = 0x3f3f3f3f;
int a[105], dp1[maxn], dp2[maxn];
int main(){
int n, s;
while(~scanf("%d%d", &n, &s)){
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
memset(dp1, inf, sizeof(dp1));
memset(dp2, -1, sizeof(dp2));
dp1[0] = dp2[0] = 0;
for(int i = 1; i <= n; i ++){
for(int j = a[i]; j <= s; j ++){
if(dp1[j - a[i]] != inf)
dp1[j] = min(dp1[j], dp1[j - a[i]] + 1);
if(dp2[j - a[i]] != -1)
dp2[j] = max(dp2[j], dp2[j - a[i]] + 1);
}
}
if(dp1[s] == inf) dp1[s] = -1;
printf("%d %d\n", dp1[s], dp2[s]);
}
return 0;
}