【HDU6300】极角排序

1.题目链接。题目大意,给3n个点,把这3n个点划分成n个三角形。题目数据保证存在一组解。输出每个三角形所使用的点的下标。

2.找到左下角(其他地方也行)的一个点,然后以这个点为极角排序,极角排序之后相邻的三个点划分成一组即可。

#include <bits/stdc++.h>
#define maxn 4005
#pragma warning(disable:4996)
typedef long long ll;
using namespace std;
const double eps = 1e-8;
struct point {
	double x, y;
	int index;
	point() {}
	point(int _x, int _y) {
		x = _x, y = _y;
	}
};
point p[maxn];
int pos;
double cross(double x1, double y1, double x2, double y2)
{
	return (x1*y2 - x2 * y1);
}

double compare(point a, point b, point c)//计算极角
{
	return cross((b.x - a.x), (b.y - a.y), (c.x - a.x), (c.y - a.y));
}
bool cmp(point a, point b)
{
	if (compare(p[pos], a, b) == 0)//计算叉积,函数在上面有介绍,如果叉积相等,按照X从小到大排序
		return a.x < b.x;
	else return compare(p[pos], a, b)> 0;
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t--) 
	{
		pos = 0;
		int n;
		scanf("%d", &n);
		for (int i = 0; i < 3 * n; i++) 
		{
			scanf("%lf%lf", &p[i].x, &p[i].y);
			p[i].index = i + 1;
			if (p[0].y > p[i].y || (p[i].y == p[0].y&&p[0].x > p[i].y)) 
			{
				swap(p[i], p[0]);
			}
		}
		sort(p + 1, p + 3 * n, cmp);
		int cnt = 1;
		for (int i = 0; i < 3 * n; i++) 
		{
			if (cnt == 1) printf("%d", p[i].index);
			else printf(" %d", p[i].index);
			cnt++;
			if (cnt == 4) 
			{
				cout << endl;
				cnt = 1;
			}
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值