题目链接:(—_—) zZ
题目大意: 有n个骨头, 和一个容量为v的背包, 现在给出每个骨头的重量和价值, 求在容量为v的背包中能装的第k大的价值。
思路:dp[j][k]表示容量为j时第k大的价值。
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int a[32], b[32], w[122], V[122], dp[122][32];
int main()
{
int i = 0, j = 0, l = 0, t = 0, n = 0, v = 0, k = 0, c1 =0, c2 = 0, c3 =0;
scanf("%d", &t);
while(t--)
{
scanf("%d %d %d", &n, &v, &k);
for(i = 1; i<=n; i++)
scanf("%d", &V[i]);
for(i = 1; i<=n; i++)
scanf("%d", &w[i]);
memset(dp, 0, sizeof(dp));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for(i = 1; i<=n; i++)
{
for(j = v; j>=w[i]; j--)
{
for(l = 1; l<=k; l++)
{
a[l] = dp[j][l];
b[l] = dp[j-w[i]][l]+V[i];//第i大选取当前的
}
c1 = 1, c2 = 1, c3 = 1;
while(c3<=k && (c1<=k || c2<=k))//合并第k大的
{
if(a[c1]<b[c2])
dp[j][c3] = b[c2++];
else
dp[j][c3] = a[c1++];
if(dp[j][c3] != dp[j][c3-1])
c3++;
}
}
}
printf("%d\n", dp[v][k]);
}
return 0;
}