zoukankan      html  css  js  c++  java
  • HDU 2639 01背包求第k大

    Bone Collector II

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3718    Accepted Submission(s): 1903


    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
     
    Author
    teddy
     
    Source
     
    题意:2602的改版 与普通01背包不同是求第k大价值
     
    题解:增加一维  存储 dp[i][w] 容量为i的背包存放的物品的第w大价值和 
            增加一步 两个序列合并的过程 形成新的k大
     1 #include<iostream>
     2  #include<cstring>
     3  #include<cstdio>
     4  #include<queue>
     5  #include<stack>
     6  #include<map>
     7  #include<set>
     8  #include<algorithm>
     9  #define ll __int64
    10  #define pi acos(-1.0)
    11  #define mod 1
    12  #define maxn 10000
    13  using namespace std;
    14  int t;
    15  int n,v,k;
    16  int a[105];
    17  int b[105];
    18  int we[105],va[105];
    19  int dp[1005][105];
    20  int main()
    21  {
    22      while(scanf("%d",&t)!=EOF)
    23      {
    24          for(int i=1;i<=t;i++)
    25          {
    26              memset(we,0,sizeof(we));
    27              memset(va,0,sizeof(va));
    28              memset(dp,0,sizeof(dp));
    29              scanf("%d %d %d",&n,&v,&k);
    30              for(int j=1;j<=n;j++)
    31               scanf("%d",&we[j]);
    32              for(int j=1;j<=n;j++)
    33               scanf("%d",&va[j]);
    34              for(int j=1;j<=n;j++)
    35              {
    36                  for(int l=v;l>=va[j];l--)
    37                  {
    38                      memset(a,0,sizeof(a));
    39                      memset(b,0,sizeof(b));
    40                      for(int m=1;m<=k;m++)
    41                      {
    42                          a[m]=dp[l][m];
    43                          b[m]=dp[l-va[j]][m]+we[j];
    44                     }
    45                      a[k+1]=-1;
    46                      b[k+1]=-1;
    47                      int x=1,y=1,w=1;
    48                      while(w<=k&&(x<=k||y<=k))//合并的过程
    49                      {
    50                          if(a[x]>b[y])
    51                          {
    52                            dp[l][w]=a[x];
    53                            x++;    
    54                         }
    55                         else
    56                         {
    57                             dp[l][w]=b[y];
    58                             y++;
    59                         }
    60                         if(w==1||dp[l][w]!=dp[l][w-1])       
    61                             w++;
    62                      }
    63                  }
    64              }
    65              cout<<dp[v][k]<<endl;
    66         }
    67     }
    68      return 0;
    69  }
  • 相关阅读:
    XML学习笔记(七)Schema语法杂项
    UML和模式应用第一部分:绪论
    XML学习笔记(六)Schema语法之复杂类型
    XML学习笔记(四)Schema介绍篇
    XML学习笔记(三)进阶篇
    Xml学习笔记(二)Javascript篇
    AutoItLibrary安装报错(robotframework)解决
    robot framework 上个用例的输出作为下个用例的输入 (Set Global Variable的用法)
    robot framework ——关键字run keyword if 如何在一个条件下接多个执行语句,以及如何写复杂条件句
    robot framework 如何获取隐藏元素的文本,以及可见元素的文本
  • 原文地址:https://www.cnblogs.com/hsd-/p/5441013.html
Copyright © 2011-2022 走看看