[백준](Java) 21944 - 문제 추천 시스템 Version 2

민지킴·2021년 7월 1일
0

백준

목록 보기
38/48
post-thumbnail

문제 링크

https://www.acmicpc.net/problem/21944

문제 풀이

이전 Version에서 알고리즘이라는 필드가 추가되었다. 또한 몇가지 명령어가 추가 되었다.
ts는 난이도순으로 정렬하고, 같은 난이도는 idx 순으로 정렬한 TreeSet이다. 알고리즘에 대해서는 정렬을 신경쓰지 않고 다른 자료형을 만들어서 접근할 생각을 했다.
그렇게 만든 자료형은 Map<Integer, TreeSet> algoTreeSet이다.
이렇게 선언한 이유는 특정 알고리즘을 키값으로 하여, 그 알고리즘에 대해서 구분을 해놓기 위함이다.

levelmap, algomap은 idx를 key값으로 하고,각각 난이도, 알고리즘을 value값으로 가지는 자료형으로 solved command를 풀기 위해서 추가했다.

command가 recommand 인 경우 알고리즘 분류가 G인것중에서 난이도가 제일 높은것의 번호이다. 그래서 algoTreeSet의 key 값을 통해서 해당 알고리즘에 속하는 Value인 TreeSet을 구한뒤에 거기서 맨 앞에있는값(제일 쉬운 난이도), 제일 뒤에 있는값(제일 어려운 난이도)를 구했다.

          if (command.equals("recommend")) {
                int n_algo = sc.nextInt();
                int pos = sc.nextInt();
                if (pos == 1) {
               System.out.println(algoTreeSet.get(n_algo).last().idx);
                } else {
                    System.out.println(algoTreeSet.get(n_algo).first().idx);
                }
            }

recommend2의 경우 제일 간단하게 난이도로 정렬된 ts의 맨 앞, 맨 뒤의 값을 구하면 되었다.

else if (command.equals("recommend2")) {
              int pos = sc.nextInt();
              if (pos == 1) {
                  System.out.println(ts.last().idx);
              } else {
                  System.out.println(ts.first().idx);
              }
          } 

recommend3의 경우 새로운 메소드를 사용해서 풀었는데
floor와 ceiling이었다.

에서 지정된 난이도보다 같거나 높은것중에서 제일 작은것 -> ceiling
지정도니 난이도보다 같거나 낮은것중에서 제일 큰것 -> floor
를 사용해서 풀 수 있었다.
또한 return이 null으로 나올수 있으므로 이를 체크해주었다.

else if (command.equals("recommend3")) {
              int pos = sc.nextInt(); // 1, -1
              int stand = sc.nextInt();// 난이도
              if (pos == 1) {
                  if (ts.ceiling(new Problem(0, stand, 0)) == null) {
                      System.out.println(-1);
                  } else {
                      System.out.println(ts.ceiling(new Problem(0, stand, 0)).idx);
                  }
              } else {
                  if (ts.floor(new Problem(0, stand, 0)) == null) {
                      System.out.println(-1);
                  } else {
                      System.out.println(ts.floor(new Problem(0, stand, 0)).idx);
                  }
              }
          } 

마지막으로 add, solove는 큰 어려움 없이 위에 선언했던 모든 자료형들에서 값을 추가하거나, 삭제해서 풀수 있었다.

else if (command.equals("add")) {
              int n_idx = sc.nextInt();
              int n_level = sc.nextInt();
              int n_algo = sc.nextInt();
              ts.add(new Problem(n_idx, n_level, n_algo));
              if (algoTreeSet.containsKey(n_algo)) {
                  TreeSet<Problem> temp = algoTreeSet.get(n_algo);
                  temp.add(new Problem(n_idx, n_level, n_algo));
                  algoTreeSet.replace(n_algo, temp);
              } else {
                  TreeSet<Problem> temp = new TreeSet<>();
                  temp.add(new Problem(n_idx, n_level, n_algo));
                  algoTreeSet.put(n_algo, temp);
              }
              levelmap.put(n_idx, n_level);
              algomap.put(n_idx, n_algo);
          } else {
              int n_idx = sc.nextInt();
              int n_level = levelmap.get(n_idx);
              int n_algo = algomap.get(n_idx);
              ts.remove(new Problem(n_idx, n_level, n_algo));
              algoTreeSet.get(n_algo).remove(new Problem(n_idx, n_level, n_algo));
              levelmap.remove(n_idx);
              algomap.remove(n_idx);
          }

