跳到正文

题解

P3251 [JLOI2012]时间流逝

Semorius约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年3月21日
收录
2026年8月20日
更新
2026年11月4日
篇幅
约 4 分钟
获赞
9
题目
P3251

版本

  1. v1当前

模拟赛出了这题,整理一下。sto jimmyywang

由于每次选到的数都比上一个小,所以所有可达到的状态序列都是单调递减的,每次只能在序列的末尾增减一个数,如果序列中所有数的和大于 TT,则停止操作。

尝试在每种状态间连边,从上一种状态到下一种可到达的状态连有向边。像这样(T=3,n=2T=3,n=2):

发现每一个状态删去末尾的数后,对应唯一的状态,所以所有的状态连完边后形成一个树形结构。由于存在回溯,所以树上均为双向边。叶子结点就表示已经超过阈值,没有后继状态。设状态总数为 SS,将每个点编号为 11 到 SS,极限数据下 SS 约为 1e61e6。

设 dpxdp_x 表示从编号为 xx 的状态走到叶子结点的期望步数。那么对于每一个 x∈leafx \in leaf,显然 dpx=0dp_x=0(已经到达)。对于每一个非叶子非根节点 xx 所表示的状态,有 PP 的概率走到父亲,有 1−Pcntx\frac{1-P}{cnt_{x}} 的概率走到每一个儿子,其中 cntxcnt_{x} 表示 xx 的子节点个数,所以有如下转移:

dpx=P×(dpfa+1)+1−Pcntx×∑y∈son(dpy+1)dp_x=P \times (dp_{fa}+1)+\frac{1-P}{cnt_x} \times \sum_{y \in son}(dp_y+1)

化简以后得到:

dpx=1+P×dpfa+1−Pcntx×∑y∈sondpydp_x=1 + P \times dp_{fa} + \frac{1-P}{cnt_x} \times \sum_{y \in son}dp_y

这个转移有后效性。尝试用待定系数法将 dpxdp_x 用 dpfadp_{fa} 表示。

设 dpx=kx×dpfa+bxdp_x = k_x \times dp_{fa} + b_x,代入上式得:

dpx=1+P×dpfa+1−Pcntx×∑y∈son(ky×dpx+by)dp_x=1 + P \times dp_{fa} + \frac{1-P}{cnt_x} \times \sum_{y \in son}(k_y \times dp_x + b_y)

化简得:

dpx=P1−1−Pcntx×∑y∈sonky×dpfa+1+1−Pcntx∑y∈sonby1−1−Pcntx∑y∈sonkydp_x = \frac{P}{1-\frac{1-P}{cnt_x} \times \sum_{y \in son}k_y} \times dp_{fa} + \frac{1 + \frac{1-P}{cnt_x} \sum_{y \in son}b_y}{1 - \frac{1-P}{cnt_x} \sum_{y \in son}k_y}

所以:

kx=P1−1−Pcntx×∑y∈sonkyk_x = \frac{P}{1-\frac{1-P}{cnt_x} \times \sum_{y \in son}k_y} bx=1+1−Pcntx∑y∈sonby1−1−Pcntx∑y∈sonkyb_x = \frac{1 + \frac{1-P}{cnt_x} \sum_{y \in son}b_y}{1 - \frac{1-P}{cnt_x} \sum_{y \in son}k_y}

回溯时转移 bxb_x 和 kxk_x 即可。

由于根节点(初始状态:空序列)没有父亲,所以在根节点时 P=0P = 0, brootb_{root} 即为答案。

时间复杂度 O(S)O(S)。

C++
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 2000005;
double P, b[SIZE], k[SIZE];
int n, m, a[SIZE], iid;

bool cmp(int x, int y){
	return x > y;
}

void deal(int now, int id, int sum){
	if(sum > m) return;
	int x = id, cnt = n-now+1;
	double sb = 0, sk = 0, t = (1.0-P);
	for(int i = now; i <= n; i++){
		int y = ++iid;
		deal(i, y, sum+a[i]);
		sb += b[y]; sk += k[y];
	}
	if(x == 1) P = 0, t = 1.0; 
	t = t / (double)cnt;
	k[x] = P/(1.0-t*(double)sk);
	b[x] = (1.0+t*(double)sb)/(1.0-t*(double)sk);
}

int main(){
	while(~scanf("%lf%d%d", &P, &m, &n)){
		for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1, cmp);
		iid = 1;
		deal(1, 1, 0);
		printf("%.3lf\n", b[1]);	
	}
	return 0;
}
原文
存档评论1 条抓取于