RE的6种可能
1、数组开小了
2、数组越界了
3、除0了
4、递归爆栈了
5、指针
6、申请空间过多了。
还是感觉自己有问题,码得太多,想的太少,实践得更少。
自己先跟自己玩玩攻防游戏,多玩几盘,就知道正确的防守方法了,然后思路与代码就是分分钟的事情。
先抛开代码与算法不管,然后搞出一种手算的解决方案(像玩单机游戏那样去不断尝试),最后再想办法用代码去实现。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 50010;
struct Point
{
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
const double eps = 1e-10;
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
Point ReadPoint()
{
double x,y;
scanf("%lf %lf",&x,&y);
return Point(x,y);
}
Point operator + (Point A,Vector B)
{
return Point(A.x+B.x,A.y+B.y);
}
Vector operator - (Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator * (Vector A,double t)
{
return Vector(A.x*t,A.y*t);
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double angle(Vector V)
{
return atan2(V.y,V.x);
}
struct Line
{
Point p;
Vector v;
double ang;
Line(Point p=Point(),Vector v=Vector()):p(p),v(v)
{
ang=angle(v);
}
bool operator < (const Line& rhs) const
{
return ang<rhs.ang;
}
};
bool OnLeft(Line L,Point P)
{
return dcmp(Cross(L.v,P-L.p))>0;
}
Point GetLineIntersection(Line A,Line B)
{
Vector u=B.p-A.p;
double t=Cross(u,B.v)/Cross(A.v,B.v);
return A.p+A.v*t;
}
Point p[maxn];
Line q[maxn];
int GetHalfPlaneIntersection(Line* L,int n,Point* poly)
{
sort(L,L+n);
int first,last;
q[first=last=0]=L[0];
for(int i=1;i<n;i++)
{
while(first<last&&!OnLeft(L[i],p[last-1])) last--;
while(first<last&&!OnLeft(L[i],p[first])) first++;
q[++last]=L[i];
if(dcmp(Cross(q[last].v,q[last-1].v))==0)
{
last--;
if(OnLeft(q[last],L[i].p)) q[last]=L[i];
}
if(first<last) p[last-1]=GetLineIntersection(q[last-1],q[last]);
}
while(first<last&&!OnLeft(q[first],p[last-1])) --last;
if(last-first<=1) return 0;
p[last]=GetLineIntersection(q[last],q[first]);
int m=0;
for(int i=first;i<=last;++i)
poly[m++]=p[i];
return m;
}
int n;
Point P[maxn];
Point poly[maxn];
Line L[maxn];
int main()
{
while(scanf("%d",&n)==1&&n)
{
for(int i=0;i<n;i++)
P[i]=ReadPoint();
for(int i=0;i<n/2;i++)
{
Point temp=P[i];
P[i]=P[n-i-1];
P[n-i-1]=temp;
}
int l=1,r=n;
while(l<r)
{
int m=l+(r-l)/2;
for(int i=0;i<n;i++)
L[i]=Line(P[i],P[(i+m)%n]-P[i]);
if(!GetHalfPlaneIntersection(L,n,poly)) r=m;
else l=m+1;
}
printf("%d\n",l-1);
}
return 0;
}