P2722 总分 Score Inflation
题目入口:
https://www.luogu.org/problem/P2722
我的感想
这里一开始写成了下面这样:
错误代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
int time, nums;
scanf("%d %d", &time, &nums);
int points[10010], times[10010], dp[10010]; //当前剩余时间下能得到的最大分数
memset(dp, 0, sizeof(dp));
for (int i = 0; i < nums; i++)
scanf("%d %d", &points[i], ×[i]); //获取每种题目的分数及耗时
for (int i = 0; i < nums; i++){ //dp
for (int j = time; j > 0; j--){
if (dp[j-times[i]]+points[i] >= dp[j])
dp[j] = dp[j-times[i]]+points[i];
}
}
printf("%d\n", dp[time]);
return 0;
}
原因在于:每个种类的题目有多个,所以每一种类的题目是没有限定数量的,可以无限次取,是一道裸的完全背包,并非01背包。
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
int time, nums;
scanf("%d %d", &time, &nums);
int points[10010], times[10010], dp[10010]; //当前剩余时间下能得到的最大分数
memset(dp, 0, sizeof(dp));
for (int i = 0; i < nums; i++)
scanf("%d %d", &points[i], ×[i]); //获取每种题目的分数及耗时
for (int i = 0; i < nums; i++){ //dp
for (int j = times[i]; j <= time; j++){
if (dp[j-times[i]]+points[i] >= dp[j])
dp[j] = dp[j-times[i]]+points[i];
}
}
printf("%d\n", dp[time]);
return 0;
}