POJ1228 Grandpa's Estate

嘟嘟嘟


一句话题意:给定一个凸包,判断这个凸包是否是稳定凸包。


稳定凸包就是新加一个点,如果新的凸包和原来一样,就是稳定凸包;否则就不是。
判断很简单,如果凸包上的一条边只有两个端点的话,就不是稳定凸包了。
刚开始我想跑出点数最多的凸包(就是共线的点都算上),然后(O(n))判断。然而经过极轴排序发现是不可能的。最后不得不到网上找题解,结果发现竟然都是这种错误的想法,真不知道那些把(WA)了的程序贴上来的人是什么怎么想的。
正解是(O(n ^ 2))的。跑出点数最多的凸包不可能,但是最少的是可以的。弹栈的时候如果有向面积是(0),那也弹。
跑完后,对于每一条边,在(O(n))判断有几个点在这条边上,如果点数小于(3),就说明不是稳定凸包。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e3 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n;
struct Point
{
  int x, y;
  Point operator - (const Point& oth)const
  {
    return (Point){x - oth.x, y - oth.y};
  }
  int operator * (const Point& oth)const
  {
    return x * oth.y - oth.x * y;
  }
  friend int dis(const Point& A)
  {
    return A.x * A.x + A.y * A.y;
  }
}p[maxn],S;

bool cmp(Point A, Point B)
{
  int s = (A - S) * (B - S);
  if(s) return s > 0;
  return dis(S - A) < dis(S - B);
}

int st[maxn], top = 0;
void Graham()
{
  top = 0;
  int id = 1;
  for(int i = 2; i <= n; ++i)
    if(p[i].x < p[id].x || (p[i].x == p[id].x && p[i].y < p[id].y)) id = i;
  if(id != 1) swap(p[id].x, p[1].x), swap(p[id].y, p[1].y);
  S.x = p[1].x, S.y = p[1].y;
  sort(p + 2, p + n + 1, cmp);
  st[++top] = 1;
  for(int i = 2; i <= n; ++i)
    {
      while(top > 1 && (p[st[top]] - p[st[top - 1]]) * (p[i] - p[st[top - 1]]) <= 0) top--;
      st[++top] = i;
    }
}
int main()
{
  int T = read();
  while(T--)
    {
      n = read();
      for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
      if(n < 6) {puts("NO"); continue;}
      Graham();
      st[top + 1] = 1;
      bool flg = 1;
      for(int i = 1, cnt = 0; i <= top && flg; ++i)
	{
	  cnt = 0;
	  for(int j = 1; j <= n; ++j)
	    if((p[st[i + 1]] - p[st[i]]) * (p[j] - p[st[i]]) == 0) cnt++;
	  if(cnt < 3) flg = 0;
	}
      puts(flg ? "YES" : "NO");
    }
  return 0;
}
原文地址:https://www.cnblogs.com/mrclr/p/9995507.html