TYVJ1214
大水题,凑数
恰好装满的完全背包求最大最小价值
复习:完全背包从小到大枚举,恰好装满初始化要为无穷大正负分别考虑
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #define INF 11111111 6 using namespace std; 7 const int maxn = 105; 8 const int maxv = 10005; 9 int dp[maxv],dp1[maxv]; 10 int a[maxn]; 11 int main() 12 { 13 int n,s; 14 cin>>n>>s; 15 for(int i = 1;i<=n;++i)cin>>a[i]; 16 for(int i = 1;i<=s;++i){dp[i] = -INF;dp1[i] = INF;} 17 dp[0] = dp1[0] = 0; 18 for(int i = 1;i<=n;++i) 19 for(int j = a[i];j<=s;++j) 20 { 21 dp[j] = max(dp[j],dp[j-a[i]]+1); 22 dp1[j] = min(dp1[j],dp1[j-a[i]]+1); 23 } 24 printf("%d\n%d\n",dp1[s],dp[s]); 25 return 0; 26 }
弱者究竟为何而战?!