BZOJ1278: 向量vector(计算几何 随机化乱搞)

题意

题目链接

Sol

讲一下我的乱搞做法。。。。

首先我们可以按极角排序。然后对(y)轴上方/下方的加起来分别求模长取个最大值。。

这样一次是(O(n))的。

我们可以对所有向量每次随机化旋转一下,然后执行上面的过程。数据好像很水然后就艹过去了。。。

#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int MAXN = 1e5 + 10;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N;
template<typename A> inline A sqr(A x) {
	return x * x;
}
struct Point {
	double x, y;
	Point operator + (const Point &rhs) const {
		return {x + rhs.x, y + rhs.y};
	}
	Point operator - (const Point &rhs) const {
		return {x - rhs.x, y - rhs.y};
	}
	double operator ^ (const Point &rhs) const {
		return x * rhs.y - y * rhs.x;
	}
	bool operator < (const Point &rhs) const {
		return atan2(y, x) < atan2(rhs.y, rhs.x);
	}
	double len() {
		return sqr(x) + sqr(y);
	}
	void rotate(double ang) {
		double l = len(), px = x, py = y;
		x = px * cos(ang) - py * sin(ang);
		y = px * sin(ang) + py * cos(ang);
	}
}p[MAXN];
double check() {
	Point n1 = {0, 0}, n2 = {0, 0}; 
	for(int i = 1; i <= N; i++) 
		if(p[i].y >= 0) n1 = n1 + p[i];
		else n2 = n2 + p[i];
	return max(n1.len(), n2.len());
}
int main() {
	N = read();
	for(int i = 1; i <= N; i++) scanf("%lf %lf", &p[i].x, &p[i].y);
	sort(p + 1, p + N + 1);
	double ans = 0;
	for(double i = 1; i <= 180; i ++) {
		ans = max(ans, check());
		for(int j = 1; j <= N; j++) p[j].rotate(1);
	}
	LL gg = ans;
	ans = gg;
	printf("%.3lf", ans);
	return 0;
}