题目传送门
解题思路:
f[i]表示凑总面值i所需的最少邮票张数,然后快乐的跑完全背包.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<map> 4 #include<cstring> 5 6 using namespace std; 7 8 int k,n,a[51],aa,now,f[2000001]; 9 map<int,bool> vis; 10 11 int main() { 12 scanf("%d%d",&k,&n); 13 for(int i = 1;i <= n; i++) { 14 scanf("%d",&a[i]); 15 aa = max(aa,a[i]); 16 } 17 memset(f,0x3f3f3f,sizeof(f)); 18 f[0] = 0; 19 for(int i = 1;i <= n; i++) f[a[i]] = 1; 20 for(int i = 1;i <= n; i++) 21 for(int j = a[i];j <= aa * k; j++) 22 f[j] = min(f[j],f[j-a[i]] + 1); 23 for(int i = 1;i <= aa * k; i++) 24 if(f[i] > k) { 25 printf("%d",i - 1); 26 return 0; 27 } 28 printf("%d",k * aa); 29 return 0; 30 }