zoukankan      html  css  js  c++  java
  • HDU

    先上题目:

    Watch The Movie

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 4722    Accepted Submission(s): 1497


    Problem Description
    New semester is coming, and DuoDuo has to go to school tomorrow. She decides to have fun tonight and will be very busy after tonight. She like watch cartoon very much. So she wants her uncle to buy some movies and watch with her tonight. Her grandfather gave them L minutes to watch the cartoon. After that they have to go to sleep.
    DuoDuo list N piece of movies from 1 to N. All of them are her favorite, and she wants her uncle buy for her. She give a value Vi (Vi > 0) of the N piece of movies. The higher value a movie gets shows that DuoDuo likes it more. Each movie has a time Ti to play over. If a movie DuoDuo choice to watch she won’t stop until it goes to end.
    But there is a strange problem, the shop just sell M piece of movies (not less or more then), It is difficult for her uncle to make the decision. How to select M piece of movies from N piece of DVDs that DuoDuo want to get the highest value and the time they cost not more then L.
    How clever you are! Please help DuoDuo’s uncle.
     
    Input
    The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case:
    The first line is: N(N <= 100),M(M<=N),L(L <= 1000)
    N: the number of DVD that DuoDuo want buy. 
    M: the number of DVD that the shop can sale.
    L: the longest time that her grandfather allowed to watch.
    The second line to N+1 line, each line contain two numbers. The first number is the time of the ith DVD, and the second number is the value of ith DVD that DuoDuo rated.
     
    Output
    Contain one number. (It is less then 2^31.)
    The total value that DuoDuo can get tonight.
    If DuoDuo can’t watch all of the movies that her uncle had bought for her, please output 0.
     
    Sample Input
    1
    3 2 10
    11 100
    1 2
    9 1
     
    Sample Output
    3

       题意:给你n部电影,l分钟,每部电影有时长和观看价值,要你从中选不多不少m部电影出来,,保证在l分钟以内观看价值是最大的。

      这是一条二维01背包,基本做法只需在一维的基础上再加一维,不过这里还有一个限制条件,那就是不多不少选m部,所以需要保证最终选到的电影恰好有m部,而不是最多有m部。

      状态转移方程: j>=c[i] && k>=1 && dp[j-c[i]][k-1]>=0(意思是中间选了k-1部片子)时  dp[j][k] = max{dp[j][k] , dp[j-c[i]][k-1]+v[i]}

      具体过程看代码

    上代码:

    #include <cstdio>
    #include <cstring>
    #define MAX 1001
    #define max(x,y) (x > y ? x : y)
    using namespace std;
    
    int v[100],c[100],dp[MAX][101];
    int n,m,l;
    
    int main()
    {
        int t;
        //freopen("data.txt","r",stdin);
        scanf("%d",&t);
        while(t--){
            scanf("%d %d %d",&n,&m,&l);
            for(int i=0;i<n;i++){
                scanf("%d %d",&c[i],&v[i]);
            }
    
            /**
             * 初始化是关键,只有j==0的时候全部赋为0,代表一开始的最大值为0,赋为-1,是为了
             * 保证最终选取m部片子,不多不少,如果中间少选了片子的话,是得到-1而已
             */
            for(int i=0;i<=l;i++){
                for(int j=0;j<=m;j++){
    
                    dp[i][j]= (j==0 ? 0 : -1);
                }
            }
    
            /**
             * 在别人的代码中发现了新的优化技巧,那就是因为当j<c[i]的时候永远是跳过的,所以j的范围改成l~c[i]
             * 可以提高效率,也就是说将范围压缩在边界~当前物品的对应代价之间
             */
            for(int i=0;i<n;i++){
                for(int j=l;j>=c[i];j--){
                        for(int k=m;k>=1;k--){
                            if(dp[j-c[i]][k-1]>=0) dp[j][k]=max(dp[j][k] , dp[j-c[i]][k-1]+v[i]);
                            //printf("%d ",dp[j][k]);
                        }
                        //printf("    ");
                }
                //printf("
    ");
            }
    
            /**
             * 如果有最大值,一定会出现在dp[l][m]中,因为dp[l][]的的含义就是前l个物品中的最大值
             */
            if(dp[l][m]>=0) printf("%d
    ",dp[l][m]);
            else printf("0
    ");
        }
        return 0;
    }
    3496
  • 相关阅读:
    Uncaught DOMException: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document. 打包vue dist到外网,无法访问
    linux find 命令
    CentOS提示 Failed to set locale, defaulting to C的解决方法
    JSP页面上添加Fckeditor
    对自己成为程序员的要求
    数组举例
    Java中构造方法被别封装后的调用
    JS读取/创建本地文件及目录文件夹的方法
    初学SSH(struts+spring+hibernate)的纠结问题
    POI Excel文件XSSF导入知识点
  • 原文地址:https://www.cnblogs.com/sineatos/p/3530029.html
Copyright © 2011-2022 走看看