线性dp Codeforces Round #239 (Div. 2) D题 Long Path

本文探讨了一种迷宫寻路算法,通过分析房间间的连接方式和特定规则,使用动态规划方法计算从起始房间到达目标房间所需的最少步数。文章首先介绍了问题背景,随后详细解释了算法原理,并给出了两种实现方案,一种是递归方法但存在超时风险,另一种是优化后的动态规划方法,能够有效避免重复计算,显著提升效率。

Long Path

One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.

The maze is organized as follows. Each room of the maze has two one-way portals. Let’s consider room number i (1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number p i, where 1 ≤ p i ≤ i.

In order not to get lost, Vasya decided to act as follows.

  1. Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
  2. Let’s assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room p i), otherwise Vasya uses the first portal.

Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.


题目大意:n 个房间,标号为 1-n,起始位置为 1,需要到 n+1 号房间,每次进入一个房间要在房间打个标记,如果该房间总的标记的数量为偶数,那么下一次可以去第 i+1 个房间,如果为奇数,那么下一次只能去第 p[i] 个房间,问到第 n+1 个房间的步数;

比较容易想到:dp[i]=dp[i-1]+1;dp[p[i]]=dp[i]+1; 然后写一个递归程序,但是超时;

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=1100;
const int M=2000100;
const LL mod=1e9+7;
int n,p[N];
LL sum[N],dp[N];
void dfs(int q){
	if(q==n+1) return;
	sum[q]++;
	if(sum[q]%2ll==0){
		(dp[q+1]=dp[q]+1ll)%=mod;
		dfs(q+1);
	}
	else{
		(dp[p[q]]=dp[q]+1ll)%=mod;
		dfs(p[q]);
	}
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>p[i];
	dfs(1);
	cout<<dp[n+1]<<endl;
	return 0;
}

可以发现如果第 i 个房间是第一次到达,那么第 i-1 个房间一定是到达2次,所以:

dp[i] 表示第一次到达第 i 个房间的步数,转移方程为:

dp[i]+=dp[i-1]+1;
dp[i]+=dp[i-1]+1-dp[p[i-1]];表示第 i-1 个房间绕回去再回到第 i-1 个房间的步数,因为是到达两次,所以必须绕;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=1100;
const int M=2000100;
const LL mod=1e9+7;
int n,p[N];
LL dp[N];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>p[i];
	for(int i=2;i<=n+1;i++){
		dp[i]+=dp[i-1]+1;
		dp[i]+=dp[i-1]+1-dp[p[i-1]];
		dp[i]%=mod; 
	}
	cout<<(dp[n+1]+mod)%mod<<endl;
	return 0;
}
对于Codeforces Round 1005 Div. 2中的D解答或解释,当前提供的引用资料并未直接涉及该轮次的比赛题目详情。然而,可以基于过往比赛的经验以及相似类型的编程竞赛问提供一般性的指导。 ### 解决方案概述 通常情况下,在解决此类算法竞赛题目时,会遵循特定的方法论来处理输入数据并计算所需的结果。虽然具体到Codeforces Round 1005 Div. 2 Problem D的信息未被提及,但可以根据以往经验推测可能涉及到的数据结构和算法技术: - **读取测试案例数量**:程序首先应该能够接收多个独立的测试案例数目\(t\),其中每一个案例都包含了不同的参数集[^3]。 - **解析数组元素**:针对每个测试案例,需解析给定长度为\(n\)的一系列整数\[a_1, a_2,...,a_n\]作为操作对象[^2]。 - **查询次数限制**:需要注意的是,所有测试案例中查询总数不得超过设定的最大值,比如这里提到不超过\(2 \times 10^5\)次查询[^1]。 - **输出格式规定**:当准备打印最终答案时,应按照指定格式输出结果,并继续处理下一个测试案例直到完成全部测试[^4]。 考虑到这些通用原则,如果要设计一个适用于此类型问的解决方案框架,则可能会如下所示: ```python def solve_problem(): import sys input = sys.stdin.read data = input().split() index = 0 results = [] t = int(data[index]) index += 1 for _ in range(t): n = int(data[index]) index += 1 numbers = list(map(int, data[index:index+n])) index += n # 假设这里是解决问的核心逻辑部分, # 需要根据具体的Problem D描述调整这部分代码实现。 result_for_case = "Example Result" results.append(result_for_case) print("\n".join(results)) ``` 上述伪代码展示了如何构建基本架构用于批量处理多组测试用例,但是核心业务逻辑需要依据实际问定义进行填充和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值