P1915 [NOI2010] 成长快乐

此题为世纪难题

题目提供者 洛谷

难度 NOI/NOI+/CTSC

输入输出样例

输入 #1

5 1 6 0 0
1
5 2 2 0 0

 输出 #1

1
5
5 2 2 1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~此题非常难,小白就不用想着独自完成了

题解:

 

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <complex>
#include <ctime>
#include <cmath>
 
using namespace std;
 
const int maxN = 10010;
const double zero = 1e-12, INF = 1e198;
struct Res
{
	double t; complex <double> p; int ord; Res() {}
	Res(double t, complex <double> p, int ord):
		t(t), p(p), ord(ord) {}
} res[maxN], _res[maxN]; bool eaten[maxN];
complex <double> p[maxN], v[maxN];
double w[maxN], V, _deltaw, deltaw, T, tem;
vector <pair <double, int> > ls;
vector <pair <double, int> >::iterator iter;
int ord[maxN], n, cnt, _cnt;
 
inline bool cmp(const int &a, const int &b)
{return w[a] < w[b];}
 
inline double sqr(double x) {return x * x;}
 
inline double sqr(complex <double> z)
{return sqr(real(z)) + sqr(imag(z));}
 
inline double solve(complex <double> p,
					complex <double> v,
					complex <double> O)
{
	double a = sqr(v) - sqr(V),
	b = -2 * (real(v) * real(O - p) + imag(v) * imag(O - p)),
	c = sqr(p - O),
	delta = sqr(b) - 4 * a * c;
	if (delta < -zero) return INF;
	if (fabs(a) < zero)
		return (b > -zero) ? INF : (-c / b);
	double ans = INF,
		   x1 = (-b - sqrt(delta)) / (2 * a),
		   x2 = (-b + sqrt(delta)) / (2 * a);
	if (x1 > -zero) ans = x1;
	if (x2 > -zero && x2 < ans) ans = x2;
	return ans;
} //计算出从O出发,追赶一个当前位置为p,以速度v移动的小虾所需时间。
 
inline double _rand() {return (double)rand() / RAND_MAX;}
 
inline bool judge()
{
	if (++iter == ls.end()) {--iter; return 0;}
	if (rand() & 1) {--iter; return 0;}
	//先以0.5的概率初步淘汰较差的解。
	double nxt = iter -> first;
	--iter; double ths = iter -> first;
	double delta = (ths - nxt);
	if (delta / ths < 3.40e-2) return 1;
	//若较差的解比当前解差3.4%以上,直接淘汰。
	return _rand() < 1 / exp(delta / tem);
	//否则以概率e ^ (-delta / tem)的概率接受更差的解。
}
 
int main()
{
	freopen("nemo10.in", "r", stdin);
	freopen("nemo10.out", "w", stdout);
	srand(time(NULL));
	scanf("%lf%lf%lf%lf%lf%d", w + 0, &V, &T, &p[0].real(), &p[0].imag(), &n);
	complex <double> p0 = p[0]; double w0 = w[0];
	for (int i = 1; i < n + 1; ord[i] = i, ++i)
		scanf("%lf%lf%lf%lf%lf", w + i, &p[i].real(), &p[i].imag(), &v[i].real(), &v[i].imag());
	sort(ord + 1, ord + n + 1, cmp); //将小虾按体重从小到大的顺序排序。
	tem = 1; //初始温度设为1。
	for (int k = 0; k < 15; ++k)
	{
		memset(eaten, 0, sizeof eaten);
		_deltaw = 0; _cnt = 0; p[0] = p0; w[0] = w0;
		for (double t = 0; t < T;)
		{
			ls.clear();
			for (int i = 1; w[ord[i]] < w[0]; ++i)
			if (!eaten[ord[i]])
				ls.push_back(make_pair(w[ord[i]] / sqr(solve(p[ord[i]] + t * v[ord[i]], v[ord[i]], p[0])), ord[i]));
			sort(ls.begin(), ls.end(), greater <pair <double, int> > ());
			iter = ls.begin(); if (iter == ls.end()) break;
			while (judge()) ++iter;
			int pos = iter -> second;
			if (T - (t += solve(p[pos] + t * v[pos], v[pos], p[0])) < -zero) break;
			_deltaw += w[pos], w[0] += w[pos]; eaten[pos] = 1;
			_res[_cnt++] = Res(t, p[0] = p[pos] + t * v[pos], pos);
		}
		if (_deltaw > deltaw)
		{
			deltaw = _deltaw;
			for (cnt = 0; cnt < _cnt; ++cnt) res[cnt] = _res[cnt];
		} //更新最优解。
		tem *= .5;
	}
	printf("%d\n%.9lf\n", cnt, deltaw);
	for (int i = 0; i < cnt; ++i)
		printf("%.9lf %.9lf %.9lf %d\n", res[i].t, res[i].p.real(), res[i].p.imag(), res[i].ord);
	return 0;
}

 

