CodeForces 407B Long Path

B. Long Path
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 pi, where 1 ≤ pi ≤ i.

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

  • Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1
  • 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 pi), 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.

Input

The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.

Output

Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).

Sample test(s)
input
2
1 2
output
4
input
4
1 1 2 3
output
20
input
5
1 1 1 1 1
output
62


这道题一开始当模拟题做,果断就TLE了o(╯□╰)o

后来一看题目都说了数据会很大,都让模10^9+7了

TLE简直是活该啊(⊙o⊙)…


蓝后...发现是DP题


根据题目可以推断出,当第一次走到 room i 时,再走一步会往回走,走到 room pi

只有第二次走到 room i 时,才会继续往前走

假设第一次走到 room i 需要dp[i]步

那么走到 room i+1 就需要先走一步退回 room pi,再经过不知道多少步再次走到 room i,再走一步走到room i+1

dp[i+1] = dp[i]+1+不知道多少步+1


那么这个不知道多少步到底是多少步捏╮(╯▽╰)╭

让我们来分析一下,嗯嗯


记第一次走到 room pi 时各room的状态集合为S = { s1, s2, s3,..., s(i-1) }(集合中的元素表示各房间的奇偶性,如用0表示even,1表示odd)

那么当走到 room i 再回到 room pi 时 S 不变

也就是此时与第一次走到 room pi 时处于同一状态(仅仅看room 1 - room i-1 的话)

因此再次走到 room i 所需的步数与第一次相同 即 dp[i]-dp[pi]

代入上面的式子,得状态转移方程:

dp[i+1] = 2dp[i]-dp[pi]+2


代码如下:

#include<iostream>
using namespace std;

const int maxn = 1000 + 5;
const int mod = 1000000000 + 7;

int portal[maxn];
long long dp[maxn];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> portal[i];
  dp[1] = 0;
  for (int i = 1; i <= n; i++)
    dp[i+1] = (2*dp[i]-dp[portal[i]]+2+mod) % mod;
  cout << dp[n+1] << endl;
  return 0;
}


代码中有一个要注意的地方

因为题目要求的是总步数对(10^7+7)的模

根据取模的性质,可以在每次状态转移后对结果进行取模

为了避免负数的产生,加上(10^7+7)再取模

### 2D Gaussian Splatting 的概念 2D Gaussian Splatting 是一种用于高效表示和渲染三维场景的技术,其核心思想是通过二维高斯分布(2D Gaussians)来近似三维空间中的物体表面[^2]。相比传统的基于体素或网格的方法,2DGS 提供了一种更加紧凑且快速的方式来进行场景建模和渲染。 --- ### 2D Gaussian Splatting 的实现方法 #### 数据结构设计 2D Gaussian Splatting 使用的是 **定向椭圆盘** 来作为基本单元,这些单元可以看作是对三维场景的一种简化表示形式。每一个 2D 高斯分布由以下几个参数定义: - 中心位置 $(\mu_x, \mu_y)$ 表示该高斯分布在图像平面上的位置。 - 协方差矩阵 $\Sigma$ 描述了形状和方向的信息。 - 不透明度 ($\alpha$) 控制可见程度。 - RGB颜色值决定视觉效果。 这种数据结构的设计使得它能够很好地适应现代图形处理硬件的能力,在GPU上执行高效的并行运算成为可能[^4]。 #### 渲染过程优化 在实际渲染过程中,2DGS 利用了精确的光线椭圆形基元之间的相交测试技术 ("ray-splat intersection") ,从而实现了比传统体积渲染更高的精度以及更低的时间复杂度。具体来说: 1. 对于每一条从相机出发到达屏幕像素点的视线(ray),找到其发生碰撞的所有splat; 2. 计算每个被击中的splat对该特定像素贡献的颜色强度; 3. 将所有相关联的结果按照一定规则融合起来形成最终画面输出。 这种方法避免了由于视角变化而导致的传统3D GS可能出现的问题——即当观察角度改变时产生的深度排序不连贯现象。 --- ### 应用场景分析 #### 新视图合成 得益于其强大的表达能力和优秀的性能表现,2D Gaussian Splatting 特别适合应用于虚拟现实(VR)/增强现实(AR)领域内的实时动态环境创建任务之中。例如,在电影制作或者游戏开发当中,可以通过采集少量真实世界的图片资料之后构建完整的沉浸式体验场所。 #### 自动驾驶感知系统改进 另外一方面,在自动驾驶汽车所依赖的各种传感器获取的数据基础上运用此算法还可以进一步提升对于周围障碍物识别的速度准确性。因为相较于其他类型的模型而言,采用2D GS方式不仅可以减少存储需求量还能加快推理速度,这对于需要即时响应的安全关键型应用尤为重要[^3]。 #### SLAM 系统升级 同时,在同步定位地图绘制(SLAM)研究方面也有着广泛的应用前景。借助于2D Gaussian Splatting 技术的优势特性可以帮助机器人更有效地理解其所处的空间布局情况,并据此做出合理的行为决策。 ```python import numpy as np def render_2d_gaussian(center, covariance_matrix, alpha, color): """ Simulates rendering a single 2D Gaussian splat. Parameters: center (tuple): The mean position of the Gaussian distribution on image plane. covariance_matrix (np.ndarray): Covariance matrix defining shape and orientation. alpha (float): Opacity value controlling visibility level. color (list or tuple): RGB values specifying appearance characteristics. Returns: rendered_image (np.ndarray): A synthetic representation showing how this element contributes visually within scene context. """ # Placeholder implementation details omitted here... pass ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值