zoukankan      html  css  js  c++  java
  • Bone Collector II(DP+第K优解)

     Bone Collector II

     HDU - 2639 

    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.

    InputThe 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. 
    OutputOne 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
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 #define inf 0x3f3f3f3f
     7 typedef long long ll;
     8 const int maxn=1100;
     9 int dp[maxn][maxn];
    10 int val[maxn],w[maxn];
    11 int casen,n,V,k;
    12 int a[maxn],b[maxn];
    13 int main()
    14 {
    15     cin>>casen;
    16     while(casen--)
    17     {
    18         scanf("%d%d%d",&n,&V,&k);
    19         for(int i=0;i<n;i++)
    20             scanf("%d",&val[i]);
    21         for(int i=0;i<n;i++)
    22             scanf("%d",&w[i]);
    23         memset(dp,0,sizeof(dp));
    24         if(k==0)
    25         {
    26             printf("0
    ");
    27             continue;
    28         }
    29         for(int i=0;i<n;i++)
    30         {
    31             for(int j=V;j>=w[i];j--)
    32             {
    33                 for(int m=1;m<=k;m++)
    34                 {
    35                     a[m]=dp[j][m];
    36                     b[m]=dp[j-w[i]][m]+val[i];
    37                 }
    38                 int x=1,y=1,w=1;
    39                 a[k+1]=b[k+1]=-inf;
    40                 while(w<=k&&(x<=k||y<=k))
    41                 {
    42                     if(a[x]>b[y])
    43                     {
    44                         dp[j][w]=a[x++];
    45                     }
    46                     else
    47                     {
    48                         dp[j][w]=b[y++];
    49                     }
    50                     if(w==1||dp[j][w]!=dp[j][w-1])//去重
    51                     {
    52                         w++;
    53                     }
    54                 }
    55             }
    56         }
    57         printf("%d
    ",dp[V][k]);
    58     }
    59 }
    60 /*和01背包的思路一样,j从V倒着循环,越循环得到的当前体积下的最优解值越大。
    61 dp[j][m]表示的是在体积为j的情况下第m大最优解,而这第m大最优解与dp[j][m]和dp[j-w[i]][m]+val[i]有关,至于
    62 为什么不写成dp[j][m]=max(dp[j][m],dp[j-w[i]][m]+val[i]),而是要开a和b两个数组,是因为比如说有两组从大到小的数据:
    63 第一组   第二组
    64 10         8
    65 5          6
    66 1          2
    67 max(dp[j][m],dp[j-w[i]][m]+val[i])在算m=1时,得到10便会把8掩盖,因而应该开两个数组分别记录dp[j][m]和dp[j-w[i]][m]+val[i]的m大值
    68 最后以10 8 6 5 2 1的顺序赋值给dp[j][1],dp[j][2].....
    69 */
  • 相关阅读:
    点击添加按钮添加一条记录,点击删除按钮删除本条记录
    两个input在一行让它们能对齐
    H5页面在微信中禁止下拉露出网页
    纯css实现隐藏滚动条仍可以滚动
    jQuery弹出层layer插件的使用
    flex组合流动布局实例---利用css的order属性改变盒子排列顺序
    媒体查询样式失效的解决办法
    menu-普通menu弹出框样式
    5lession-path路径相关操作
    do_pj--下拉代码脚本的使用
  • 原文地址:https://www.cnblogs.com/1013star/p/9609754.html
Copyright © 2011-2022 走看看