poj1228 Grandpa's Estate
Grandpa's Estate
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14553 | Accepted: 4070 |
Description
Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.
Output
There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.
Sample Input
1 6 0 0 1 2 3 4 2 0 2 4 5 0
Sample Output
NO
Source
题意:给出n个凸包上的点,问有没有更大面积的凸包包含原凸包上的所有点.
分析:凸包已经是凸的了,如果一个凸包上有≥3个点,那么就不可能存在.问题就是如何求凸包上的每一条边是否有≥3个点.
首先将凸包给求出来.然后对于一个点i,如果i与i-1的边和i 与 i+1的边不共线 并且 i和i+1的边 与 i+1和i+2的边不共线,那么i所在的边就只有2个点.根据叉积是否为0来判断.
不过还有一个问题,如果用极角排序的话共线的点不会被放进凸包,那么就必须要用到水平排序了,正着求一次凸包,反着求一次凸包即可.
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int n,tot,ans,T; const double eps = 1e-8; struct node { double x,y; } p[50010],q[50010]; node sub(node a,node b) { node temp; temp.x = a.x - b.x; temp.y = a.y - b.y; return temp; } double det(node a,node b) { return a.x * b.y - a.y * b.x; } bool cmp(node a,node b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } void solve() { sort(p + 1,p + 1 + n,cmp); for (int i = 1; i <= n; i++) { while (tot >= 2 && det(sub(q[tot],q[tot - 1]),sub(p[i],q[tot - 1])) < 0) //千万不要加= tot--; q[++tot] = p[i]; } int k = tot + 1; for (int i = n - 1; i >= 1; i--) { while (tot >= k && det(sub(q[tot],q[tot - 1]),sub(p[i],q[tot - 1])) < 0) tot--; q[++tot] = p[i]; } } bool cc(double aa) { if (fabs(aa) < eps) return false; return true; } bool check() { q[tot + 1] = q[1]; for (int i = 2; i < tot - 1; i++) { if (det(sub(q[i + 1],q[i - 1]),sub(q[i],q[i - 1])) != 0.0 && det(sub(q[i + 2],q[i]),sub(q[i + 1],q[i])) != 0.0) return false; } return true; } int main() { scanf("%d",&T); while (T--) { tot = 0; scanf("%d",&n); for (int i = 1; i <= n; i++) scanf("%lf%lf",&p[i].x,&p[i].y); if (n < 6) { puts("NO"); continue; } solve(); if (check()) puts("YES"); else puts("NO"); } return 0; }