题目:信号中转
思路:动态规划
代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
//快速输入
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//快速输出
static PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out));
static int mod=1_000_000_007;
public static void main(String[] args) throws IOException {
String[] s=br.readLine().split(" ");
int n=Integer.parseInt(s[0]);
int[] a=new int[n];
s=br.readLine().split(" ");
for(int i=0;i<n;i++) {
a[i]=Integer.parseInt(s[i]);
}
int[] dp=new int[n];
int flag=Integer.MAX_VALUE;
Arrays.fill(dp, flag);
dp[0]=0;
for(int i=0;i<n;i++) {
if(dp[i]==flag) {
continue;
}
for(int j=i+1;j<n&&j-i<=a[i];j++) {
dp[j]=Math.min(dp[j], dp[i]+1);
}
}
pw.println(dp[n-1]);
pw.flush();
}
}