Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 25706번 자전거 묘기 (silver 4 본문
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[]args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int n=Integer.parseInt(br.readLine());
StringTokenizer st=new StringTokenizer(br.readLine());
int[] height=new int[n+1];
for(int i=1; i<=n; i++){
height[i]=Integer.parseInt(st.nextToken());
}
height[n]=1;
for(int i=n-1; i>=1; i--){
if(height[i]==0){
height[i]=height[i+1]+1;
}
else{
if(i+1+height[i]<=n){
height[i]=height[i+1+height[i]]+1;
}
else{
height[i]=1;
}
}
}
for(int i=1; i<=n; i++){
bw.write(height[i]+" ");
}
bw.flush();
}
}
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder("");
int n=Integer.valueOf(br.readLine());
int[] num=new int[n+1];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=1; i<=n; i++){
num[i]=Integer.valueOf(st.nextToken());
}
int[] dp=new int[n+1];
dp[n]=1;
for(int i=n-1; i>=1; i--){
int t=i+num[i]+1;
if(t>n){
dp[i]=1;
}
else{
dp[i]=dp[t]+1;
}
}
for(int i=1; i<=n; i++){
sb.append(dp[i]+" ");
}
System.out.println(sb.toString());
}
}
//24.03.05코드 간결해진것 같다.
'백준공부 > java' 카테고리의 다른 글
[백준] 2623번 음악프로그램 (gold 3 (0) | 2024.03.15 |
---|---|
[백준] 27961번 고양이는 많을수록 좋다 (bronze 1 (0) | 2024.03.15 |
[백준] 10610번 30 (silver 4 (0) | 2024.03.15 |
[백준] 20955번 민서의 응급 수술 (gold 4 (2) | 2024.03.15 |
[백준] 2812번 크게 만들기 (gold 3 (0) | 2024.03.15 |