Problem
A certain forest consists of N trees, each of which is inhabited by a squirrel.
The boundary of the forest is the convex polygon of smallest area which contains every tree, as if a giant rubber band had been stretched around the outside of the forest.
Formally, every tree is a single point in two-dimensional space with unique coordinates (Xi, Yi), and the boundary is the convex hull of those points.
Some trees are on the boundary of the forest, which means they are on an edge or a corner of the polygon. The squirrels wonder how close their trees are to being on the boundary of the forest.
One at a time, each squirrel climbs down from its tree, examines the forest, and determines the minimum number of trees that would need to be cut down for its own tree to be on the boundary. It then writes that number down on a log.
Determine the list of numbers written on the log.
Input
The first line of the input gives the number of test cases, T. T test cases follow; each consists of a single line with an integer N, the number of trees, followed by N lines with two space-separated integers Xi and Yi, the coordinates of each tree. No two trees will have the same coordinates.
Output
For each test case, output one line containing “Case #x:”, followed by N lines with one integer each, where line i contains the number of trees that the squirrel living in tree i would need to cut down.
Limits
-106 ≤ Xi, Yi ≤ 106.
Small dataset
1 ≤ T ≤ 100.
1 ≤ N ≤ 15.
Large dataset
1 ≤ T ≤ 14.
1 ≤ N ≤ 3000.
Sample
Input
Output
2
5
0 0
10 0
10 10
0 10
5 5
9
0 0
5 0
10 0
0 5
5 5
10 5
0 10
5 10
10 10
Case #1:
0
0
0
0
1
Case #2:
0
0
0
0
3
0
0
0
0
In the first sample case, there are four trees forming a square, and a fifth tree inside the square. Since the first four trees are already on the boundary, the squirrels for those trees each write down 0. Since one tree needs to be cut down for the fifth tree to be on the boundary, the fifth squirrel writes down 1.
简述一下题目:
平面上有一些点,问删除掉那些点才能使得某一个点成为凸包边界上的点。
一个点成为凸包上的点的充要条件是:
过该点的某一条直线的一侧没有点。
那么我们枚举每一个点,然后以这个点为原点建系,并将其他点极角排序。
然后只要用一条直线按照极角序转一圈,计算出一侧最少的点即可。
如何极角排序呢?
①需要用到C++中的函数atan2(y,x):
简单来说就是从第三象限开始,逆时针递增
②还有一种方法是看象限,然后用叉积算。
int cmp(const point&a,const point&b){
if(a==b)return 0;
int s1=(a.y<=0 && a.x<=0 || a.y<0);
int s2=(b.y<=0 && b.x<=0 || b.y<0);
if(s1!=s2)return s1>s2;
return a*b>0;
}
代码:O(n2logn)
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define eps 1e-9
#define Pi acos(-1)
using namespace std;
int x[3005],y[3005],T,N;
double a[6005];
int main()
{
cin>>T;
for (int t=1;t<=T;t++)
{
printf("Case #%d:\n",t);
scanf("%d",&N);
for (int i=1;i<=N;i++)
scanf("%d%d",&x[i],&y[i]);
for (int i=1;i<=N;i++)
{
int n=0;
for (int j=1;j<=N;j++)
if (i!=j)
a[++n]=atan2(y[j]-y[i],x[j]-x[i]);
sort(a+1,a+1+n);
for (int j=1;j<=n;j++)
a[j+n]=a[j]+2*Pi;
int j=1,ans=n;
for (int k=1;k<=n;k++)
{
j=max(k,j);
while (a[j+1]<a[k]+Pi-eps)
j++;
ans=min(ans,j-k);
}
cout<<ans<<endl;
}
}
return 0;
}