不正方形(四个点构成一个凸四边形)
程序员文章站
2022-04-02 21:30:15
...
链接:http://oj.saikr.com/problem/ADPC2-C
题意:两黄个点得在两个红点连线的两边且红点也得在黄点连线的两边
#include<stdio.h>
#include<vector>
using namespace std;
typedef long long ll;
struct node{
ll x,y;
}A[255],B[255];
vector<int>v1,v2;
int main()
{
int N,M;
scanf("%d %d",&N,&M);
for(int i=0;i<N;i++)
scanf("%lld %lld",&A[i].x,&A[i].y);
for(int i=0;i<M;i++)
scanf("%lld %lld",&B[i].x,&B[i].y);
bool Flag=false;
for(int i1=0;i1<N;i1++)
{
for(int i2=i1+1;i2<N;i2++)
{
/*TL
for(int j1=0;j1<M;j1++)
{
for(int j2=j1+1;j2<M;j2++)
{
ll a=A[i2].y-A[i1].y,b=A[i1].x-A[i2].x;
ll c=A[i1].y*(A[i2].x-A[i1].x)+A[i1].x*(A[i1].y-A[i2].y);
int f1=0,f2=0;
if(A[i1].y==A[i2].y)
{
if(B[j1].y<A[i1].y) f1=-1;
else if(B[j1].y>A[i1].y) f1=1;
if(B[j2].y<A[i2].y) f2=-1;
else if(B[j2].y>A[i2].y) f2=1;
}
else if(A[i1].x==A[i2].x)
{
if(B[j1].x<A[i1].x) f1=-1;
else if(B[j1].x>A[i1].x) f1=1;
if(B[j2].x<A[i2].x) f2=-1;
else if(B[j2].x>A[i2].x) f2=1;
}
else
{
if(a*B[j1].x+b*B[j1].y+c<0) f1=-1;
else if(a*B[j1].x+b*B[j1].y+c>0) f1=1;
if(a*B[j2].x+b*B[j2].y+c<0) f2=-1;
else if(a*B[j2].x+b*B[j2].y+c>0) f2=1;
}
if(f1!=0&&f1!=f2)
{
a=B[j2].y-B[j1].y,b=B[j1].x-B[j2].x;
c=B[j1].y*(B[j2].x-B[j1].x)+B[j1].x*(B[j1].y-B[j2].y);
if(B[j1].x==B[j2].x)
{
if(A[i1].x<B[j1].x) f1=-1;
else if(A[i1].x>B[j1].x) f1=1;
else f1=0;
if(A[i2].x<B[j2].x) f2=-1;
else if(A[i2].x>B[j2].x) f2=1;
else f2=0;
}
else if(B[j1].y==B[j2].y)
{
if(A[i1].y<B[j1].y) f1=-1;
else if(A[i1].y>B[j1].y) f1=1;
else f1=0;
if(A[i2].y<B[j2].y) f2=-1;
else if(A[i2].y>B[j2].y) f2=1;
else f2=0;
}
else
{
if(a*A[i1].x+b*A[i1].y+c<0) f1=-1;
else if(a*A[i1].x+b*A[i1].y+c>0) f1=1;
else f1=0;
if(a*A[i2].x+b*A[i2].y+c<0) f2=-1;
else if(a*A[i2].x+b*A[i2].y+c>0) f2=1;
else f2=0;
}
if(f1!=0&&f1!=f2)
Flag=true,i1=i2=N,j1=j2=M;
}
}
}
*/
v1.clear(),v2.clear();//别忘了claer
for(int i=0;i<M;i++)
{
ll a=A[i2].y-A[i1].y,b=A[i1].x-A[i2].x;
ll c=A[i1].y*(A[i2].x-A[i1].x)+A[i1].x*(A[i1].y-A[i2].y);
if(A[i1].y==A[i2].y)
{
if(B[i].y<A[i1].y) v1.push_back(i);
else if(B[i].y>A[i1].y) v2.push_back(i);
}
else if(A[i1].x==A[i2].x)
{
if(B[i].x<A[i1].x) v1.push_back(i);
else if(B[i].x>A[i1].x) v2.push_back(i);
}
else
{
if(a*B[i].x+b*B[i].y+c<0) v1.push_back(i);
else if(a*B[i].x+b*B[i].y+c>0) v2.push_back(i);
}
}
for(int j=0;j<v1.size();j++)
{
for(int k=0;k<v2.size();k++)
{
int j1=v1[j],j2=v2[k];
ll a=B[j2].y-B[j1].y,b=B[j1].x-B[j2].x;
ll c=B[j1].y*(B[j2].x-B[j1].x)+B[j1].x*(B[j1].y-B[j2].y);
int f1=0,f2=0;
if(B[j1].x==B[j2].x)
{
if(A[i1].x<B[j1].x) f1=-1;
else if(A[i1].x>B[j1].x) f1=1;
if(A[i2].x<B[j2].x) f2=-1;
else if(A[i2].x>B[j2].x) f2=1;
}
else if(B[j1].y==B[j2].y)
{
if(A[i1].y<B[j1].y) f1=-1;
else if(A[i1].y>B[j1].y) f1=1;
if(A[i2].y<B[j2].y) f2=-1;
else if(A[i2].y>B[j2].y) f2=1;
}
else
{
if(a*A[i1].x+b*A[i1].y+c<0) f1=-1;
else if(a*A[i1].x+b*A[i1].y+c>0) f1=1;
if(a*A[i2].x+b*A[i2].y+c<0) f2=-1;
else if(a*A[i2].x+b*A[i2].y+c>0) f2=1;
}
if(f1!=0&&f1!=f2)
Flag=true,i1=N,i2=N,j=v1.size(),k=v2.size();//break
}
}
}
}
if(Flag==true) printf("YES");
else printf("NO");
return 0;
}