light1203 - Guarding Bananas【凸包】

1203 - Guarding Bananas
Time Limit: 3 second(s)Memory Limit: 32 MB

Once there was a lazy monkey in a forest. But he loved banana too much. One day there was a storm in the jungle and all the bananas fell from the trees. The monkey didn't want to lose any of the bananas. So, he wanted to find a banana such that he can eat that and he can also look after the other bananas. As he was lazy, he didn't want to move his eyes too wide. So, you have to help him finding the banana from where he can look after all the bananas but the degree of rotating his eyes is as small as possible. You can assume that the position of the bananas can be modeled as 2D points.

Here a banana is shown, from where the monkey can look after all the bananas with minimum eye rotation.

Input

Input starts with an integer T (≤ 13), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 105) denoting the number of bananas. Each of the next n lines contains two integers x y (-109 ≤ x, y ≤ 109) denoting the co-ordinate of a banana. There can me more than one bananas in the same co-ordinate.

Output

For each case, print the case number and the minimum angle in degrees. Errors less than 10-6 will be ignored.

Sample Input

Output for Sample Input

2

1

4 4

4

0 0

10 0

10 10

2 1

Case 1: 0

Case 2: 45.0000000

Note

Dataset is huge. Use faster I/O methods.

题意:求凸包的最小角重点较多
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define PI acos(-1.0)
#define inf 1e20
using namespace std;
struct point{
	double x,y;
}A[100010],result[100010];
int sig(double n){
	if(fabs(n)<eps)return 0;
	else if(n<0)return -1;
	return 1;
}
double MIN(double a,double b){
	return a<b?a:b;
}
double dis(point a,point b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
double dist(point a,point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cp(point p1,point p2,point p3){
	return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
bool cmp1(point a,point b){
	if(a.y==b.y)
		return a.x<b.x;
	return a.y<b.y;
}
bool cmp2(point a,point b){
	double ans=cp(A[0],a,b);
	if(sig(ans)==0)
		return dis(A[0],a)-dis(A[0],b)<=0;
	return ans>0;
}
int main()
{
	int t,k=1,n,i,j;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		int pos=0;
		for(i=0;i<n;++i){
			scanf("%lf%lf",&A[i].x,&A[i].y);
		}
		double angle=inf;
		if(n<=2)angle=0;
		else{
			sort(A,A+n,cmp1);
			int m=1;
			for(i=1;i<n;++i){
				if(A[i].x==A[i-1].x&&A[i].y==A[i-1].y)continue;
				A[m++]=A[i];
			}n=m;
			sort(A+1,A+n,cmp2);
			int top=1;
			result[0]=A[0];
			result[1]=A[1];
			for(i=2;i<n;++i){
				while(top>1&&sig(cp(result[top-1],result[top],A[i]))<=0)top--;
				result[++top]=A[i];
			}
			if(top<2)angle=0;
			else{
				result[top+1]=result[0];result[top+2]=result[1];
				for(i=1;i<=top+1;++i){
					double a=dist(result[i],result[i-1]);
					double b=dist(result[i],result[i+1]);
					double c=dist(result[i-1],result[i+1]);
					double temp=acos((a*a+b*b-c*c)/(2.0*a*b));
					angle=MIN(angle,temp);
				}
				if(angle==inf)angle=0;
			}
		}
		printf("Case %d: %.8lf\n",k++,180*angle/PI);
	}
	return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值