洛谷4385 - [COCI2009]Dvapravca

今天模拟赛的第三题(又达成了原题考试的成就)。考场的时候由于 T2 调太久这题暴力都没写… 现在依然不会正解于是写了个随机化算法 AC 。

每次随机两个点,然后计算出他们的连线的斜率,每次 $O(n)$ 判断一下选取与这条直线平行的两条直接的答案,随机个次几万次w就过了…

这里可能有个细节问题,也就是说我们每次选出的两个点中可能有不合法的点,实际上影响不大,因为我们可以通过略倾斜直线来取到另一个合法的点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// =================================
// author: memset0
// date: 2019.01.07 16:15:49
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 1e3 + 10;
const double inf = 1e30;
int n, ans, now;

struct node {
int x, y;
inline bool operator == (const node &other) const {
return x == other.x && y == other.y;
}
};
std::vector <node> a, b, c;

int solve() {
int ans = 0, __1 = rand() % c.size(), __2 = rand() % c.size();
node _1 = c[__1], _2 = c[__2];
if (_1 == _2 || (__1 > a.size() && __2 > a.size())) { rand(); return 0; }
if (_1.x == _2.x) {
int min = -1e9 - 10, max = 1e9 + 10;
for (auto u : b) u.x < _1.x ? min = std::max(min, u.x) : max = std::min(max, u.x);
for (auto u : a) if (min <= u.x && u.x <= max) ++ans;
rand();
} else {
double k = (_1.y - _2.y) / (double)(_1.x - _2.x);
double _b = ((ll)_1.y * _2.x - (ll)_2.y * _1.x) / (double)(_2.x - _1.x);
double min = -inf, max = inf;
for (auto u : b) {
double b = u.y - k * u.x;
b < _b ? min = std::max(min, b) : max = std::min(max, b);
}
for (auto u : a) {
double b = u.y - k * u.x;
if (min <= b && b <= max) ++ans;
}
}
return ans;
}

void main() {
srand(20040725);
read(n);
for (int i = 1, x, y, c; i <= n; i++) {
read(x), read(y), c = getchar();
if (c == 'R') a.push_back((node){x, y});
else b.push_back((node){x, y});
}
for (auto u : a) c.push_back(u);
for (auto u : b) c.push_back(u);
int limit = 130000000 / n;
while (limit--) ans = std::max(ans, solve());
print(ans, '\n');
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×