hdu 2639 (0-1背包第k优解)Bone Collector II

Bone Collector II

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


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 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
 

Author
teddy
 

Source
 

Recommend
teddy
 
尝试用结构体来储存,到最后再排序,但是感觉会有的情况是不存在的,所以还是用队列标记再合并:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int val[1005],v[1005];
int f[1005][35],A[35],B[35];
int N,V,K;
void dp()
{
    int i,j,k,a,b,c;
    memset(f,0,sizeof(f));
    for(i=1;i<=N;i++)
    for(j=V;j>=v[i];j--)
    {
        for(k=1;k<=K;k++)
        {
           A[k]=f[j-v[i]][k]+val[i];
           B[k]=f[j][k];
        }
        A[k]=-1;B[k]=-1;
        a=b=c=1;
        while(c<=K&&(A[a]!=-1||B[b]!=-1))
        {
            if(A[a]>B[b])
            f[j][c]=A[a],a++;
            else
            f[j][c]=B[b],b++;
            if(f[j][c]!=f[j][c-1])
            c++;
        }
    }
    cout<<f[V][K]<<endl;
}
int main()
{
    int t,i;
    cin>>t;
    while(t--)
    {
        cin>>N>>V>>K;
        for(i=1;i<=N;i++)
        cin>>val[i];
        for(i=1;i<=N;i++)
        cin>>v[i];
        dp();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
背包问题贪心算法背包问题 ---- * 已知有n种物品和一个可容纳M重量的背包,每种物品i的重量是w[i]。假定将物品i的一部分x[i]放入背包就会得到p[i]x[i]的效益,这里, * 0<=x[i]<=1,p[i]>0.采用怎样的方法才能使装包的效益最大呢? * 考虑以下情况下的背包问题:n = 3,M = 20,(p0,p1,p2) = (25,24,15),(w0,w1,w2) = * (18,15,10).其中的4个可行解是 * (x0,x1,x2) w0x0 + w1x1 + w2x2 p0x0 + p1x1 + p2x2 * (1/2,1/3,1/4) 16.5 24.25 * (1,2/15,0) 20 28.2 * (0,2/3,1) 20 31 * (0,1,1/2) 20 31.5 * 在这4个可行解中第四个的效益值最大。 定理:如果 p1/w1>=p2/w2>=...>=pn/wn,则算法对于给定的背包问题实例生成一个最优解。 证明: * 设X= (x1,...,xn)是最优解。如果所有的xi = 1,显然这个解是最优解。于是,设j是使xj != 1 的最小下标。由算法可知,对于1<=i<=j * ,xi=1;对于 j<i<=n,xi =0;对于j, 0<=xj<1.如果X不是一个最优解,则必定存在一个可行解Y=(y1,...yn),使得 * piyi > pixi.不失 一般性,可以假定 wiyi =M.设k是使得yk!=xk的最小下标。显然,这样的k必定存在。由上面的假设,可以推得yk<xk. * 这可从3种可能发生的情况,即k<j,k=j,k>j分别得到证明: (1)若k<j,则xk = 1.因yk!=xk,从而yk<xk. (2)若k=j ,由于 ∑wjxi = * M,且对1<=i<j,有xi=yi=1,而对j<i<=n,有xi =0.若yk>xk,显然有∑wiyi>M,与Y是可行解矛盾。若yk=xk * ,与假设yk!=xk矛盾,故yk<xk. (3)若k>j,则∑wiyi>m,这是不可能的。 * 现在,假定把yk增加到xk,那么必须从(yk+1,...,yn)中减去同样多的量,使得所有的总容量仍是M。这导致一个新的解Z=(z1,...zn), * 其中,zi = xi , 1<=i<=k,并且∑(k<i<=n)wi(yi-zi)= wk(zk-yk).因此,对于Z有 * ∑pizi = ∑piyi + (zk-yk)wkpk/wk-∑(k<i<=n)(yi-zi)wipi/wi * >= ∑piyi +[(zk-yk)wk-∑(yi-zi)wi]pk/wk * = ∑piyi * 如果∑pizi>∑piyi,则Y不可能是最优解。如果这两个和数相等,同时Z=X,则X就是最优解;若Z!=X,则重复上面的讨论,或者证明Y不是最 * 优解,或者把Y转换成X,从而证明了X也是最优解。证毕。 */ public class BinSerch { //对数组buf降序排列 同时 index 数组记录排序前的数组索引 public static void order(double[] buf, int[] index) { int count = 1; while (count++ < buf.length) { for (int i = buf.length - 1; i > 0; i--) { if (buf[i] > buf[i - 1]) { double temp = buf[i]; buf[i] = buf[i - 1]; buf[i - 1] = temp; int temp1 = index[i]; index[i] = index[i - 1]; index[i - 1] = temp1; } else continue; } } for (int j = 0; j < buf.length; j++) { System.out.print(buf[j] + "(" + j + ")"); } System.out.println(); } public static void main(String[] args) { //对上述背包问题求最优解 int n = 3; //物品数量 double[] p = { 25, 24, 15 }; //效益数组 double[] w = { 18, 15, 10 }; //重量数组 double[] pw = { p[0] / w[0], p[1] / w[1], p[2] / w[2] }; //选取pi/wi为其量度标准 int[] index = { 0, 1, 2 }; //数组索引 double[] record = new double[3];//记录排序前数组下标 double cu = 20; //背包剩余容量 order(pw, index); //排序 //背包问题贪心算法 int i = 0; for (i = 0; i < n; i++) { if (w[index[i]] < cu) { record[i] = 1; cu = cu - w[index[i]]; } else { break; } } if (i < n) { record[i] = cu / w[index[i]]; } for (int j = 0; j < record.length; j++) { System.out.print("x" + j + "\t"); System.out.print(record[j] + "\t"); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值