题解 P2283 【[HNOI2003]多边形】
题目链接:Link
Solution
这题一看图就会发现这是个半平main交。。。然后就可以开心地敲啦!
code:
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
struct Point
{
double x,y;
Point():x(0),y(0) { }
Point(double _x,double _y):x(_x),y(_y) { }
};
typedef Point Vector;
inline Vector operator+(const Vector &a,const Vector &b) { return Vector(a.x+b.x,a.y+b.y); }
inline Vector operator-(const Vector &a,const Vector &b) { return Vector(a.x-b.x,a.y-b.y); }
inline Vector operator*(const Vector &a,double b) { return Vector(a.x*b,a.y*b); }
inline Vector operator/(const Vector &a,double b) { return Vector(a.x/b,a.y/b); }
inline double Dot(const Vector &a,const Vector &b) { return a.x*b.x+a.y*b.y; }
inline double Cross(const Vector &a,const Vector &b) { return a.x*b.y-a.y*b.x; }
inline double Length(const Vector &a) { return sqrt(Dot(a,a)); }
const double eps=1e-8;
inline int dcmp(const double &v) { return fabs(v)<eps?0:(v>0?1:-1); }
struct Line
{
Point p; Vector v;
double ang;
Line(Point _p=Point(),Vector _v=Vector()):p(_p),v(_v) { ang=atan2(v.y,v.x); }
};
inline bool operator<(const Line &a,const Line &b) { return a.ang<b.ang; }
inline bool OnLeft(const Line &L,const Point &p) { return dcmp(Cross(L.v,p-L.p))==1; }
Point GetLineIntersection(const Line &a,const Line &b)
{
Vector u=a.p-b.p;
double t=Cross(b.v,u)/Cross(a.v,b.v);
return a.p+a.v*t;
}
double Area(int n,Point* p)
{
double res=0;
for(int i=1;i<=n-2;i++) res+=Cross(p[i+1]-p[i],p[0]-p[i]);
return res/2;
}
int BPMJ(int n,Line *L,Point* poly)
{
sort(L,L+n);
int first,last;
Point *p=new Point[n];
Line *q=new Line[n];
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;
int m=0;
p[last]=GetLineIntersection(q[last],q[first]);
for(int i=first;i<=last;i++) poly[m++]=p[i];
return m;
}
const int maxn=1505;
int n,x,y;
Point p[maxn],poly[maxn];
Line m[maxn];
int main()
{
#ifdef local
freopen("pro.in","r",stdin);
#endif
scanf("%d",&n);
for(int i=n-1;i>=0;i--) scanf("%lf%lf",&p[i].x,&p[i].y);
for(int i=0;i<n;i++) m[i]=Line(p[i],p[(i+1)%n]-p[i]);
// for(int i=0;i<n;i++) printf("(%lf,%lf) (%lf,%lf)\n",m[i].p.x,m[i].p.y,m[i].v.x,m[i].v.y);
int cnt=BPMJ(n,m,poly);
// for(int i=0;i<cnt;i++) printf("(%lf,%lf)\n",poly[i].x,poly[i].y);
printf("%.2lf\n",Area(cnt,poly));
return 0;
}
然后我们意识到了一件事:Case1 WA了!!!看了半天也没看明白。。。最后翻了翻前人的AC记录后发现:数据出锅了,它点的给出顺序是逆时针,而默认的输入顺序为顺时针。怎么办呢?如果你懒的话直接打表过数据,考场上求稳的话跑一跑凸包然后比较一些采样点的排列顺序瞎搞一通就行了。。。
本作品由happyZYM采用知识共享 署名-非商业性使用-相同方式共享 4.0 (CC BY-NC-SA 4.0) 国际许可协议(镜像(简单版)镜像(完整版))进行许可。
转载请注明出处:https://www.cnblogs.com/happyZYM/p/11379542.html (近乎)全文转载而非引用的请在文首添加出处链接。