B. 丁神又去谷歌 2014新生暑假个人排位赛02
题目描述
丁神又要去Google上班了,这一次丁神想多做几道水题,并使题目的总水量最大.丁神同一时刻只能在水一道题,只有做完这道题才能得到它的水值,丁神的总时间为 t ,现在一共有 n 道题,编号从1到 n ,每道题有两个值 a 和 b , a 为做这道题需要的时间, b 为题目的水值。
输入格式
输入第一行为数据组数 T(T≤10) ,接下来 T 组数据,每组数据中第一行为两个数 t 和 n , n 为题目的数量, t 为总时间,接下来 n 行,每行两个正整数 a 和 b 。 (1≤a,t≤1000,1≤n≤100,1≤b≤1000000000)
输出格式
对于每组数据,输出对应的最大总水量,每个输出占一行。
输入样例
1
10 2
8 16
6 12
输出样例
16
赛中提交:WA WA WA AC
题目大意:简单的背包DP
思路:
背包dp
WA原因和反省:
(1)dp数组的第二个下标开得太小,原来只开了106,而t(背包容量)的范围是1000
开成了1006就过了
(2)另外就是long long的问题,数据很大最大是1e10,因为还要一直加和,所以必须要开int
下面是AC代码
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 100005
using namespace std;
typedef long long ll;
int v[106],w[106];
ll dp[106][1006];
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n,t;
memset(dp,0,sizeof(dp));
scanf("%d %d",&t,&n);
for(int i=0;i<n;i+=1){
scanf("%d %d",&w[i],&v[i]);
}
for(int i=0;i<n;i+=1){
for(int j=0;j<=t;j+=1){
if(j<w[i]){
dp[i+1][j]=dp[i][j];
}
else{
dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
}
}
}
printf("%lld\n",dp[n][t]);
}
return 0;
}