코드

import java.util.*;

public class Main {

    public static class Problem implements Comparable<Problem> {
        int idx;
        int level;
        int algo;

        public Problem(int idx, int level, int algo) {
            this.idx = idx;
            this.level = level;
            this.algo = algo;
        }

        public int compareTo(Problem o) {
            if (level - o.level == 0) {
                return idx - o.idx;
            } else {
                return level - o.level;
            }
        }

        public String toString() {
            return "idx : " + idx + " level : " + level + " algo : " + algo;
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        TreeSet<Problem> ts = new TreeSet();
        Map<Integer, TreeSet<Problem>> algoTreeSet = new HashMap();
        Map<Integer, Integer> levelmap = new HashMap<>();
        Map<Integer, Integer> algomap = new HashMap<>();

        for (int i = 0; i < n; i++) {
            int n_idx = sc.nextInt();
            int n_level = sc.nextInt();
            int n_algo = sc.nextInt();
            ts.add(new Problem(n_idx, n_level, n_algo));
            if (algoTreeSet.containsKey(n_algo)) {
                TreeSet<Problem> temp = algoTreeSet.get(n_algo);
                temp.add(new Problem(n_idx, n_level, n_algo));
                algoTreeSet.replace(n_algo, temp);
            } else {
                TreeSet<Problem> temp = new TreeSet<>();
                temp.add(new Problem(n_idx, n_level, n_algo));
                algoTreeSet.put(n_algo, temp);
            }

            levelmap.put(n_idx, n_level);
            algomap.put(n_idx, n_algo);
        }

        int m = sc.nextInt();

        for (int i = 0; i < m; i++) {
            String command = sc.next();
            if (command.equals("recommend")) {
                int n_algo = sc.nextInt();
                int pos = sc.nextInt();
                if (pos == 1) {
                    System.out.println(algoTreeSet.get(n_algo).last().idx);
                } else {
                    System.out.println(algoTreeSet.get(n_algo).first().idx);
                }
            } else if (command.equals("recommend2")) {
                int pos = sc.nextInt();
                if (pos == 1) {
                    System.out.println(ts.last().idx);
                } else {
                    System.out.println(ts.first().idx);
                }
            } else if (command.equals("recommend3")) {
                int pos = sc.nextInt(); // 1, -1
                int stand = sc.nextInt();// 난이도
                if (pos == 1) {
                    if (ts.ceiling(new Problem(0, stand, 0)) == null) {
                        System.out.println(-1);
                    } else {
                        System.out.println(ts.ceiling(new Problem(0, stand, 0)).idx);
                    }
                } else {
                    if (ts.floor(new Problem(0, stand, 0)) == null) {
                        System.out.println(-1);
                    } else {
                        System.out.println(ts.floor(new Problem(0, stand, 0)).idx);
                    }
                }
            } else if (command.equals("add")) {
                int n_idx = sc.nextInt();
                int n_level = sc.nextInt();
                int n_algo = sc.nextInt();
                ts.add(new Problem(n_idx, n_level, n_algo));
                if (algoTreeSet.containsKey(n_algo)) {
                    TreeSet<Problem> temp = algoTreeSet.get(n_algo);
                    temp.add(new Problem(n_idx, n_level, n_algo));
                    algoTreeSet.replace(n_algo, temp);
                } else {
                    TreeSet<Problem> temp = new TreeSet<>();
                    temp.add(new Problem(n_idx, n_level, n_algo));
                    algoTreeSet.put(n_algo, temp);
                }
                levelmap.put(n_idx, n_level);
                algomap.put(n_idx, n_algo);
            } else {
                int n_idx = sc.nextInt();
                int n_level = levelmap.get(n_idx);
                int n_algo = algomap.get(n_idx);
                ts.remove(new Problem(n_idx, n_level, n_algo));
                algoTreeSet.get(n_algo).remove(new Problem(n_idx, n_level, n_algo));
                levelmap.remove(n_idx);
                algomap.remove(n_idx);
            }
        }
    }
}

profile
하루하루는 성실하게 인생 전체는 되는대로

0개의 댓글