其他分享
首页 > 其他分享> > [SCOI2007]最大土地面积

[SCOI2007]最大土地面积

作者:互联网

嘟嘟嘟


无意间看到了一个计算几何。


\(n <= 2000\)就很愉快了。枚举求完凸包后\(O(n ^ 2)\)枚举对角线,然后另两个点用旋转卡壳维护就完事了。
结果数据(或是题意)坑人,有的有重复的点,如果选了两个重复的点的话就算成三角形了(凭什么),所以应该求一个最简凸包(就是点最少)。


哎,本来十几分钟就写完了的,因为这个坑爹错误找了半天。

#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 In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e3 + 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
{
  db x, y;
  In Point operator + (const Point& oth)const
  {
    return (Point){x + oth.x, y + oth.y};
  }
  In Point operator - (const Point& oth)const
  {
    return (Point){x - oth.x, y - oth.y};
  }  
  In db operator * (const Point& oth)const
  {
    return x * oth.y - y * oth.x;
  }
  friend In db dis(Point A)
  {
    return A.x * A.x + A.y * A.y;
  }
}p[maxn], S;

In bool cmp(Point& A, Point& B)
{
  db s = (A - S) * (B - S);
  if(fabs(s) > eps) return s > eps;
  return dis(A - S) < dis(B - S);
}
int st[maxn], top = 0;
In void Graham()
{
  S = p[1];
  for(int i = 2; i <= n; ++i)
    if(p[i].x < S.x || (fabs(p[i].x - S.x) < eps && p[i].y < S.y)) S = p[i];
  sort(p + 1, p + n + 1, cmp);
  for(int i = 1; i <= n; ++i)
    {
      while(top > 1 && (p[st[top]] - p[st[top - 1]]) * (p[i] - p[st[top - 1]]) < eps) --top;
      st[++top] = i;
    }
}

db ans = 0;
In db calc(Point A, Point B, Point C, Point D)
{
  return ((B - A) * (C - A) + (C - A) * (D - A)) * 0.5;
}
In int nxt(int x) {return x + 1 > top ? 1 : x + 1;}
In void Rota()
{
  for(int i = 1; i <= top; ++i)
    {
      int k = i, h = nxt(nxt(i));
      for(int j = nxt(nxt(i)); nxt(nxt(j)) ^ i; j = nxt(j))
    {
      while(nxt(k) != j && (p[st[k]] - p[st[i]]) * (p[st[j]] - p[st[i]]) < (p[st[nxt(k)]] - p[st[i]]) * (p[st[j]] - p[st[i]])) k = nxt(k);
      while(nxt(h) != i && (p[st[j]] - p[st[i]]) * (p[st[h]] - p[st[i]]) < (p[st[j]] - p[st[i]]) * (p[st[nxt(h)]] - p[st[i]])) h = nxt(h);
      ans = max(ans, calc(p[st[i]], p[st[k]], p[st[j]], p[st[h]]));
    }
    }
}

int main()
{
  n = read();
  for(int i = 1; i <= n; ++i) scanf("%lf %lf", &p[i].x, &p[i].y);
  Graham(); Rota();
  printf("%.3lf\n", ans);
  return 0;
}

标签:oth,SCOI2007,const,Point,面积,db,土地,return,include
来源: https://www.cnblogs.com/mrclr/p/10388272.html