考虑发扬人类智慧——随机化大法。
问题等价于选择一条直线并投影,然后求最长红色连续段长度。
可以 枚举直线, 排序,然后 求最长红色连续段长度。
就有了 的做法。
直接打乱原排列,再卡时限退出即可。
然后可以卡卡常,增加 AC 的风险。
Code
实测小概率可过,平均能拿 。
同样的代码交了 发能 AC 两发,挺不错的,可以复现。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct pt
{
ll x, y;
bool red;
};
const int MAXN = 1024;
alignas(64) pt pts[MAXN];
alignas(64) struct Node
{
double val;
int col;
bool operator<(const Node &o) const
{
return val < o.val;
}
} pj[MAXN];
int main()
{
cin.tie(0)->sync_with_stdio(false), cout.setf(ios::fixed), cout.precision(10);
int N;
cin >> N;
for (int i = 0; i < N; ++i)
{
char c;
cin >> pts[i].x >> pts[i].y >> c;
pts[i].red = (c == 'R');
}
int ans = 0;
srand(time(NULL));
random_shuffle(pts, pts + N);
constexpr int lim = CLOCKS_PER_SEC * 96 / 100;
for (int i = 0; i < N && clock() < lim; ++i)
{
for (int j = i + 1; j < N; ++j)
{
ll dx = pts[j].x - pts[i].x;
ll dy = pts[j].y - pts[i].y;
double nx = dy, ny = -dx;
double norm = sqrt(nx * nx + ny * ny);
nx /= norm;
ny /= norm;
for (register int k = 0; k < N; ++k)
{
pj[k].val = pts[k].x * nx + pts[k].y * ny;
pj[k].col = pts[k].red ? 1 : -1;
}
sort(pj, pj + N);
int cur = 0;
for (register int k = 0; k < N; ++k)
{
if (pj[k].col == -1)
{
if (cur > ans)
{
ans = cur;
}
cur = 0;
}
else
{
++cur;
}
}
if (cur > ans)
{
ans = cur;
}
}
}
cout << ans << '\n';
return 0;
}正解就是用线段树维护,因为高度雷同,就不放了。