【洛谷 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\n", ans);
	return 0;
}
posted @ 2019-01-24 16:52  Qihoo360  阅读(162)  评论(0编辑  收藏  举报
You're powerful!