https://www.acmicpc.net/problem/25556
제25556호: 포항공대
파닉스가 순열을 정리할 수 있으면 YES를 반환하고 그렇지 않으면 NO를 반환합니다.
www.acmicpc.net
입력 예시 1)
10
4 3 6 7 8 9 10 2 1 5 --> YES
입력 예시 2)
5
5 4 3 2 1 --> NO
내 솔루션 아이디어: 이전에 온 숫자 > 이후에 온 숫자인 경우 cnt(= 0)가 1씩 증가합니다. 따라서 cnt가 3보다 크면 문제에 제시된 순열을 정리할 수 없다고 생각합니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String() args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int cnt = 0;
int init = Integer.parseInt(st.nextToken());
while (st.hasMoreTokens()) {
int second = Integer.parseInt(st.nextToken());
if (init > second ) {
cnt++;
}
init = second;
}
if (cnt > 3) {
System.out.print("NO");
}else{
System.out.print("YES");
}
}
}
정답 풀이 방법 -> 모든 스택에서 입력 개수가 크면 cnt를 증가 -> 오름차순으로 정렬할 수 있나요?
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.in));
int num = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
Stack<Integer>() stack= new Stack(4);
for (int i = 0; i < 4; i++) {
stack(i) = new Stack<>();
stack(i).push(0);
}
while (st.hasMoreTokens()) {
int second = Integer.parseInt(st.nextToken());
int cnt = 0;
for (int i = 0; i < 4; i++) {
if (stack(i).peek() < second) {
stack(i).push(second);
break;
}else{
cnt++;
}
if (cnt > 3) {
System.out.print("NO");
return;
}
}
}
System.out.print("YES");
}
}
