HRBUST--2317 Game(完全背包)

Kim 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 will 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.

  Kim 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 , Kim’s roommate Mik is a Geek(also a Kim fan), he hacked the manager’s computer and get the timetable of the manager, and tell Kim when will the manager come to their room tomorrow. A big E-sport Event is around the corner, so Kim list m skills he should practice, each skill takes some time to practice and improve Kim’s skill by some points. You should calculate the max total improvement points Kim 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 Kim 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, vi, 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 Kim 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 Kim will be catch playing games any time in the interval of his practicing, excluded the beginning and the ending of each practice time. 

Sample 1:

D.Game sample 1

Sample 2:

D.Game sample 2

题意:ACM实验室不能玩游戏老师会进行检查,如果没有发现立刻离开。给出老师检查的时间点,每个技能需要练习的时间长度和所得分数,问如何使所得分数最多?

思路:比赛的时候只是知道是一个背包问题,没有学过完全背包就用01背包去做一直没A。回来之后学习了一下完全背包发现就是一个模板题。用老师检查的时间点将总时间分成n+1段,然后对每段分别使用完全背包进行计算,加和结果就是最后的答案。

完全背包模板:

1 memset(dp,0,sizeof(dp));
2                 for(int j=0;j<k;j++)
3                 {
4                     for(int v=p[j].x;v<=f[i];v++)
5                     {
6                         dp[v]=max(dp[v],dp[v-p[j].x]+p[j].y);
7                     }
8                 }

AC代码:

 1 #include <iostream>
 2 #include<cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 struct node
 7 {
 8     int x,y;
 9 } p[105];
10 int cmp1(const struct node &a,const struct node &b)
11 {
12     if(a.x!=b.x)
13         return a.x<b.x;
14     else
15         return a.y>b.y;
16 }
17 int main()
18 {
19     int f[105];
20     int t,l,n,m;
21     while(~scanf("%d",&t))
22     {
23         while(t--)
24         {
25             scanf("%d%d%d",&l,&n,&m);
26             for(int i=0;i<n;i++)
27             {
28                 scanf("%d",&f[i]);
29             }
30             f[n]=l;
31             for(int i=n;i>0;i--)
32                 f[i]=f[i]-f[i-1];
33             for(int i=0;i<m;i++)
34             {
35                 scanf("%d%d",&p[i].x,&p[i].y);
36             }
37             sort(p,p+m,cmp1);
38             int k=1;
39             for(int i=1;i<m;i++)
40             {
41                 if(p[i].x!=p[k-1].x)
42                 {
43                     p[k].x=p[i].x;
44                     p[k].y=p[i].y;
45                     k++;
46                 }
47             }
48             int ans=0;
49             int dp[505];
50             for(int i=0;i<=n;i++)
51             {
52                 memset(dp,0,sizeof(dp));
53                 for(int j=0;j<k;j++)
54                 {
55                     for(int v=p[j].x;v<=f[i];v++)
56                     {
57                         dp[v]=max(dp[v],dp[v-p[j].x]+p[j].y);
58                     }
59                 }
60                 ans=ans+dp[f[i]];
61             }
62             printf("%d\n",ans);
63         }
64     }
65     return 0;
66 }
View Code

 

posted @ 2016-12-14 11:18  Wally的博客  阅读(166)  评论(0编辑  收藏  举报