PAT TOP 1023 The Best Polygon (35)

问题描述:

1023 The Best Polygon (35 分)

An n-gon is a polygon with n sides. For example, a triangle is a 3-gon. Now you are asked to find the best n-gon in a given convex N-gon. The vertices of the n-gon are selected from vertices of the N-gon. The n-gon you are supposed to find must have the largest area among all possible n-gons, which means that it approximates the N-gon the best. The figure below shows the best 6-gon (the shaded part) in a 10-gon.

6gon.JPG

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N ( 6≤N≤300 ) which is the total number of vertices of the convex N-gon, and n ( 6≤n≤min(10,N) ) which is the total number of vertices of the approximating convex n-gon. Then N lines follow, the i-th line gives the 2-D coordinates (x, y) of the i-th vertex ( i=0,⋯,N−1 ). The x and y coordinates in a line are real numbers with their absolute values no more than 1000, and they are separated by a space.

Output Specification:

Print in a line all the vertex indices of the best n-gon in descending order. All the numbers are separated by a space and there must be no extra space at the beginning or the end of the line. It is guaranteed that the solution is unique.

Sample Input:

10 6
133.0 1.0
544.0 71.0
558.0 206.0
536.0 338.0
463.0 436.0
330.0 503.0
188.0 499.0
305.0 2.0
55.0 410.0
2.0 140.0

Sample Output:

9 8 5 3 1 0

 

时隔一年之后,博主终于在这题上拿到了AC。。。

博主第一次看到这题大约是2018年3月下旬。那时候,博主当然用了自己唯一掌握的的dfs程序,经过了数天的努力,终于在2,4,5号测试点获得了运行超时 。显然dfs的时间复杂度太高。。。身心俱疲的博主抱着试一试的心理,在自己学校的评优网站上晒出了此题。我校的ACM大佬迅速地给出了动态规划的算法。因为博主那时候比较忙,就拖着没有去仔细地阅读她的程序。。。

(图中的关键信息已经被隐去。。。)

这一拖就拖了一年。。。一年之后的今天,再次闲下来的博主再一次拿出了此题。花了两天,写出了自己的动态规划程序。。。

这一题总共分为三步:1.利用极角排序算法把题目中给的散乱的点列以顺/逆时针方向排序。2.用动态规划的方法计算出最大面积。3.回溯动态规划过程,找到这个过程所选取的点。

关于1:对于给出的所有点,先计算出它们的几何中心。调用math库中的atan2()函数把所有点对于几何中心的极角计算出来并依次排序;

关于2:开三维数组vdp[i][j][k](i<j,k<j-i),表示以第i个顶点为起始,以第j个顶点为结尾的子图中所有包含i-j边的k边形的最大面积。状态转移方程如下:vdp[i][j][k]=max[vdp[i][jj][k-1]+S(i,jj,j)] 其中jj在(i,j)内变动,S(i,jj,j)表示三角形(i,jj,j)的面积的两倍。

关于3:vdp[i][j][k]不仅保存一个表示最大面积的double变量s,也保存每一次状态转移int型的jj。这样,最后在vdp[m][][]里找到最大面积时,可以通过jj找到上一个状态的位置。

关于优化:在初始化vdp数组,即k=3时,可以通过旋转卡壳法求出面积最大的三角形。

(p.s.当年ACM大佬的动态规划在2号测试点没能通过的原因是:博主给她的极角排序算法存在隐藏的bug。。。)

 

久违的AC代码:

#include<bits/stdc++.h>
using namespace std;
double x,y;
int n,m;
const double eps=1e-8;
inline int sgn(double d)
{return d<-eps?-1:d>eps;}
struct point
{
	double x;
	double y;
	int id;
} po;
struct cmp1 
{
	bool operator() (const point& p1,const point& p2) const
	{
		return atan2((p1.y-y),(p1.x-x))<atan2((p2.y-y),(p2.x-x));
	}
};
vector<point> v;
vector<double> va;
inline double area(int n1,int n2)
{
	if(!sgn(va[n1*n+n2]))
	{
		va[n1*n+n2]=v[n1].x*v[n2].y-v[n1].y*v[n2].x;
		va[n2*n+n1]=-va[n1*n+n2];
	}
	return va[n1*n+n2];
}
int main()
{
//	freopen("data1.txt","r",stdin);
	scanf("%d %d",&n,&m);
	va.resize(n*n,0.0);
	x=0.0;
	y=0.0;
	for(int i=0;i<n;i++)
	{
		scanf("%lf %lf",&po.x,&po.y);
		x+=po.x;
		y+=po.y;
		po.id=i;
		v.emplace_back(po);
	}
	x=x/n;
	y=y/n;
	sort(v.begin(),v.end(),cmp1());
	vector<pair<double,int> > vdp((n-2)*(n-2)*(m-2),make_pair(0.0,0));
	for(int i=0;i<n-2;i++)
	{
		for(int j=i+2;j<n;j++)
		{
			double maxs=0.0;
			int k=i+1;
			for(;k<j;)
			{
				double s=area(i,k)+area(k,j)+area(j,i);
				if(sgn(s-maxs)>0)
				{
					k++;
					maxs=s;
				}
				else
				break;
			}
			vdp[i*(n-2)+j-2].first=maxs;
			if(k<j)
			vdp[i*(n-2)+j-2].second=k-1;
			else
			vdp[i*(n-2)+j-2].second=j-1;			
		}
	}
	for(int l=1;l<m-2;l++)
	{
		for(int i=0;i<n-l-2;i++)
		{
			double maxs=0.0;
			int kk=0;
			for(int j=i+l+2;j<n;j++)
			{
				for(int jj=i+l+1;jj<j;jj++)
				if(sgn(maxs-vdp[(l-1)*(n-2)*(n-2)+i*(n-2)+jj-2].first-area(jj,j)-area(j,i)-area(i,jj))<0)
				{
					maxs=vdp[(l-1)*(n-2)*(n-2)+i*(n-2)+jj-2].first+area(jj,j)+area(j,i)+area(i,jj);
					kk=jj;
				}
				vdp[l*(n-2)*(n-2)+i*(n-2)+j-2].first=maxs;
				vdp[l*(n-2)*(n-2)+i*(n-2)+j-2].second=kk;
			}
		}
	}
	double maxs=0.0;
	int ii=0,jj=0;
	for(int i=0;i<(n-m+1);i++)
	{
		for(int j=i+m-3;j<n-2;j++)
		{
			if(sgn(maxs-vdp[(m-3)*(n-2)*(n-2)+i*(n-2)+j].first)<0)
			{
				maxs=vdp[(m-3)*(n-2)*(n-2)+i*(n-2)+j].first;
				ii=i;
				jj=j;
			}
		}
	}
	vector<int> vr;
	vr.emplace_back(v[ii].id);
	vr.emplace_back(v[jj+2].id);
	for(int i=m-3;i>=0;i--)
	{
		vr.emplace_back(v[vdp[i*(n-2)*(n-2)+ii*(n-2)+jj].second].id);
		jj=vdp[i*(n-2)*(n-2)+ii*(n-2)+jj].second-2;
	}
	sort(vr.begin(),vr.end(),greater<int>());
	printf("%d",vr[0]);
	for(int i=1;i<m;i++)
	printf(" %d",vr[i]);
    return 0;
}

 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值