Hi,
I'm trying to solve 11072 and am getting WA.
My idea:
Compute the convex hull. Then, to check if the points lie inside the hull, check if the absolute value of the sum of areas formed by the triangle and 2 points of the hull is equal to the area of the convex hull. Is this correct? I think so, because i pass all the cases in the threads.
Here's the code:
Code: Select all
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<list>
#include<queue>
#include<cctype>
#include<stack>
#include<map>
#include<set>
#include<complex>
using namespace std;
template <class type1> type1 cross(const complex <type1> &a, const complex <type1> &b) { return imag(conj(a) * b); }
bool polarcmp(const complex <int> &a,const complex<int> &b){return cross(a,b)>0;}
bool cmp(const complex <int> &a, const complex <int> &b)
{
if(a.imag()==b.imag())
return a.real()<b.real();
else
return a.imag()<b.imag();
}
template < class type1> vector<complex<type1> > convexhull(const vector<complex<type1> > & arg1)
{
vector<complex <type1> > v=arg1;
sort(v.begin(),v.end(),cmp);
complex<type1> p=v[0];
for(unsigned i=0;i<v.size();i++)
v[i]-=p;
stable_sort(v.begin()+1,v.end(),polarcmp);
vector<complex<type1> > s;
s.push_back(v[0]);
s.push_back(v[1]);
for(unsigned i=2;i<v.size();i++)
{
while(s.size()>=2 && cross(s.back()-s[s.size()-2],v[i]-s.back())<=0)
s.pop_back();
s.push_back(v[i]);
}
for(unsigned i=0;i<s.size();i++)
s[i]+=p;
return s;
}
template<class type1> type1 area(vector<complex<type1> > p)
{
type1 sum=0;
for(unsigned i=1;i<p.size()-1;i++)
{
sum+=cross(p[i]-p[0],p[i+1]-p[0]);
}
return sum;
}
int main()
{
int n;
cin>>n;
vector<complex<int> > v(n);
for(int i=0;i<n;i++)
{
int a,b;
scanf(" %d %d",&a,&b);
v[i]=complex<int>(a,b);
}
vector<complex<int> > chull=convexhull(v);
int checkarea=area(chull);
int p;
cin>>p;
for(int i=0;i<p;i++)
{
int x,y;
scanf(" %d %d",&x,&y);
int testarea=0;
for(int j=0;j<chull.size();j++)
{
vector<complex<int> > a(3);
a[0]=complex<int> (x,y);
a[1]=chull[j];
a[2]=chull[(j+1)%chull.size()];
int t=area(a);
testarea+=(t>=0?t:-t);
}
if(checkarea==testarea)
printf("inside\n");
else
printf("outside\n");
}
return 0;
}