[ACM 预备训练] Week3
查看代码源文件
Visit Cai1Hsu/blog
C/C++ 自动构建: 前往 GitHub
Easy ~ P1028 [NOIP2001 普及组] 数的计算
思路
- 见代码注释
代码
1 | n = int(input()) |
Normal ~ P1192 台阶问题
思路
核心思想
类似于数学归纳法的递推思想,对于上第n个台阶,我们可以考虑从[n - k, n - 1]中每一个台阶跳到n。
因此,我们只需记录[1, n]间每一个数的方法,然后求[n - k, n - 1]的和
处理 edge case
- 当
i <= k时,我们除了可以通过前面的台阶跳上去,还可以从起点直接跳上去。 - 其次,当
i <= k时,要确保区间左端点大于等于1。
代码
1 |
|
Hard ~ P1044 [NOIP2003 普及组] 栈
思路
创建一个 dp[i,j] 记录 当 已经(总共)压入i个元素 和 已经(总共)弹出j个元素 时的情况数。
代码
1 | int n = int.Parse(Console.ReadLine()!); |
关于遍历顺序的解释
The condition tpush == tpop checks if the stack is empty, in which case we can only push values onto the stack. Therefore, we set dp[tpush, tpop] = dp[tpush - 1, tpop] to update the value of dp[tpush, tpop].
If the stack is not empty (tpush < tpop), then we can either push a value onto the stack (dp[tpush - 1, tpop]) or pop a value from the stack (dp[tpush, tpop - 1]). Therefore, we set dp[tpush, tpop] = dp[tpush - 1, tpop] + dp[tpush, tpop - 1] to update the value of dp[tpush, tpop].
The reason for starting the inner loop at tpush is that the values of dp[tpush - 1, tpop are computed before the values of dp[tpush, tpop - 1]. This is because the values of tpush are enumerated in increasing order, so the value of tpush - 1 is always less than or equal to the value of tpush.
If we started the inner loop at tpop = 1, then we would need to compute the values of dp[tpush, tpop - 1] before the values of dp[tpush - 1, tpop]. This would require us to store the values of dp in a different order, which would make the algorithm more complicated.
Therefore, enumerating the values of tpush before the values of tpop and starting the inner loop at tpush ensures that the values of dp are computed correctly and simplifies the implementation of the algorithm.
Lunatic ~ P1003 [NOIP2011 提高组] 铺地毯
思路
- 先读入数据,将地毯全部储存起来
- 读入坐标
- 遍历地毯,如果被新的地毯覆盖,则更新答案
代码
感受一下Rust的优美
1 | use std::io::stdin; |
博客仓库
- 标题: [ACM 预备训练] Week3
- 作者: Caiyi Shyu
- 创建于 : 2023-10-23 11:01:21
- 更新于 : 2024-09-30 18:43:49
- 链接: https://blog.caiyi1.me/2023/10/23/ACM-Week3/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。