对于不同的数据,还可以用特殊方法求解。

第一组数据显然手算,不多说。

第二组数据较小,可以暴力枚举,附程序:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <complex>
 
using namespace std;
 
const int maxN = 10010;
const double zero = 1e-12, INF = 1e198;
struct Res
{
	double t; complex <double> p; int ord; Res() {}
	Res(double t, complex <double> p, int ord):
		t(t), p(p), ord(ord) {}
} res[maxN], tmp[maxN]; bool eaten[maxN];
complex <double> p[maxN], v[maxN];
double w[maxN], V, deltaw, T;
int n, cnt;
 
inline double sqr(double x) {return x * x;}
 
inline double sqr(complex <double> z)
{return sqr(real(z)) + sqr(imag(z));}
 
inline double solve(complex <double> p,
					complex <double> v,
					complex <double> O)
{
	double a = sqr(v) - sqr(V),
	b = -2 * (real(v) * real(O - p) + imag(v) * imag(O - p)),
	c = sqr(p - O),
	delta = sqr(b) - 4 * a * c;
	if (delta < -zero) return INF;
	if (fabs(a) < zero)
		return (b > -zero) ? INF : (-c / b);
	double ans = INF,
		   x1 = (-b - sqrt(delta)) / (2 * a),
		   x2 = (-b + sqrt(delta)) / (2 * a);
	if (x1 > -zero) ans = x1;
	if (x2 > -zero && x2 < ans) ans = x2;
	return ans;
}
 
void Dfs(int i, double w0, double t, complex <double> p0)
{
	for (int j = 1; j < n + 1; ++j)
	if (!eaten[j] && w[j] < w0)
	{
		double ths = solve(p[j] + t * v[j], v[j], p0);
		if (t + ths - T > zero) continue;
		complex <double> pos = p[j] + (t + ths) * v[j];
		eaten[j] = 1;
		tmp[i] = Res(t + ths, pos, j);
		Dfs(i + 1, w0 + w[j], t + ths, pos);
		tmp[i] = Res(0, 0, 0);
		eaten[j] = 0;
	}
	if (w0 - w[0] > deltaw)
	{
		deltaw = w0 - w[0];
		for (cnt = 0; tmp[cnt].t > zero; ++cnt)
			res[cnt] = tmp[cnt];
	}
	return;
}
int main()
{
	freopen("nemo2.in", "r", stdin);
	freopen("nemo2.out", "w", stdout);
	scanf("%lf%lf%lf%lf%lf%d", w + 0, &V, &T, &p[0].real(), &p[0].imag(), &n);
	for (int i = 1; i < n + 1; ++i)
		scanf("%lf%lf%lf%lf%lf", w + i, &p[i].real(), &p[i].imag(), &v[i].real(), &v[i].imag());
	Dfs(0, w[0], 0, p[0]);
	printf("%d\n%.9lf\n", cnt, deltaw);
	for (int i = 0; i < cnt; ++i)
		printf("%.9lf %.9lf %.9lf %d\n", res[i].t, res[i].p.real(), res[i].p.imag(), res[i].ord);
	return 0;
}

第三组数据小虾的体重有规律,按顺序吃就是了,附程序:

​
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <complex>
 
using namespace std;
 
const int maxN = 10010;
const double zero = 1e-12, INF = 1e198;
struct Res
{
	double t; complex <double> p; int ord; Res() {}
	Res(double t, complex <double> p, int ord):
		t(t), p(p), ord(ord) {}
} res[maxN]; bool eaten[maxN];
complex <double> p[maxN], v[maxN];
double w[maxN], V, deltaw, T;
int ord[maxN], n, cnt;
 
inline bool cmp(const int &a, const int &b)
{return w[a] < w[b];}
 
inline double sqr(double x) {return x * x;}
 
inline double sqr(complex <double> z)
{return sqr(real(z)) + sqr(imag(z));}
 
inline double solve(complex <double> p,
					complex <double> v,
					complex <double> O)
{
	double a = sqr(v) - sqr(V),
	b = -2 * (real(v) * real(O - p) + imag(v) * imag(O - p)),
	c = sqr(p - O),
	delta = sqr(b) - 4 * a * c;
	if (delta < -zero) return INF;
	if (fabs(a) < zero)
		return (b > -zero) ? INF : (-c / b);
	double ans = INF,
		   x1 = (-b - sqrt(delta)) / (2 * a),
		   x2 = (-b + sqrt(delta)) / (2 * a);
	if (x1 > -zero) ans = x1;
	if (x2 > -zero && x2 < ans) ans = x2;
	return ans;
}
 
