凸包P2742

#include <iostream>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
#include <chrono>
#include <ctime>
#include <cmath>
#include <cctype>
#include <string>
#include <cstdio>
#include <iomanip>


#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <iterator>
using namespace std;

#define _CRT_SECURE_NO_WARNINGS

//void rfIO()
//{
//	FILE *stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

inline int read(int& x) {
	char ch = getchar();
	int f = 1; x = 0;
	while (ch > '9' || ch < '0') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	return x * f;
}
//void ReadFile() {
//	FILE* stream1;
//	freopen_s(&stream1,"in.txt", "r", stdin);
//	freopen_s(&stream1,"out.txt", "w", stdout);
//}

static auto speedup = []() {ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return nullptr; }();

const int maxn = 1e5 + 7;

struct Point{
	double x, y;
	Point(){ x = y = 0; }
	Point(double _x, double _y) :x(_x), y(_y){}
	void input(){
		cin >> x >> y;
	}
	//点积 区分上下 cosx
	double dot(const Point &a) {
		return x + a.x + y + a.y;
	}
	//叉积 区分左右 sinx
	double cross(const Point &a){
		return x * a.y - y * a.x;
	}
	//距离
	double dis(const Point &a){
		return sqrt((x - a.x) * (x - a.x) + (y - a.y) * (y - a.y));
	}
	//距离的平方
	double dis2(const Point &a){
		return (x - a.x) * (x - a.x) + (y - a.y) * (y - a.y);
	}
}p[maxn],s[maxn];

double cross(const Point &a, const Point & b, const Point &c, const Point &d){
	Point t(a.x - b.x, a.y - b.y),t2(c.x - d.x,c.y - d.y);
	return t.cross(t2);
}

bool cmp(const Point &a, const Point &b){
	double c1 = cross(p[1], a, p[1], b);
	if (c1 > 0) return true;
	else if (c1 == 0)return p[1].dis2(a) < p[1].dis2(b);
	return false;
}

int main()
{
	int n,top = 1,idx = 1;
	cin >> n;
	
	for (int i = 1; i <= n; i++){
		p[i].input();
		if (p[idx].y > p[i].y) idx = i;
		else if (p[idx].y == p[i].y && p[idx].x > p[i].x)idx = i;
	}
	swap(p[1], p[idx]);
	sort(p + 2, p + n + 1, cmp);
	s[1] = p[1];
	for (int i = 2; i <= n; i++){
		while (top > 1 && cross(s[top - 1],s[top],s[top],p[i]) <= 0){
			top--;
		}
		s[++top] = p[i];
	}
	s[++top] = p[1];
	double ans = 0;
	for (int i = 1; i < top; i++){
		ans += s[i].dis(s[i + 1]);
	}
	cout << fixed << setprecision(2) << ans << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值