이 영역을 누르면 첫 페이지로 이동
천천히 꾸준히 조용히 블로그의 첫 페이지로 이동

천천히 꾸준히 조용히

페이지 맨 위로 올라가기

천천히 꾸준히 조용히

천천히 꾸준히 조용히.. i3months 블로그

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

  • 2022.04.23 18:23
  • Algorithm/Baekjoon
반응형

 

 

지난번에 풀었던 버전1 문제와 비슷하지만.. 추가된 조건들이 많아 구현하는데 어려움이 있었다..

 

알고리즘에 대한 분류는 전체 알고리즘의 수가 100개임을 고려해 ArrayList를 이용해 각각 추가해서 처리해줬고, 알고리즘이 추가됨에 따라 해시맵으로는 문제 번호를 통해 알고리즘 분류와 난이도를 얻을 수 있도록 설계했다.

 

풀다 보니 코드가 길어져서.. 중간에 실수가 있었으면 정말 슬펐을 것 같다..

 

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();

        TreeSet<Problem> ts = new TreeSet<>();
        HashMap<Integer, Problem2> hm = new HashMap<>();

        ArrayList<TreeSet<Problem>> algorithm_list = new ArrayList<>();

        int N = Integer.parseInt(br.readLine());

        StringTokenizer st;

        for(int i=0; i<101; i++){
            algorithm_list.add(new TreeSet<>()); 
        } // algorithm's kind == 100

        for(int i=0; i<N; i++){     
            st = new StringTokenizer(br.readLine());       
            int num = Integer.parseInt(st.nextToken());            
            int difficulty = Integer.parseInt(st.nextToken());    
            int algorithm = Integer.parseInt(st.nextToken());            
            
            ts.add(new Problem(num, difficulty));
            algorithm_list.get(algorithm).add(new Problem(num, difficulty));
            hm.put(num, new Problem2(difficulty, algorithm));
        }

        int M = Integer.parseInt(br.readLine());

        for(int i=0; i<M; i++){
            st = new StringTokenizer(br.readLine());
            String command = st.nextToken();

            if(command.equals("recommend")){
                int algorithm = Integer.parseInt(st.nextToken());
                int cmd = Integer.parseInt(st.nextToken());

                if(cmd == 1){
                    sb.append(algorithm_list.get(algorithm).last().num + "\n"); // first 일수도..
                }
                if(cmd == -1){
                    sb.append(algorithm_list.get(algorithm).first().num + "\n"); // last 일수도..
                }
            }

            if(command.equals("recommend2")){
                int cmd = Integer.parseInt(st.nextToken());

                if(cmd == 1){
                    sb.append(ts.last().num + "\n"); // swap..?
                }
                if(cmd == -1){
                    sb.append(ts.first().num + "\n"); // swap?
                }
            }

            if(command.equals("recommend3")){
                int cmd = Integer.parseInt(st.nextToken());
                int condition = Integer.parseInt(st.nextToken());

                if(cmd == 1){
                    Problem temp = ts.higher(new Problem(0, condition));
                    if(temp == null){
                        sb.append(-1 + "\n");
                    }else{
                        sb.append(temp.num + "\n");
                    }
                }

                if(cmd == -1){
                    Problem temp = ts.lower(new Problem(0, condition));
                    if(temp == null){
                        sb.append(-1 + "\n");
                    }else{
                        sb.append(temp.num + "\n");
                    }
                }
            }

            if(command.equals("add")){
                int num = Integer.parseInt(st.nextToken());
                int difficulty = Integer.parseInt(st.nextToken());
                int algorithm = Integer.parseInt(st.nextToken());

                ts.add(new Problem(num, difficulty));
                algorithm_list.get(algorithm).add(new Problem(num, difficulty));
                hm.put(num, new Problem2(difficulty, algorithm));                
            }

            if(command.equals("solved")){
                int num = Integer.parseInt(st.nextToken());
                int difficulty = hm.get(num).difficulty;
                int algorithm = hm.get(num).algorithm;

                ts.remove(new Problem(num, difficulty));
                hm.remove(num);
                algorithm_list.get(algorithm).remove(new Problem(num, difficulty));
                
            }
        }

        System.out.println(sb);

        

    }

    static class Problem implements Comparable<Problem>{
        int num = 0;
        int difficulty = 0;

        Problem(int a, int b){
            num = a;
            difficulty = b;
        }

        public int compareTo(Problem o2){
            if(difficulty > o2.difficulty){
                return 1;                
            }else if(difficulty < o2.difficulty){
                return -1;
            }else{
                if(num > o2.num){
                    return 1;
                }else if(num < o2.num){
                    return -1;
                }else{
                    return 0;
                }
            }
        }

    }// end of Problem

    static class Problem2{
        int difficulty = 0;
        int algorithm = 0;

        Problem2(int a, int b){
            difficulty = a;
            algorithm = b;
        }
    }

}
반응형

