ZOJ-1465

 仍然是凸包,不过这题数据很强,发现我第一次写的那个凸包是有问题,只是那题数据比较弱没有WA,关键在于极角排序的时候,叉积相等时需要再比较两点和原点的距离,距原点较近的排前面,这样能保证距离原点较远的那个点成为凸包的点。。这也是看了别人的文章才发现的,不然自己调的估计DEBUG死也不知道原因。。

#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

namespace
{
	pair<int, int> p;

	inline int cross(const pair<int, int> &p1, const pair<int, int> &p2)
	{
		return p1.first * p2.second - p1.second * p2.first;
	}

	inline pair<int, int> vect(const pair<int, int> &p1,
			const pair<int, int> &p2)
	{
		return make_pair(p2.first - p1.first, p2.second - p1.second);
	}

	bool cmp(const pair<int, int> &p1, const pair<int, int> &p2)
	{
		int cp = cross(vect(p, p2), vect(p, p1));
		if (cp == 0)
			return (p1.first - p.first) * (p1.first - p.first)
					+ (p1.second - p.second) * (p1.second - p.second)
					< (p2.first - p.first) * (p2.first - p.first)
							+ (p2.second - p.second) * (p2.second - p.second);
		return cp < 0;
	}

	bool point_cmp(const pair<int, int> &p1, const pair<int, int> &p2)
	{
		return p1.second != p2.second ?
				p1.second < p2.second : p1.first < p2.first;
	}

	double dist(const pair<int, int> &p1, const pair<int, int> &p2)
	{
		return sqrt(
				(p1.first - p2.first) * (p1.first - p2.first)
						+ (p1.second - p2.second) * (p1.second - p2.second));
	}
}

int main()
{
	int T, N, L, x, y;
	vector<pair<int, int> > V;
	vector<pair<int, int> > Q;
	scanf("%d", &T);
	for (int t = 0; t < T; t++)
	{
		if (t)
			puts("");
		scanf("%d %d", &N, &L);
		V.clear();
		Q.clear();
		while (N--)
		{
			scanf("%d %d", &x, &y);
			V.push_back(make_pair(x, y));
		}
		vector<pair<int, int> >::iterator it = min_element(V.begin(), V.end(),
				point_cmp);
		p = *it;
		V.erase(it);
		sort(V.begin(), V.end(), cmp);

		Q.push_back(p);
		Q.push_back(V[0]);
		Q.push_back(V[1]);
		for (size_t i = 2; i < V.size(); i++)
		{
			while (Q.size() > 1
					&& cross(vect(Q[Q.size() - 2], Q.back()),
							vect(Q[Q.size() - 2], V[i])) <= 0)
				Q.pop_back();
			Q.push_back(V[i]);
		}

		double sum = dist(Q.front(), Q.back());
		for (size_t i = 1; i < Q.size(); i++)
			sum += dist(Q[i - 1], Q[i]);

		sum += 2 * M_PI * L;
		printf("%d\n", int(sum + 0.5));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值