题目大意:抽离模型为判定输入点是否全部在凸包上,是输出“No”,否输出“Yes”。
解题策略:求出凸包,直接判定“栈”中顶点数top与nodeNum的关系即可(一开始以为要用到判定点是否在多边形内这个算法,后来发现不需要)
/*
UVA 10078 The Art Gallery
AC by J_Dark
ON 2013/5/6 16:45
Time 0.012s
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
/
struct point{
double x, y;
point(double a, double b){
x = a;
y = b;
}
double Distance(point t){
return sqrt((x-t.x)*(x-t.x) + (y-t.y)*(y-t.y));
}
};
vector<point> p;
vector<int> CH; //存放凸包顶点序号 模拟栈
int testCase, top, cc=0, nodeNum;
/
void Input(){
p.clear();
CH.clear();
CH.resize(nodeNum+5);
double xx, yy;
for(int i=0; i<nodeNum; i++){
cin >> xx >> yy;
p.push_back(point(xx, yy));
}
}
bool cmp(point a, point b){
if(a.y == b.y) return a.x < b.x;
return a.y < b.y;
}
bool turnRight(point px1, point px2, point pp){
const double eps = 1e-20;
if((px2.x - px1.x)*(pp.y - px2.y) - (pp.x - px2.x)*(px2.y - px1.y) <= eps) return true;
return false;
}
void ComputeCH(){
sort(p.begin(), p.end(), cmp);
CH[0] = 0;
CH[1] = 1;
top = 1;
//从起点0到到排序最后点作凸包右链 过程1
for(int i=2; i<nodeNum; i++){
while( top && turnRight(p[CH[top-1]], p[CH[top]], p[i]) )
{
top--;
}
CH[++top] = i;
}
int len = top;
//从排序最高点到到起点0fab反向作凸包右链 过程2
CH[++top] = nodeNum-2;
for(int i=nodeNum-3; i>=0; i--){
//top!=len, 不考虑已在过程1生成凸包上的点
while( top!=len && turnRight(p[CH[top-1]], p[CH[top]], p[i]) )
{
top--;
}
CH[++top] = i;
}
}
void Output(){
if(top == nodeNum) cout << "No" << endl; //输入点全在凸包上
else cout << "Yes" << endl;
}
/
int main(){
while(cin >> nodeNum && nodeNum)
{
Input();
ComputeCH(); //计算凸包
Output(); //判断并输出
}
return 0;
}