POJ2079 Triangle

Posted mrclr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2079 Triangle相关的知识,希望对你有一定的参考价值。

嘟嘟嘟


题意:给一堆点,求其中三个点构成的三角形的最大面积。


刚开始不知咋的忘了三角形三条边可能都不在凸包上,然后快速的打了个旋转卡壳结果(WA)了。还是自己太年轻了……


正解也是旋转卡壳。对于三角形三个点(i, j, k)(k, j, i)挨个旋转就行啦。

#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 = 5e4 + 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 inline int dis(const Point& A)
  {
    return A.x * A.x + A.y * A.y;
  }
}p[maxn], S;

bool cmp(const Point& A, const Point& B)
{
  int s = (A - S) * (B - S);
  if(s) return s > 0;
  return dis(A - S) < dis(B - S);
}
int st[maxn], top = 0;
void Graham()
{
  int id = 1; top = 0;
  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 nxt(int x)
{
  if(++x > top) x = 1;
  return x;
}
db area(Point A, Point B, Point C)
{
  return (B - A) * (C - A);
}
db rota()
{
  db ans = 0;
  for(int i = 1, j = 2, k = 3; i <= top; ++i)
    {
      while(nxt(k) != i && area(p[st[i]], p[st[j]], p[st[k]]) < area(p[st[i]], p[st[j]], p[st[nxt(k)]])) k = nxt(k);
      ans = max(ans, area(p[st[i]], p[st[j]], p[st[k]]));
      while(nxt(j) != k && area(p[st[i]], p[st[j]], p[st[k]]) < area(p[st[i]], p[st[nxt(j)]], p[st[k]])) j = nxt(j);
      ans = max(ans, area(p[st[i]], p[st[j]], p[st[k]]));
    }
  return ans / 2;
}

int main()
{
  while(scanf("%d", &n) && n != -1)
    {
      for(int i = 1; i <= n; ++i) p[i].x = read(), p[i].y = read();
      if(n < 3) {puts("0.00"); continue;}
      Graham(); st[top + 1] = st[1];
      printf("%.2f
", rota());
    }
  return 0;
}









以上是关于POJ2079 Triangle的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2079 Triangle [旋转卡壳]

●POJ 2079 Triangle

[poj] 2079 Triangle || 旋转卡壳

POJ2079 Triangle

POJ2079:Triangle

模板旋转卡壳求 面积最大的三角形 poj2079