码迷,mamicode.com
首页 > 其他好文 > 详细

POJ2079 Triangle

时间:2018-11-22 20:57:57      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:ace   include   mes   putchar   point   operator   凸包   print   \n   

嘟嘟嘟


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


刚开始不知咋的忘了三角形三条边可能都不在凸包上,然后快速的打了个旋转卡壳结果\(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\n", rota());
    }
  return 0;
}

POJ2079 Triangle

标签:ace   include   mes   putchar   point   operator   凸包   print   \n   

原文地址:https://www.cnblogs.com/mrclr/p/10003270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!