• 【洛谷 P2742】【模板】二维凸包


    题目链接
    二维凸包板子。。有时间会补总结的。

    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int MAXN = 10010;
    struct point{
    	double x, y;
    }p[MAXN];
    int cmp1(const point a, const point b){
    	return a.x == b.x ? a.y < b.y : a.x < b.x;
    }
    double k(point a, point b){
    	return a.x == b.x ? 1e18 : (b.y - a.y) / (b.x - a.x);
    }
    double dis(point a, point b){
    	double px = b.x - a.x, py = b.y - a.y;
    	return sqrt(px * px + py * py);
    }
    int n, top, st[MAXN];
    double ans;
    int main(){
    	scanf("%d", &n);
    	for(int i = 1; i <= n; ++i)
    	   scanf("%lf%lf", &p[i].x, &p[i].y);
    	sort(p + 1, p + n + 1, cmp1);
    	for(int i = 1; i <= n; ++i){
    	   while((top > 1 && k(p[st[top - 1]], p[i]) < k(p[st[top - 1]], p[st[top]]))) --top;
    	   st[++top] = i;
        }
        for(int i = 2; i <= top; ++i)
           ans += dis(p[st[i]], p[st[i - 1]]);
        top = 0;
    	for(int i = 1; i <= n; ++i){
    	   while((top > 1 && k(p[st[top - 1]], p[i]) > k(p[st[top - 1]], p[st[top]]))) --top;
    	   st[++top] = i;
        }
        for(int i = 2; i <= top; ++i)
           ans += dis(p[st[i]], p[st[i - 1]]);
        printf("%.2lf
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    js对象的所有方法
    js数组的所有方法
    Scss语法
    new一个对象的过程
    promises的深入学习
    jsonp的原理介绍及Promise封装
    Vue页面缓存和不缓存的方法
    JavaScript数据类型
    JS常用函数原理的实现
    @Autowired注解在抽象类中实效的原因分析
  • 原文地址:https://www.cnblogs.com/Qihoo360/p/10315431.html
Copyright © 2020-2023  润新知