您的位置:首页 > 其它

uva 12589 learning vector

2015-09-09 00:11 381 查看
题意分析:给出n个矢量,从中选择k个,以坐标原点为起点,收尾相连,问:这样的k个周围相连矢量与x轴围成图形的最大面积的两倍是多少?

这里着重说一下为什么要对这些矢量按斜率进行排序:

其次,对于选出来的矢量,我们一定按照斜率大的先放的策略进行放置。理由如下:

对于任意选定的一组矢量收尾相加(这里的矢量都满足x>=0,y>=0),其最终最右端高度是一样的,其水平宽度是一样的。每一个矢量都为它的后继矢量提供了一个基底高度,下图红色部分即为第一个矢量提供的基底高度,斜率高的矢量具有高度高,长度短的特点,为后继提供的面积更大。至此,粗略证明了贪心的正确性。

// Created by Chenhongwei in 2015.
// Copyright (c) 2015 Chenhongwei. All rights reserved.

#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#include"climits"
#include"queue"
#include"cmath"
#include"map"
#include"set"
#include"stack"
#include"vector"
#include"sstream"
#include"algorithm"
using namespace std;
typedef long long ll;
struct data
{
int x,y;
}a[100];
bool cmp(data l,data r)
{
if(l.x==0&&r.x==0)
return l.y>r.y;
else
return l.y*r.x>r.y*l.x;
}
int dp[100][3000];
int main()
{
//ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,n,k;
cin>>T;
for(int z=1;z<=T;z++)
{
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i].x>>a[i].y;
sort(a+1,a+n+1,cmp);
memset(dp,0xc3,sizeof dp);
dp[0][0]=0;
for(int i=1;i<=n;i++)
for(int h=2500;h>=a[i].y;h--)
for(int j=k;j>=1;j--)
dp[j][h]=max(dp[j][h],dp[j-1][h-a[i].y]+(h*2-a[i].y)*a[i].x);
int ans=0;
for(int i=1;i<=2500;i++)
ans=max(ans,dp[k][i]);
cout<<"Case "<<z<<": "<<ans<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: