POJ 2178 (凸包 旋转卡壳)
2015-11-11 10:01
169 查看
题意是求平面最远点对距离的平方。
旋转卡壳的板子题。
旋转卡壳的板子题。
#include <cstring> #include <cmath> #include <iostream> #include <algorithm> #include <cstdio> using namespace std; typedef unsigned long long ll; #define maxn 51111 #define pi acos (-1) #define rotate Rotate const double eps = 1e-8; int dcmp (double x) { if (fabs (x) < eps) return 0; else return x < 0 ? -1 : 1; } struct point { double x, y; point (double _x = 0, double _y = 0) : x(_x), y(_y) {} point operator - (point a) const { return point (x-a.x, y-a.y); } point operator + (point a) const { return point (x+a.x, y+a.y); } bool operator < (const point &a) const { return x < a.x || (x == a.x && y < a.y); } bool operator == (const point &a) const { return dcmp (x-a.x) == 0 && dcmp (y-a.y) == 0; } }; point operator * (point a, double p) { return point (a.x*p, a.y*p); } double cross (point a, point b) { return a.x*b.y-a.y*b.x; } double dot (point a, point b) { return a.x*b.x + a.y*b.y; } int n, m; point p[maxn], ch[maxn]; int ConvexHull () { sort (p, p+n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n-2; i >= 0; i--) { while (m > k && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } double dis (point a, point b) { double xx = a.x-b.x, yy = a.y-b.y; return xx*xx+yy*yy; } double rotate_calipers (point *p) { double ans = 0; int cur = 1; for (int i = 0; i < m; i++) { while (cross (p[i]-p[(i+1)%m], p[(cur+1)%m]-p[cur]) < 0) cur = (cur+1)%m; ans = max (ans, max (dis (p[i], p[cur]), dis (p[(i+1)%m], p[(cur+1)%m]))); } return ans; } int main () { //freopen ("in", "r", stdin); ios::sync_with_stdio(0); while (scanf ("%d", &n) == 1) { for (int i = 0; i < n; i++) { scanf ("%lf%lf", &p[i].x, &p[i].y); } m = ConvexHull (); printf ("%lld\n", (long long) rotate_calipers (ch)); } return 0; }
相关文章推荐
- js clearInterval()方法的定义和用法
- android获取网络数据
- swift 快速奔跑的兔几 本节的内容是:SceneKit 第一说 创建一个3D胶囊
- 解决ajax跨域访问的问题
- 定时任务在服务器集群中的实现问题
- pAdTy_-4 与其它应用程序的交互
- sql server 建表常用语句 (包括主键,外键 等)
- 八皇后问题
- LeetCode 046 Permutations
- ASP.NET easyUI--datagrid 通过ajax请求ASP.NET后台数据的分页查询
- MySQL存储过程中的3种循环
- EF学习小记
- [Atom] Cannot install package behind a proxy on Win7
- java 重载和重写的区别
- 射频识别技术漫谈(16)——Mifare UltraLight
- 获得 1000 个赞的程序员高薪职业建议
- TableViewCell分割线为一个像素点
- WebRTC中peerconnectclient&server 简析
- 图片上下左右居中
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十五)