-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP11060GreedyFail.java
More file actions
40 lines (34 loc) · 954 Bytes
/
P11060GreedyFail.java
File metadata and controls
40 lines (34 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package quki.algorithm.dp;
import java.util.Scanner;
public class P11060GreedyFail {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
int[] s = new int[n];
int inf = 1000000000;
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
a[i] = x;
if(i==0){
s[i] = 0;
}else{
s[i] = inf;
}
}
for(int i = 0; i<n;i++){
int t = a[i];
for(int j = 1; j<=t; j++){
int newIndex = i+j;
if(newIndex <= n-1){
if(s[newIndex] > s[i]+1){
s[newIndex] = s[i]+1;
}
}
}
}
int ans = s[n-1];
if(ans == inf) ans =-1;
System.out.println(ans);
}
}