int main()
{
	freopen("nemo3.in", "r", stdin);
	freopen("nemo3.out", "w", stdout);
	scanf("%lf%lf%lf%lf%lf%d", w + 0, &V, &T, &p[0].real(), &p[0].imag(), &n);
	for (int i = 1; i < n + 1; ord[i] = i, ++i)
		scanf("%lf%lf%lf%lf%lf", w + i, &p[i].real(), &p[i].imag(), &v[i].real(), &v[i].imag());
	sort(ord + 1, ord + n + 1, cmp);
	for (double t = 0; T - t > zero;)
	{
		static int i = 0; ++i; if (i > n) break;
		double Min = solve(p[ord[i]] + t * v[ord[i]], v[ord[i]], p[0]);
		int pos = ord[i];
		if (fabs(Min - INF) < zero) break;
		if (T - (t += Min) < -zero) break;
		w[0] += w[pos]; deltaw += w[pos]; eaten[pos] = 1;
		res[cnt++] = Res(t, p[0] = p[pos] + t * v[pos], pos);
	}
	printf("%d\n%.9lf\n", cnt, deltaw);
	for (int i = 0; i < cnt; ++i)
		printf("%.9lf %.9lf %.9lf %d\n", res[i].t, res[i].p.real(), res[i].p.imag(), res[i].ord);
	return 0;
}

​

第四组数据中所有小虾静止不动且在一条直线上,据说是区间动态规划,方法至今未知。

第五、六组数据中所有小虾都在高速向下掉,而Nemo的移动速度很小,可以近似看作Nemo在只能水平移动的情况下去接住小虾来吃。

先将小虾按竖直方向排序,然后进行一下操作。

设f[i]表示前i个小虾中恰好第i个被吃所能得到的最大收益,只需要一个n^2的动态规划即可求解。

附程序:

​
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <complex>
 
using namespace std;
 
const int maxN = 10010;
const double zero = 1e-12, INF = 1e198;
struct Res
{
	double t; complex <double> p; int ord; Res() {}
	Res(double t, complex <double> p, int ord):
		t(t), p(p), ord(ord) {}
} res[maxN]; bool eaten[maxN];
complex <double> p[maxN], v[maxN];
double f[maxN], w[maxN], V, deltaw, T;
int ord[maxN], g[maxN], n, cnt;
 
inline bool cmp(const int &a, const int &b)
{return imag(p[a]) < imag(p[b]);}
 
void calc(int i)
{
	if (g[i]) calc(g[i]);
	res[cnt++] = Res(imag(p[i]) / 1000, p[i], i);
	deltaw += w[i];
	return;
}
 
int main()
{
	freopen("nemo5.in", "r", stdin);
	freopen("nemo5.out", "w", stdout);
	scanf("%lf%lf%lf%lf%lf%d", w + 0, &V, &T, &p[0].real(), &p[0].imag(), &n);
	for (int i = 1; i < n + 1; ord[i] = i, ++i)
		scanf("%lf%lf%lf%lf%lf", w + i, &p[i].real(), &p[i].imag(), &v[i].real(), &v[i].imag());
	sort(ord + 1, ord + n + 1, cmp);
	int st = 1;
	while (imag(p[ord[st]]) / 1000 - real(p[ord[st]]) / V < -zero) ++st;
	f[ord[st]] = w[ord[st]];
	for (int i = 1; i < st; ++i) f[ord[i]] = -INF;
	for (int i = 0; i < n; ++i)
	if (f[ord[i]] > -INF)
	for (int j = i + 1; j < n + 1; ++j)
	{
		if (imag(p[ord[j]] - p[ord[i]]) / 1000 - fabs(real(p[ord[i]] - p[ord[j]])) / V > -zero)
		if (f[ord[i]] + w[ord[j]] > f[ord[j]])
			f[ord[j]] = f[ord[i]] + w[ord[j]],
			g[ord[j]] = ord[i];
	}
	int pos = ord[n];
	for (int i = 1; i < n + 1; ++i) if (f[i] > f[pos]) pos = i;
	calc(pos);
	printf("%d\n%.9lf\n", cnt, deltaw);
	for (int i = 0; i < cnt; ++i)
		printf("%.9lf %.9lf %.9lf %d\n", res[i].t, res[i].p.real(), 0., res[i].ord);
	return 0;
}

​

第七、八组数据中可以发现小虾的位置呈现数字三角形的形状,只需要按数字三角形的方法进行动态规划即可。

附程序:

​
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <complex>
 
using namespace std;
 
const int maxN = 2010, INF = 0x3f3f3f3f;
const double zero = 1e-12;
struct Res
{
	double t; complex <int> p; int ord; Res() {}
	Res(double t, complex <int> p, int ord):
		t(t), p(p), ord(ord) {}
} res[maxN], tmp[maxN]; bool eaten[maxN];
complex <int> p[maxN], v[maxN];
double w[maxN], deltaw, V, T, f[maxN][maxN];
int ord[maxN][maxN], g[maxN], n, cnt;
 
