跳到正文

题解

P2831 [NOIP 2016 提高组] 愤怒的小鸟 题解

Wy_x约 10 分钟
原文
文章信息与版本

文章信息

发布
2025年6月8日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 10 分钟
获赞
1
题目
P2831

版本

  1. v1当前

这是一个以另一种方式用记忆化搜索实现的状压 Dp。

我们可以想到,先预处理出经过 ≥2\ge 2 个猪的抛物线都经过哪几头猪,放到一个 vector 里,再把抛物线相同的猪集合合并,再 DFS 选哪几条抛物线。

对于单点,直接钦定其仅经过一头猪。

显然每条抛物线经过的猪可以用一个 <218< 2^{18} 的整数存储,于是可以搜索。

最劣时间复杂度疑似 O(2171)O(2^{171})(每两个猪与原点形成的抛物线均不相同)。


考虑优化。

发现我们可以记搜,于是设 dp[i][j]dp[i][j] 为枚举到第 ii 个抛物线时,已经寄了的猪的集合为 jj。

于是复杂度骤降,最劣情况下大约会跑 2×10142 \times 10^{14} 次,尝试交一发,获得了 55 Pts。

考虑剪枝。

优化 00:在不影响精度前提下,把 double 换为 float,把 long long 换为 int 甚至 short。

优化 11:发现我们存抛物线经过的猪时也可以用一个 <218< 2^{18} 的整数存储,这样每次 dfs 可以做到 O(1)O(1) 修改。

优化 22:对于第 ii 条抛物线来说,如果这条抛物线经过的猪已经全寄掉,那么就不选这条抛物线。

优化 33:如果搜到第 ii 条抛物线时,场上没有活着的猪,那么就直接返回。

再交一发,获得了 85 Pts。

但从 CCF 官网中拿到了数据,自测(题目 U569841)发现(自测结果)最大的一个点跑了 4.68s。

成功距离我们只有亿步之遥。


优化 44:考虑贪心,把每条抛物线经过的猪按照经过猪的数目从大到小 sort 一遍,dfs 先递归选抛物线情况。

优化 55:发现多测,memset 每次清空极大的 dp 数组,时间开销不能承受,那可以记录 dfs 更改过的状态二元组,推入一个 vector 里,dfs 完遍历 vector 清空 dp 数组(4.27 s)。将上面 vector 换为数组实现,3.53 s。

貌似无法优化了,怎么办?

优化 66:考虑直接舍弃单点情况,改为在 dfs 中计算,发现枚举完所有抛物线后,jj 的二进制中零的个数就是需要额外发射的抛物线数量,额外发射的每条抛物线只经过一头猪。2.43 s

已经很接近正解了!

但似乎能剪的枝都剪完了。

(最后的优化)优化 77:考虑对优化 66 扩展。发现当过多个点的抛物线只能往当前寄掉猪的集合 jj 添加一头猪时,等价于最后额外发射一条抛物线将这头猪送上西天,所以就不选这条抛物线。1.12 s

世界的真理,我已解明!


推一下 a,ba,b 柿子:

ax22+bx2=y2a{x_2}^2+bx_2=y_2 ax2+b=y2x2a{x_2}+b=\frac{y_2}{x_2} ax1x2+bx1=y2x1x2a{x_1}{x_2}+b{x_1}=\frac{y_2x_1}{x_2}

联立

ax12+bx1=y1a{x_1}^2+bx_1=y_1

下式减上式,解得

a=y1x2−y2x1x12x2−x1x22a=\frac{y_1x_2-y_2x_1}{{x_1}^2x_2-x_1{x_2}^2} b=y2x2−ax2b=\frac{y_2}{x_2}-ax_2

那么答案显而易见了 该放代码了:

C++
#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;
}

原文