Guarding Bananas
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
2
1
4 4
4
0 0
10 0
10 10
2 1
Sample Output
Case 1: 0
Case 2: 45.0000000
Note
Dataset is huge. Use faster I/O methods.
題目鏈接:https://vjudge.net/problem/LightOJ-1203
題目大意:給n個點,求凸包的最小夾角
思路:Graham掃描算法 | 捲包裹算法 求凸包,點積求最小夾角
代碼:(Graham掃描算法)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long
const double pi=acos(-1.0);
const int N=100005;
struct node
{
ll x,y;
} e[N],s[N],a[N];
bool cmp(node a,node b)
{
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
}
ll cross(node a,node b,node c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
ll dot(node a,node b,node c)
{
return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
}
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp1(node a,node b)
{
ll mm=cross(e[0],a,b);
if(mm>0) return 1;
else if(mm==0&&dis(e[0],a)-dis(e[0],b)<=0)
return 1;
else return 0;
}
int n,tot;
void graham()
{
sort(e,e+n,cmp);
a[0]=e[0];
int l=1;
for(int i=1; i<n; i++)
{
if(e[i].x==e[i-1].x&&e[i].y==e[i-1].y) continue;
a[l++]=e[i];
}
n=l;
if(n<3){tot=n-1;return;}
sort(a+1,a+n,cmp1);
s[0]=a[0],s[1]=a[1];
tot=1;
for(int i=2; i<n; i++)
{
while(tot&&cross(s[tot-1],s[tot],a[i])<=0)
tot--;
s[++tot]=a[i];
}
}
double get(node a,node b,node c)
{
return acos(1.0*dot(a,b,c)/dis(a,b)/dis(a,c));
}
int main()
{
int t,T=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%lld%lld",&e[i].x,&e[i].y);
if(n<3) printf("Case %d: 0\n",T++);
else
{
graham();
if(tot<2)
{
printf("Case %d: 0\n",T++);
continue;
}
s[++tot]=s[0],s[++tot]=s[1];
double ans=pi*2;
for(int i=1; i<tot; i++)
ans=min(ans,get(s[i],s[i-1],s[i+1]));
printf("Case %d: %.7f\n",T++,ans*180/pi);
}
}
return 0;
}
代碼:(捲包裹算法)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
#define ll long long
const double pi=acos(-1.0);
const int N=100005;
struct node
{
ll x,y;
} e[N],s[N],a[N];
bool cmp(node a,node b)
{
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
}
ll cross(node a,node b,node c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
ll dot(node a,node b,node c)
{
return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
}
double dis(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int n,tot;
void convex()
{
sort(e,e+n,cmp);
a[0]=e[0];
int l=1;
for(int i=1; i<n; i++)
{
if(e[i].x==e[i-1].x&&e[i].y==e[i-1].y) continue;
a[l++]=e[i];
}
n=l;
if(n<3){tot=n-1;return;}
s[0]=a[0],s[1]=a[1];
tot=1;
for(int i=2; i<n; i++)
{
while(tot&&cross(s[tot-1],s[tot],a[i])<=0)
tot--;
s[++tot]=a[i];
}
int len=tot;
s[++tot]=a[n-2];
for(int i=n-3;i>=0;i--)
{
while(tot>len&&cross(s[tot-1],s[tot],a[i])<=0)
tot--;
s[++tot]=a[i];
}
}
double get(node a,node b,node c)
{
return acos(1.0*dot(a,b,c)/dis(a,b)/dis(a,c));
}
int main()
{
int t,T=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%lld%lld",&e[i].x,&e[i].y);
if(n<3) printf("Case %d: 0\n",T++);
else
{
convex();
if(tot<3)
{
printf("Case %d: 0\n",T++);
continue;
}
s[++tot]=s[1];
double ans=pi*2;
for(int i=1; i<tot; i++)
ans=min(ans,get(s[i],s[i-1],s[i+1]));
printf("Case %d: %.7f\n",T++,ans*180/pi);
}
}
return 0;
}