Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 40 Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
Output
Sample Input
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
Sample Output
0 -1 1
#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <queue> using namespace std; int main() { int n, m, k, s; while (cin >> n >> m >> k >> s) { int dp[105];//用来存储经验,dp[j]表示容忍度为j时的最大经验值 memset(dp, 0, sizeof(dp)); int i, j; int a[105], b[105]; for (i = 1; i <= k; i++) { cin >> a[i] >> b[i]; } int ss[105];//s[j]表示容忍度为j时所打怪兽的总个数 memset(ss, 0, sizeof(ss)); int min = 9999999; for (i = 1; i <= k; i++) { for (j = b[i]; j <= m; j++)//每个怪兽可以打无数次,是完全背包问题 { if (dp[j - b[i]] + a[i] >= dp[j] && ss[j - b[i]] + 1 <= s) { ss[j] = ss[j - b[i]] + 1;//个数更新 dp[j] = dp[j - b[i]] + a[i];//经验更新 if (dp[j] >= n && j < min) {//一旦更新看看j(容忍度)是不是最小的 min = j; } } } } if (min == 9999999) cout << "-1" << endl; else cout << m - min << endl;//求的是剩余,所以要减 } return 0; }
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 40 Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Input
Output
Sample Input
10 10 1 10 1 1 10 10 1 9 1 1 9 10 2 10 1 1 2 2
Sample Output
0 -1 1
#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <queue> using namespace std; int main() { int n, m, k, s; while (cin >> n >> m >> k >> s) { int dp[105];//用来存储经验,dp[j]表示容忍度为j时的最大经验值 memset(dp, 0, sizeof(dp)); int i, j; int a[105], b[105]; for (i = 1; i <= k; i++) { cin >> a[i] >> b[i]; } int ss[105];//s[j]表示容忍度为j时所打怪兽的总个数 memset(ss, 0, sizeof(ss)); int min = 9999999; for (i = 1; i <= k; i++) { for (j = b[i]; j <= m; j++)//每个怪兽可以打无数次,是完全背包问题 { if (dp[j - b[i]] + a[i] >= dp[j] && ss[j - b[i]] + 1 <= s) { ss[j] = ss[j - b[i]] + 1;//个数更新 dp[j] = dp[j - b[i]] + a[i];//经验更新 if (dp[j] >= n && j < min) {//一旦更新看看j(容忍度)是不是最小的 min = j; } } } } if (min == 9999999) cout << "-1" << endl; else cout << m - min << endl;//求的是剩余,所以要减 } return 0; }