思路:
是这道题和这道题的结合体,我们逐个击破
c
o
d
e
code
code
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n;
double x[101010], y[101010];
struct node
{
double x, y;
}p1, p2, p3, p4;
double js(node p1, node p2, node p3)
{
return (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x);
}
bool online(node p1, node p2, node p3)
{
if(p3.x>=min(p1.x, p2.x)&&p3.x<=max(p1.x, p2.x)&&
p3.y>=min(p1.y, p2.y)&&p3.y<=max(p1.y, p2.y))
return 1;
else return 0;
}
bool check(node p1, node p2, node p3, node p4)
{
if(js(p3, p4, p1)*js(p3, p4, p2)<0&&js(p1, p2, p3)*js(p1, p2, p4)<0)
return 1;
else if(js(p3, p4, p1)==0&&online(p3, p4, p1))
return 1;
else if(js(p3, p4, p2)==0&&online(p3, p4, p2))
return 1;
else if(js(p1, p2, p3)==0&&online(p1, p2, p3))
return 1;
else if(js(p1, p2, p4)==0&&online(p1, p2, p4))
return 1;
return 0;
}
double js1(double x, double y, double x1, double y1)
{
return x*y1-y*x1;
}
int main()
{
scanf("%d", &n);
for(int i=1; i<=n; i++)
scanf("%lf%lf", &x[i], &y[i]);
if(n<=2)
{
printf("Impossible");
return 0;
}
for(int i=1; i<=n; i++)
for(int j=i+2; j<=n; j++)
{
int i1=i+1, j1=j+1;
if(j1>n) j1=1;
if(j1==1&&i==1)
continue;
p1.x=x[i], p1.y=y[i];
p2.x=x[i1], p2.y=y[i1];
p3.x=x[j], p3.y=y[j];
p4.x=x[j1], p4.y=y[j1];
if(check(p1, p2, p3, p4))
{
printf("Impossible");
return 0;
}
}
double ans=0;
for(int i=1; i<n; i++)
{
int j=i+1;
ans+=js1(x[i], y[i], x[j], y[j])/2.0;
}
ans+=js1(x[n], y[n], x[1], y[1])/2.0;
printf("%.2lf", abs(ans));
return 0;
}
|