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;
}
这里着重说一下为什么要对这些矢量按斜率进行排序:
其次,对于选出来的矢量,我们一定按照斜率大的先放的策略进行放置。理由如下:
对于任意选定的一组矢量收尾相加(这里的矢量都满足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;
}
相关文章推荐
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- android BaseAdapter
- XML的四种解析器(dom_sax_jdom_dom4j)原理及性能比较
- Vuforia SDK----增强现实非凡体验
- java之 ------ 类反射【详解】
- nodejs 笔记
- BAPI 修改销售订单的方法 ‘BAPI_SALESORDER_CHANGE’
- Unity3D的Mono是什么
- python-selenium多浏览器支持测试框架
- redis配置文件redis.conf参数说明
- 菜鸟应该怎么学习seo
- 深入解析C++中的虚函数与多态
- [LeetCode#58]Length of Last Word
- [其它招式]强大的VBS,自动化利器
- 线程与进程初理解!
- 第一天
- C#中的多线程 - 同步基础
- unity3d AssetBundle包加密
- 博客已搬家至 :qinshaoxuan.com
- 创业失败