板题hdu1348Wall

平面凸包问题是计算几何中的一个经典问题

具体就是给出平面上的多个点,求一个最小的凸多边形,使得其包含所有的点

具体形象就类似平面上有若干柱子,一个人用绳子从外围将其紧紧缠绕一圈

Graham算法##

直接讲算法

我们将所有点排序,分别求出上凸壳和下凸壳,合起来就是凸包

以上凸壳为例子,我们先将最左边的点加入凸包【可以想象,最左侧的点一定在凸包上】

之后向后查找:

1、若当前凸包内只有一点,那么加入新的点

2、如果当前凸包内不止一个点,检验新加入的点与凸包最后一个点所在直线与当前凸包最后两点坐在直线的关系

如果是这样:



满足上凸性,加入凸包

但是如果是这样:



不满足上凸性,就不加入

具体判断可以用斜率也可以用叉乘【我写斜率狂WA,还是叉乘比较滋滋】

扫过一遍,就可以得到上凸壳,下凸壳类似

复杂度\(O(nlogn)\)

hdu1348

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
using namespace std;
const int maxn = 2005,maxm = 100005;
const double INF = 1000000000000000000ll;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 3) + (out << 1) + c - '0'; c = getchar();}
return out * flag;
}
const double pi = acos(-1);
struct point{double x,y;}p[maxn];
inline bool operator <(const point& a,const point& b){
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
inline point operator -(const point& a,const point& b){
return (point){a.x - b.x,a.y - b.y};
}
inline double operator *(const point& a,const point& b){
return a.x * b.y - a.y * b.x;
}
inline double dis(int u,int v){
return sqrt((p[u].x - p[v].x) * (p[u].x - p[v].x) + (p[u].y - p[v].y) * (p[u].y - p[v].y));
}
int T,n,L,q[maxn],tail;
void cal(){
sort(p + 1,p + 1 + n);
q[tail = 1] = 1;
for (int i = 2; i <= n; i++){
while (tail > 1 && (p[i] - p[q[tail]]) * (p[q[tail]] - p[q[tail - 1]]) < 0) tail--;
q[++tail] = i;
}
int last = tail;
for (int i = n - 1; i; i--){
while (tail > last && (p[i] - p[q[tail]]) * (p[q[tail]] - p[q[tail - 1]]) < 0) tail--;
q[++tail] = i;
}
}
void print(){
double ans = 0;
for (int i = 1; i < tail; i++) ans += dis(q[i],q[i + 1]);
ans += 2.0 * pi * L;
if (T) printf("%.0lf\n\n",ans);
else printf("%.0lf\n",ans);
}
int main(){
T = read();
while (T--){
n = read(); L = read();
for (int i = 1; i <= n; i++) p[i].x = read(),p[i].y = read();
cal();
print();
}
return 0;
}

最新文章

  1. Qt5 开发 iOS 应用之访问 SQLite 数据库
  2. ipad
  3. winsock error 相关
  4. Scala的函数,高阶函数,隐式转换
  5. 论文笔记之:Dueling Network Architectures for Deep Reinforcement Learning
  6. Maven pom.xml中添加指定的中央仓库
  7. 6.HotSpot垃圾收集器
  8. ARM寻址方式
  9. sql server 2008r2 清除数据库日志
  10. Working with MTD Devices
  11. hdu 5113 Black And White
  12. 前端工具 - 15个最佳的 JavaScript 表单验证库
  13. InputStream类详解
  14. BZOJ 2733 永无乡
  15. 《SpringMVC从入门到放肆》十三、SpringMVC数据校验
  16. ubutun 下配置php和postgresql
  17. python常用模块之os模块
  18. Linux makefile讲解
  19. make clean、make mrproper、make distclean的区别【转】
  20. 迷你MVVM框架 avalonjs 学习教程19、avalon历史回顾

热门文章

  1. 数据库要素 ER
  2. 2018.5.6 解决问题:oracle------ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务
  3. matlab启动
  4. 前端应该如何去认识http
  5. 【上下界网络流 费用流】bzoj2055: 80人环游世界
  6. Python的ORM介绍
  7. 第一本C语言笔记(下)
  8. How to Install Zabbix Server on Centos6.7
  9. python-类与继承
  10. 通过代码链接ftp上传下载删除文件