'Algorithm > Baekjoon' 카테고리의 다른 글

[백준] 1972 놀라운 문자열 - Java  (0) 2022.04.24
[백준] 12757 전설의 JBNU - Java  (0) 2022.04.23
[백준] 21939 문제 추천 시스템 Version 1 - Java  (0) 2022.04.23
[백준] 1351 무한수열 - Java  (0) 2022.04.23
[백준] 2002 추월 - Java  (0) 2022.04.23

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • [백준] 1972 놀라운 문자열 - Java

    [백준] 1972 놀라운 문자열 - Java

    2022.04.24
  • [백준] 12757 전설의 JBNU - Java

    [백준] 12757 전설의 JBNU - Java

    2022.04.23
  • [백준] 21939 문제 추천 시스템 Version 1 - Java

    [백준] 21939 문제 추천 시스템 Version 1 - Java

    2022.04.23
  • [백준] 1351 무한수열 - Java

    [백준] 1351 무한수열 - Java

    2022.04.23
다른 글 더 둘러보기

정보

천천히 꾸준히 조용히 블로그의 첫 페이지로 이동

천천히 꾸준히 조용히

  • 천천히 꾸준히 조용히의 첫 페이지로 이동

검색

방문자

  • 전체 방문자
  • 오늘
  • 어제

카테고리

  • 분류 전체보기 (677) N
    • Algorithm (205)
      • Data Structure (5)
      • Theory && Tip (33)
      • Baekjoon (166)
      • ALGOSPOT (1)
    • Spring (123)
      • Spring (28)
      • Spring Web MVC (20)
      • Spring Database (14)
      • Spring Boot (6)
      • Spring 3.1 (11)
      • Spring Batch (6)
      • Spring Security (16)
      • JPA (12)
      • Spring Data JPA (5)
      • QueryDSL (4)
      • eGovFramework (1)
    • Programming Language (74)
      • C (25)
      • C++ (12)
      • Java (19)
      • JavaScript (15)
      • Python (1)
      • PHP (2)
    • Computer Science (142)
      • Machine Learning (38)
      • Operating System (18)
      • Computer Network (28)
      • System Programming (22)
      • Universial Programming Lang.. (8)
      • Computer Architecture (4)
      • Compiler Design (11)
      • Computer Security (13)
    • Database (21)
      • Database (7)
      • MySQL (3)
      • Oracle (3)
      • Redis (5)
      • Elasticsearch (3)
    • DevOps (20)
      • Docker && Kubernetes (8)
      • Jenkins (4)
      • Amazon Web Service (8)
    • Mobile (28)
      • Android (21)
      • Flutter (7)
    • 💡 솔루션 (17)
    • 👥 모각코 (9)
    • 💬 기록 (7)
    • 📚 공부 (6) N
    • -------------- (25)

최근 글

나의 외부 링크

메뉴

  • 홈
반응형

정보

i3months의 천천히 꾸준히 조용히

천천히 꾸준히 조용히

i3months

블로그 구독하기

  • 구독하기
  • RSS 피드

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. Copyright © i3months.

티스토리툴바