CF797E Array Queries 题解
题意
给定一个大小为
有
我模拟了十几分钟没看懂样例,结果发现题读错了。
- 原题面
a is an array of n positive integers, all of which are not greater than n.
You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + a__p + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations.
分析
首先想到朴素的方法就是对于每一个询问依次模拟,但是
转移时
说了半天还不是解决不了问题,你看
没错,纯 DP 确实解决不了问题,但是这正是本题的精妙之处。注意上面的公式
我们将
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, q;
int arr[maxn];
int f[maxn][305];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i]);
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= 300; j++) {
if (i + arr[i] + j > n) f[i][j] = 1;
else f[i][j] = f[i + arr[i] + j][j] + 1;
}
}
scanf("%d", &q);
while (q--) {
int p, k;
int ans = 0;
scanf("%d%d", &p, &k);
if (k <= 300) {
printf("%d\n", f[p][k]);
continue;
}
while (p <= n) {
p = p + arr[p] + k;
ans++;
}
printf("%d\n", ans);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】