P2831 [NOIP 2016 提高组] 愤怒的小鸟 题解
这是一个以另一种方式用记忆化搜索实现的状压 Dp。
我们可以想到,先预处理出经过
对于单点,直接钦定其仅经过一头猪。
显然每条抛物线经过的猪可以用一个
最劣时间复杂度疑似
考虑优化。
发现我们可以记搜,于是设
于是复杂度骤降,最劣情况下大约会跑
考虑剪枝。
优化
优化
优化
优化
再交一发,获得了 85 Pts。
但从 CCF 官网中拿到了数据,自测(题目 U569841)发现(自测结果)最大的一个点跑了 4.68s。
成功距离我们只有亿步之遥。
优化
优化
貌似无法优化了,怎么办?
优化
已经很接近正解了!
但似乎能剪的枝都剪完了。
(最后的优化)优化
世界的真理,我已解明!
推一下
联立
下式减上式,解得
那么答案显而易见了 该放代码了:
#include <bits/stdc++.h>
using namespace std;
double x[19], y[19];
// bool vis[400][20];
int tot;
int e[400]; // 每条抛物线经过猪的集合
const int N = 120;
short dp[N][1 << 18];
// 初值为 -1
// 记忆化数组,记录状态为 [pos][nw_num] 时最少需要发射几条抛物线以让所有猪祭天
bool vis2[N][1 << 18];
// 记忆化数组,记录状态为 [pos][nw_num] 是否搜到过
int n, m;
short cou[1 << 20]; // 预处理对于每个 x∈[0,1<<20),x 的二进制中 1 的个数
int MAXN; // 所有猪都祭天时的状态
#define min(x, y) (x < y ? (x == -1 ? y : x) : (y == -1 ? x : y))
// 重载 min 函数:返回 x,y 中 >=0 的最小数,否则返回 -1
struct NNOODDEE
{
int x, y;
} vv[30000000]; // 记录每次 dfs 中更改过二元组 [x,y]
int tototo = 0; // 二元组个数
int dfs(int pos, int nw_num, int cnt)
{
if (nw_num == MAXN)
return 0; // 如果所有猪已经祭天,返回
if (pos > tot)
{
return n - cou[nw_num]; // 计算还需要多少条抛物线能使所有猪祭天
return -1;
}
if (pos <= tot && vis2[pos][nw_num])
return dp[pos][nw_num];
// 搜到过这个状态,直接返回
vis2[pos][nw_num] = 1; // 标记搜到过这个状态
vv[++tototo] = {pos, nw_num};
int ans2 = -1, ans1 = -1;
// if((e[pos]&nw_num)!=e[pos])
if (cou[e[pos] | nw_num] > cou[nw_num] + 1)
// 如果第 pos 条抛物线能使祭天的猪数目增加 >1,考虑选这条抛物线
{
ans2 = dfs(pos + 1, nw_num | e[pos], cnt + 1);
if (ans2 >= 0)
ans2++; // 如果后面有方案可以使所有猪祭天,ans2++,否则 ans2=-1
}
ans1 = dfs(pos + 1, nw_num, cnt); // 不选这条抛物线
dp[pos][nw_num] = min(ans1, ans2);
// 取重载过的 min
return dp[pos][nw_num];
}
struct Node
{
double a, b;
int x, y;
} p[400]; // 抛物线
const double eps = 1e-8;
inline bool cmp(Node x, Node y) { return (x.a < (y.a - eps)) || (abs(x.a - y.a) <= eps && x.b < y.b); }
// 将同一条抛物线下标变连续
inline int count(int x)
{ // 计算 x 的二进制中 1 的个数
int ans = 0;
while (x)
{
x &= x - 1;
ans++;
}
return ans;
}
// 贪心,先选能使猪祭天数目最多的抛物线
inline bool cmp2(int x, int y)
{
return count(x) > count(y);
}
inline void solve()
{
// memset(vis,0,sizeof(vis));
memset(e, 0, sizeof(e));
// memset(vis2,0,sizeof(vis2));
tot = 0;
// for(int i=0;i<400;i++) e[i].clear();
cin >> n >> m;
MAXN = (1 << n) - 1; // 猪都祭天时的状态
for (int i = 1; i <= n; i++)
{
cin >> x[i] >> y[i];
}
for (int i = 1; i <= n; i++)
{
double a, b;
for (int j = i + 1; j <= n; j++)
{
if (abs(x[i] - x[j]) < eps)
continue; // 判两个点的连线垂直于 x 轴情况
// 推导 a,b
a = (y[i] - y[j] * x[i] / x[j]) / (x[i] * x[i] - x[i] * x[j]);
b = (y[j] - a * x[j] * x[j]) / x[j];
if (a >= 0)
continue; // 舍弃开口向上
p[++tot] = {a, b, i, j};
}
}
sort(p + 1, p + 1 + tot, cmp);
int cnt = tot;
// cout<<"cnt="<<cnt<<"\n";
tot = 0;
for (int i = 1; i <= cnt; i++)
{
int l = i, nw = 0;
++tot;
while (i < cnt && abs(p[i + 1].a - p[i].a) <= eps && abs(p[i + 1].b - p[i].b) <= eps)
i++;
for (int j = l; j <= i; j++)
{ // 计算这条抛物线能使那些猪祭天
nw |= (1 << (p[j].x - 1));
nw |= (1 << (p[j].y - 1));
}
e[tot] = nw; // 记录这条抛物线能使那些猪祭天
}
sort(e + 1, e + 1 + tot, cmp2); // 贪心
cout << dfs(1, 0, 0) << "\n";
for (int i = 1; i <= tototo; i++) // 清空
{
vis2[vv[i].x][vv[i].y] = 0;
dp[vv[i].x][vv[i].y] = -1;
}
tototo = 0;
}
signed main()
{
// freopen("a.in","r",stdin);
memset(dp, -1, sizeof(dp)); // dp 数组初值 -1 dp 数组初值 -1 dp 数组初值 -1 dp 数组初值 -1 dp 数组初值 -1
for (int i = 0; i < (1 << 20); i++)
cou[i] = count(i);
// 计算 i 的二进制中 1 的个数
int T;
cin >> T;
while (T--)
solve();
// mt19937_64 myrand(time(0));
return 0;
}