1.题目链接。题目大意,给3n个点,把这3n个点划分成n个三角形。题目数据保证存在一组解。输出每个三角形所使用的点的下标。
#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;
}