Graham算法求凸包问题
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = 1001;
struct node {
node () {};
node (int _x, int _y): x(_x), y(_y) {};
int x, y;
node operator -(node nd) {
return node(x - nd.x, y - nd.y);
}
int operator *(node nd) {
return x * nd.y - y * nd.x;
}
} p[maxn], q[maxn];
int per[maxn], n, l;
inline int dis(node p1, node p2) {
return (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
}
inline bool cmp(int v1, int v2) {
int det = (p[v1] - p[1]) * (p[v2] - p[1]);
if (det != 0)return det > 0;
return dis(p[1], p[v1]) < dis(p[1], p[v2]);
}
void Graham() {
int i, id, k;
id = 1;
for (i = 2; i <= n; i++) {
if (p[i].x < p[id].x || (p[i].x == p[id].x && p[i].y < p[id].y))id = i;
}
if (id != 1)swap(p[1], p[id]);
for (i = 1; i <= n; i++) {
per[i] = i;
}
sort(per + 2, per + n + 1, cmp);
l = 0;
q[++l] = p[1];
for (i = 2; i <= n; i++) {
k = per[i];
while (l >= 2 && (q[l] - q[l - 1]) * (p[k] - q[l - 1]) <= 0)l--;
q[++l] = p[k];
}
}
bool judge() {
if (l == 2)return 0;
int sum;
for (int i = 1; i <= l; i++) {
sum = 0;
for (int j = 1; j <= n; j++) {
if ((p[j] - q[i]) * (q[i % l + 1] - p[j]) == 0)sum++;
}
if (sum < 3)return 0;
}
return 1;
}
int main() {
int t, i;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &p[i].x, &p[i].y);
}
Graham();
if (judge())puts("YES");
else puts("NO");
}
}