题目链接:点这里!!!
题意:01背包的第k优策略。
题解:因为k只有30,我们设f[v][k]为当前容量为v的第k优策略。dp的时候我们一样各取前k个去更新就可以啦!!!
具体看代码。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
const LL MOD = 1000000007;
const int N = 105;
const int maxn = 1e5+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,vs,k,c[N],w[N],f[N*10][35];
int pp[35],qq[35];
int main(){
int T;
scanf("%d",&T);
while(T--){
clr(f,0);
scanf("%d%d%d",&n,&vs,&k);
for(int i=0;i<n;i++) scanf("%d",w+i);
for(int i=0;i<n;i++) scanf("%d",c+i);
for(int i=0;i<n;i++){
for(int v=vs;v>=c[i];v--){
for(int j=1;j<=k;j++){
pp[j]=f[v][j];
qq[j]=f[v-c[i]][j]+w[i];
}
int a=1,b=1,c=1;
while(c<=k&&(a<=k||b<=k)){
if(a<=k&&b<=k){
if(pp[a]>qq[b]) f[v][c]=pp[a++];
else f[v][c]=qq[b++];
}
else if(a<=k) f[v][c]=pp[a++];
else if(b<=k) f[v][c]=qq[b++];
if(f[v][c]!=f[v][c-1]) c++;
}
}
}
printf("%d\n",f[vs][k]);
}
return 0;
}
/*
100
5 10 2
1 1 3 3 4
5 5 2 1 7
*/