zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    63         }
    64     }
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    【华为云技术分享】从自建MongoDB聊聊云数据库MongoDB的蓬勃张力
    【华为云技术分享】【Python算法】分类与预测——支持向量机
    Python 中更优雅的环境变量设置方案
    Python解析照片EXIF信息,获取坐标位置
    【华为云技术分享】【Python算法】分类与预测——决策树
    【华为云技术分享】使用keil5打开GD32F450i的MDK项目出现的问题以及J-Link无法烧录程序对应的解决方案
    【华为云技术分享】小熊派IoT开发板华为物联网操作系统LiteOS内核实战教程01-IoT-Studio介绍及安装
    【华为云技术分享】【开发记录】Linux服务器维护常用命令(二)
    【PHP输出两位小数】使用PHP来输出保留两位小数的数字【原创】
    【SERVER_NAME】PHP中的SERVER_NAME【原创】
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6178521.html
Copyright © 2011-2022 走看看