Algorithm/백준
[백준] 2529번 부등호
sirius
2021. 3. 4. 17:25
https://www.acmicpc.net/problem/2529
2529번: 부등호
두 종류의 부등호 기호 ‘<’와 ‘>’가 k개 나열된 순서열 A가 있다. 우리는 이 부등호 기호 앞뒤에 서로 다른 한 자릿수 숫자를 넣어서 모든 부등호 관계를 만족시키려고 한다. 예를 들어, 제시
www.acmicpc.net
숫자 두개 비교했을 때 부등호가 참인 경우만 처리
import java.util.*;
import java.io.*;
public class Main {
public static int K;
public static char[] arr; // 부등호
public static boolean[] visit = new boolean[10]; // 0 ~ 9
public static List<String> ans = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
K = Integer.parseInt(st.nextToken());
arr = new char[K];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < K; i++) {
arr[i] = st.nextToken().charAt(0);
}
dfs("", 0);
Collections.sort(ans);
// 최대값
System.out.println(ans.get(ans.size() - 1));
// 최소값
System.out.println(ans.get(0));
}
public static void dfs(String num, int depth) {
if(depth == K + 1 ) {
ans.add(num);
return;
}
for(int i = 0 ; i < 10; i++) {
// 첫번째 숫자인 경우 넘어감
// 첫번째 넘어갔으므로 arr[depth - 1]
if(depth == 0 || !visit[i] && compare(num.charAt(num.length()- 1) - '0', i, arr[depth - 1 ])) {
visit[i] = true;
dfs(num + i, depth + 1);
visit[i] = false;
}
}
}
public static boolean compare(int a, int b, char c) {
if(c == '<') {
return a < b;
} else {
return a > b;
}
}
}