UVA12589_Learning Vector
大致题意:
有n个向量要你选k个,把这k个向量连起来,画出来的与x轴围成的面积最大
思路:
这个是斜率dp,让斜率大的排在前面,记忆化搜索的时候要加入一个当前高的信息,因为这个向量形成面积不仅和斜率有关还有当前高有关
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 60;
struct node{
int x;
int y;
}vec[maxn];
int t,k,n,kase;
int d[maxn][maxn][maxn*maxn];
int vis[maxn][maxn][maxn*maxn];
bool cmp(const node& a,const node& b){
return a.y*b.x>a.x*b.y;
}
int dfs(int id,int cnt,int h){
if(cnt==k) return 0;
if(id==n+1) return 0;
int &ans = d[id][cnt][h];
if(vis[id][cnt][h]==kase) return ans;
ans=0;vis[id][cnt][h]=kase;
ans=max(ans,dfs(id+1,cnt,h));
int add=2*h*vec[id].x+vec[id].x*vec[id].y;
ans=max(ans,dfs(id+1,cnt+1,h+vec[id].y)+add);
return ans;
}
int main(){
cin>>t;
for(kase=1;kase<=t;++kase){
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>vec[i].x>>vec[i].y;
sort(vec+1,vec+n+1,cmp);
printf("Case %d: %d\n",kase,dfs(1,0,0));
}
return 0;
}
浙公网安备 33010602011771号