zoukankan      html  css  js  c++  java
  • HDU 2639 Bone Collector II

    http://acm.hdu.edu.cn/showproblem.php?pid=2639

    Problem 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 231).
     
    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 <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 1e5 + 10;
    int T;
    int N, V, K;
    int value[110], weight[110];
    int dp[1010][60];
    int a[1010], b[1010];
    
    void ZeroOnePack() {
        memset(dp, 0, sizeof(dp));
        memset(a, -1, sizeof(a));
        memset(b, -1, sizeof(b));
        for(int i = 0; i < N; i ++) {
            for(int j = V; j >= weight[i]; j --) {
                for(int k = 1; k <= K; k ++) {
                    a[k] = dp[j][k];
                    b[k] = dp[j - weight[i]][k] + value[i];
                }
    
                int x, y, z;
                x = y = z = 1;
                while(z <= K && (x <= K || y <= K)) {
                    if(a[x] > b[y]) dp[j][z] = a[x ++];
                    else dp[j][z] = b[y ++];
    
                    if(dp[j][z] != dp[j][z - 1])
                        z ++;
                }
            }
        }
    }
    
    int main() {
        scanf("%d", &T);
        while(T --) {
            scanf("%d%d%d", &N, &V, &K);
            for(int i = 0; i < N; i ++)
                scanf("%d", &value[i]);
            for(int i = 0; i < N; i ++)
                scanf("%d", &weight[i]);
    
            ZeroOnePack();
            printf("%d
    ", dp[V][K]);
        }
        return 0;
    }
    

      用 a b 分别存储两种情况 加或者不加然后合并到 $dp$ 中 $dp[i][k]$ 代表的是在装在背包里为 $j$ 的时候第 $k$ 个的价值 

  • 相关阅读:
    VBScript把json字符串解析成json对象的2个方法
    vue+php接口
    td标签 内容垂直、水平居中
    win7 安装 IIS 配置ASP 【原创】
    PS 实用技巧
    通信原理实践(一)——音频信号处理
    德飞莱STM32单片机学习(一)——下载环境搭建
    电赛总结(四)——波形发生芯片总结之AD9854
    电赛总结(四)——波形发生芯片总结之AD9851
    电赛总结(四)——波形发生芯片总结之AD9834
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10156653.html
Copyright © 2011-2022 走看看