No Game School
Problem Description
BGod is a college student who love computer games, but unfortunately his school just published a rule that Games should disappear in the campus , that means nobody can play computer games in school, including the students dormitory. However, the student don’t take it seriously, that’s why the manager of the school gets so angry that he decided to go to the dormitory to punish the students. You should know the manager any punish those students who is playing games at the moment he enter the room, and leave immediately if nobody is playing game in this room.
BGod is a talented game player , in fact, he is the Carry of a famous Gaming club, so he needs time to practice he’s skills . And luckily , BGod’s roommate PY is a Geek(also a BGod fan), he hacked the manager’s computer and get the timetable of the manager, and tell BGod when will the manager come to their room tomorrow. A big E-sport Event is around the corner, so BGod list m skills he should practice, each skill takes some time to practice and improve BGod’s skill by some points. You should calculate the max total improvement points BGod can get. Note any skills can be practiced any times , including 0.
Input
The first line contains a integer T ( T <= 10 ), then T cases follows.
In each case, there are 3 parts of input.
The first part contains 3 integers L, n, m in a single line.Range[0, L] is the time BGod decide to practice , n is the times manager would enter his room, m indicate the total number of the skills.
The second part contains n integers ti(0 <= ti <= L) in a single line, means the manager would enter his room at ti. Note that ti is giving in the increasing order.
The third part has m lines , each line contains two integers ci(ci <= m), vi(vi <= 200), means this skill needs ci minutes to practice and can make vi points improvement.
L <= 500
n <= 10
m <= 100
Output
For each case, you should output the max points BGod can improve.
Sample Input
2
6 1 3
3
2 3
2 4
2 5
6 2 3
2 4
2 3
2 4
2 5
Sample Output
10
15
Hint
Note that BGod will be catch playing games any in the interval of his practicing, excluded the beginning and the ending of each practice time.
心路历程:看完题之后以为是多个01背包想了半个小时,直到考试结束后才后悔没好好读题。。。
多个完全背包,对老师进来的时间进行预处理,分成体积大小不同的背包,注意老师不进来时要单讨论一下
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int ca;
scanf("%d",&ca);
while(ca--)
{
int l,n,m,arr[13],brr[13],crr[111],vrr[111],i,j,k,w,sum=0,dp[555];
scanf("%d%d%d",&l,&n,&m);
if(l==0||m==0)//注意讨论
{
printf("0\n");
continue;
}
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
if(n!=0)//将大背包分成了很多小背包
{
brr[0]=arr[0];
for(i=1; i<n; i++)
{
brr[i]=arr[i]-arr[i-1];
}
brr[n]=l-arr[n-1];
}
else//不进来的时候
{
brr[0]=l;
}
for(i=1; i<=m; i++)
{
scanf("%d%d",&crr[i],&vrr[i]);
}
for(w=0; w<=n; w++)//完全背包模板
{
memset(dp,0,sizeof(dp));
for(i=1; i<=m; i++)
{
for(j=crr[i]; j<=brr[w]; j++)
{
dp[j]=max(dp[j],dp[j-crr[i]]+vrr[i]);
}
}
sum+=dp[brr[w]];
}
printf("%d\n",sum);
}
return 0;
}
期末不挂!