inline double sqr(double x) {return x * x;}
 
inline double sqr(complex <double> z)
{return sqr(real(z)) + sqr(imag(z));}
 
int main()
{
	freopen("nemo8.in", "r", stdin);
	freopen("nemo8.out", "w", stdout);
	scanf("%lf%lf%lf%d%d%d", w + 0, &V, &T, &p[0].real(), &p[0].imag(), &n);
	for (int i = 1; i < n + 1; ++i)
		scanf("%lf%d%d%d%d", w + i, &p[i].real(), &p[i].imag(), &v[i].real(), &v[i].imag());
	for (int i = 1; i < n + 1; ++i)
		ord[real(p[i])][imag(p[i])] = i;
	int i = n;
	for (; i; --i)
	{
		f[real(p[i])][imag(p[i])] = w[i];
		if (imag(p[i - 1]) < imag(p[i])) break;
	}
	while (--i)
	{
		int x = real(p[i]), y = imag(p[i]), ths = ord[x][y];
		if (f[x - 1][y + 1] > f[x + 1][y + 1])
		{
			f[x][y] = w[i] + f[x - 1][y + 1];
			g[ths] = ord[x - 1][y + 1];
		}
		else
		{
			f[x][y] = w[i] + f[x + 1][y + 1];
			g[ths] = ord[x + 1][y + 1];
		}
	}
	double t = 0;
	res[cnt++] = Res(t = abs(p[1] - p[0]) / V, p[1], 1);
	for (int ths = 1; g[ths]; ths = g[ths])
		res[cnt++] = Res(t += abs(complex <double> (real(p[g[ths]] - p[ths]), imag(p[g[ths]] - p[ths]))) / V, p[g[ths]], g[ths]);
	for (int i = 0; i < cnt; ++i) deltaw += w[res[i].ord];
	printf("%d\n%lf\n", cnt, deltaw);
	for (int i = 0; i < cnt; ++i)
		printf("%.9lf %d %d %d\n", res[i].t, res[i].p.real(), res[i].p.imag(), res[i].ord);
	return 0;
}

​

第九组数据中所有小虾都静止不动,简单的贪心就可以过。

第十组数据可能就只有随机化贪心能够解决了……

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
P2375 [NOI2014] 动物园是一道经典的动态规划题目,以下是该题的详细题意和解题思路。 【题意描述】 有两个长度为 $n$ 的整数序列 $a$ 和 $b$,你需要从这两个序列中各选出一些数,使得这些数构成一个新的序列 $c$。其中,$c$ 序列中的元素必须在原序列中严格递增。每个元素都有一个价值,你的任务是选出的元素的总价值最大。 【解题思路】 这是一道经典的动态规划题目,可以采用记忆化搜索的方法解决,也可以采用递推的方法解决。 记忆化搜索的代码如下: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN], a[MAXN], b[MAXN], n; int dfs(int x, int y) { if (dp[x][y] != -1) return dp[x][y]; if (x == n || y == n) return 0; int res = max(dfs(x + 1, y), dfs(x + 1, y + 1)); if (a[x] > b[y]) { res = max(res, dfs(x, y + 1) + b[y]); } return dp[x][y] = res; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); memset(dp, -1, sizeof(dp)); printf("%d\n", dfs(0, 0)); return 0; } ``` 其中,dp[i][j]表示选到a数组中第i个元素和b数组中第j个元素时的最大价值,-1表示未计算过。dfs(x,y)表示选到a数组中第x个元素和b数组中第y个元素时的最大价值,如果dp[x][y]已经计算过,则直接返回dp[x][y]的值。如果x==n或者y==n,表示已经遍历完一个数组,直接返回0。然后就是状态转移方程了,如果a[x] > b[y],则可以尝试选b[y],递归调用dfs(x, y+1)计算以后的最大价值。否则,只能继续遍历数组a,递归调用dfs(x+1, y)计算最大价值。最后,返回dp[0][0]的值即可。 递推的代码如下: ```c++ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1005; int dp[MAXN][MAXN], a[MAXN], b[MAXN], n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= 0; j--) { dp[i][j] = max(dp[i + 1][j], dp[i + 1][j + 1]); if (a[i] > b[j]) { dp[i][j] = max(dp[i][j], dp[i][j + 1] + b[j]); } } } printf("%d\n", dp[0][0]); return 0; } ``` 其中,dp[i][j]表示选到a数组中第i个元素和b数组中第j个元素时的最大价值。从后往前遍历数组a和数组b,依次计算dp[i][j]的值。状态转移方程和记忆化搜索的方法是一样的。 【参考链接】 P2375 [NOI2014] 动物园:https://www.luogu.com.cn/problem/P2375

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值