题解-luogu-p2831愤怒的小鸟

题目链接

在平面直角坐标系中给定$n$个位于第一象限的点,求至少需要从原点引出多少条开口向下的抛物线,使它们经过所有的点。

$ 1 \le n \le 18 $

感谢@oy的贡献

一道并不是特别难的状压dp。

状态定义:$f[s]$表示将所有剩余的猪消灭至少需要多少只小鸟,其中$s$表示已经消灭的猪的集合。

考虑状态转移,发射一个小鸟可以消灭尚未被消灭的猪。记$curve[i][j]$为一个二进制集合,表示经过第$i$、$j$号猪的抛物线能消灭哪些猪。

即:
$$
f[s]=min\lbrace f[s|curve[i][j]]+1\rbrace
$$
可以用记忆化搜索实现,注意精度控制和触摸状态。

代码如下:

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
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;

int T;
int N, M;

double x[19], y[19];
int curve[19][19];
int f[524289];

inline void equals(double& a, double& b, double x1, double y1, double x2, double y2) {
a = (y1 * x2 - x1 * y2) / (x1 * x1 * x2 - x2 * x2 * x1);
b = (y1 - x1 * x1 * a) / x1;
}

inline int dp(int s) {
if (f[s] != -1) return f[s];
if (s == (1 << N) - 1) return f[s] = 0;
f[s] = INF;
for (register int i = 1; i <= N; ++i) {
for (register int j = i; j <= N; ++j) {
f[s] = min(f[s], dp(s | curve[i][j]) + 1);
}
}
return f[s];
}

int main() {
scanf("%d", &T);
while (T--) {
memset(curve, 0, sizeof(curve));
scanf("%d%d", &N, &M);
for (register int i = 1; i <= N; ++i)
scanf("%lf%lf", &x[i], &y[i]);
for (register int p1 = 1; p1 <= N; ++p1) {
for (register int p2 = p1 + 1; p2 <= N; ++p2) {
double a, b;
equals(a, b, x[p1], y[p1], x[p2], y[p2]);
if (a >= 0) continue;
for (register int i = 1; i <= N; ++i) {
if (x[i] * x[i] * a + x[i] * b >= y[i] - EPS && x[i] * x[i] * a + x[i] * b <= y[i] + EPS) {
curve[p1][p2] |= (1 << (i - 1));
curve[p2][p1] |= (1 << (i - 1));
}
}
}
}
for (register int p = 1; p <= N; ++p)
curve[p][p] = (1 << (p - 1));
memset(f, -1, sizeof(f));
printf("%d\n", dp(0));
}
return 0;
}

Comments

Your browser is out-of-date!

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

×