zoukankan      html  css  js  c++  java
  • HDU2639——背包DP(K最优解)——Bone Collector II

    Description

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link: 

    Here is the link: http://acm.hdu.edu.cn/showproblem.php?pid=2602

    Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum. 

    If the total number of different values is less than K,just ouput 0.
     

    Input

    The first line contain a integer T , the number of cases. 
    Followed by T cases , each case three lines , the first line contain two integer N , V, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone. 
     

    Output

    One integer per line representing the K-th maximum of the total value (this number will be less than 2 31). 
     

    Sample Input

    3 5 10 2 1 2 3 4 5 5 4 3 2 1 5 10 12 1 2 3 4 5 5 4 3 2 1 5 10 16 1 2 3 4 5 5 4 3 2 1
     

    Sample Output

    12 2 0
     大意:背包问题的最优解问题
    用暴力
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int w[1005],v[1005];
        int t1[1005],t2[1005];
        int dp[1005][105];
        int n,m,k,T;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d%d",&n,&m,&k);
            for(int i = 1; i <= n ; i ++)
                scanf("%d",&v[i]);
            for(int i = 1; i <= n; i ++)
                scanf("%d",&w[i]);
           memset(dp,0,sizeof(dp));
           if(k == 0){
               printf("0
    ");
           continue;
           }
        for(int i = 1;i <= n ; i++){
            for(int j = m; j >= w[i];j--){
                for(int l = 1; l <= k ; l++){
                    t1[l] = dp[j][l];
                    t2[l] = dp[j-w[i]][l]+v[i];
                }
                int x = 1,y = 1, t = 1;
                t1[k+1] = t2[k+1] = -1;
                while(t <= k && ( x <= k || y <= k)){
                    if(t1[x] > t2[y])
                    dp[j][t] = t1[x++];
                    else 
                    dp[j][t] = t2[y++];
                    if(t == 1 || dp[j][t]!=dp[j][t-1])
                        t++;
                }
            }
        }
        printf("%d
    ",dp[m][k]);
      }
    return 0;
    }
    View Code
  • 相关阅读:
    Html5页面返回机制解决方案
    Linux(Fedora)下NodeJs升级最新版本(制定版本)
    fedora23开发环境搭建手册
    fedora安装sublime text教程
    实现斐波那契数列之es5、es6
    选择城市下拉框中选择框右对齐,文本右对齐问题
    前端笔记(二)
    前端基础笔记(一)
    解决点击输入框弹出软键盘导致弹窗失效的问题
    angularJS之ng-bind与ng-bind-template的区别
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4448515.html
Copyright © 2011-2022 走看看