12789번 - 도키도키 간식드리미
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>)); int N = Integer.<i>parseInt</i>(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); Stack<Integer> stack = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); int arr [] = new int [N]; for(int i = 0; i < N; i++) { arr[i] = Integer.<i>parseInt</i>(st.nextToken()); } for(int i = N-1; i >= 0; i--) { stack.push(arr[i]); } int cnt = 1; int arr2 [] = new int [N]; for(int i=0; i<N; i++){ int num = stack.pop(); if(num == cnt){ arr2[cnt-1] = num; cnt++; } else { stack2.push(num); } } int num2; for(int i=cnt-1; i<N; i++){ num2 = stack2.pop(); arr2[i] = num2; } boolean ans = true; for(int i=0; i<N; i++){ if(arr2[i] != i+1) ans = false; } if(ans) System.<i>out</i>.println("Nice"); else System.<i>out</i>.println("Sad"); } }
반례 하나 놓고 가용
댓글을 작성하려면 로그인해야 합니다.
fbwoduf1 2년 전
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.<i>in</i>)); int N = Integer.<i>parseInt</i>(br.readLine()); StringTokenizer st = new StringTokenizer(br.readLine()); Stack<Integer> stack = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); int arr [] = new int [N]; for(int i = 0; i < N; i++) { arr[i] = Integer.<i>parseInt</i>(st.nextToken()); } for(int i = N-1; i >= 0; i--) { stack.push(arr[i]); } int cnt = 1; int arr2 [] = new int [N]; for(int i=0; i<N; i++){ int num = stack.pop(); if(num == cnt){ arr2[cnt-1] = num; cnt++; } else { stack2.push(num); } } int num2; for(int i=cnt-1; i<N; i++){ num2 = stack2.pop(); arr2[i] = num2; } boolean ans = true; for(int i=0; i<N; i++){ if(arr2[i] != i+1) ans = false; } if(ans) System.<i>out</i>.println("Nice"); else System.<i>out</i>.println("Sad"); } }