UVA - 12589 Learning Vector(dp-01背包)

时间:2024-04-17 15:08:08

题目:

UVA - 12589 Learning Vector(dp-01背包)

UVA - 12589 Learning Vector(dp-01背包)

思路:

dp[j][h]表示选取了j个向量,且高度为h,利用01背包来解决问题。

没选当前的向量:dp[j][h] = dp[j][h];

选了当前的向量:dp[j][h] = dp[j-1][h-p[i].y]+2*p[i].x*(h-p[i].y)+p[i].area;(p[i].area = p[i].x+p[i].y)

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000007
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,ll> pii;
const int maxn = ;
int dp[maxn][maxn*maxn];
int n,k,H;
struct Point{
int x,y,area;
}p[maxn]; bool cmd(const Point& a,const Point& b){
return a.y*b.x > a.x*b.y;
} int main(){//01背包
//FRE();
int kase;
scanf("%d",&kase);
for(int o=; o<=kase; o++){
H = ;
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++){
scanf("%d%d",&p[i].x,&p[i].y);
p[i].area = p[i].x*p[i].y;
H += p[i].y;
}
sort(p+,p+n+,cmd);
memset(dp,-,sizeof(dp));
dp[][] = ;
for(int i=; i<=n; i++){//枚举所有的向量
for(int j=k; j>=; j--){//枚举到向量i时,对k个向量中的值进行更新
for(int h=H; h>=p[i].y; h--){
if(dp[j-][h-p[i].y]!=-){
dp[j][h] = max(dp[j][h],dp[j-][h-p[i].y]+(h-p[i].y)*p[i].x*+p[i].area);
}
}
}
}
int ans=;
for(int i=; i<=H; i++){
ans = max(ans,dp[k][i]);
}
printf("Case %d: %d\n",o,ans);
}
return ;
}