92 WA#1
查看原帖
92 WA#1
326254
LonginusMonkey楼主2024/7/9 13:01

找不到问题,拿那个凸包模板做的 凸包模板是100pt

// nlogn graham算法 
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
	double x, y; 
};
const int maxn = 1e5+100;
node point[maxn];
double check(node a1, node a2, node b1, node b2) {
	return (a2.x - a1.x) * (b2.y - b1.y) - (a2.y - a1.y) * (b2.x - b1.x);
}
double len(node a, node b) {
	return sqrt((a.x - b.x) * (a.x - b.x)+(a.y - b.y) * (a.y - b.y));
}
bool cmp(node a, node b) {
	double tmp = check(point[1], a, point[1], b);
	if(tmp > 0) {
		return 1; // 选a 
	} else if(tmp == 0 and len(point[1], a) > len(point[1], b)) {
		return 1;
	} else {
		return 0;
	}
}

node vec[maxn];

signed main() {
	int n; cin >> n;
	int idx = 1;
	for(int i=1; i<=n; ++i) {
		cin >> point[i].x >> point[i].y;
		if(i > 1 and (point[i].x < point[idx].x or point[i].x == point[idx].x and point[i].y < point[idx].y)) {
			idx = i;
		}
	}
	swap(point[1], point[idx]);
	sort(point+2, point+1+n, cmp);
	vec[0] = (point[1]);
	int sz = 1;
	for(int i=2; i<=n; ++i) {
		if(sz <= 1) {
			vec[sz++] = point[i];
		}
		while(sz > 1 and (check(vec[sz-2], vec[sz-1], vec[sz-2], point[i]) < 0
		or check(vec[sz-2], vec[sz-1], vec[sz-2], point[i]) == 0 and len(vec[sz-2], vec[sz-1]) <= len(vec[sz-2], point[i]))) {
			sz --;
		}
		vec[sz++] = point[i];
	}
	//先默认前面写的是对的好了,代码是奶牛那题
	int j=0; // j表示最远的那个数
	int ans = 0;
	for(int i=0; i<sz; ++i) {
		int nxt = (j+1)%sz;
		while((vec[i].x - vec[j].x) * (vec[i].x - vec[j].x)+(vec[i].y - vec[j].y) * (vec[i].y - vec[j].y) < 
		(vec[i].x - vec[nxt].x) * (vec[i].x - vec[nxt].x)+(vec[i].y - vec[nxt].y) * (vec[i].y - vec[nxt].y)){
			j = nxt;
			nxt = (j+1)%sz;
		}
	 	ans = max(ans, (int)((vec[i].x - vec[j].x) * (vec[i].x - vec[j].x)+(vec[i].y - vec[j].y) * (vec[i].y - vec[j].y)));
	} 
	printf("%lld", ans);
	return 0;
}
2024/7/9 13:01
加载中...