문제 링크 : https://www.acmicpc.net/problem/11399
문제 등급 : Silver II
 

11399번: ATM

첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)

www.acmicpc.net

문제 해결 로직

  1. 인출 시간이 작은 사람을 기준으로 정렬한다.
  2. 인출 한 사람의 인출 시간을 더한다.

 

아래와 같이 소스를 공유합니다.

public class ATM {
    public static Comparator<Integer> compare = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return Integer.compare(o1, o2);
        }
    };
    public static List<Integer> list = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        while(st.hasMoreTokens()){
            int person = Integer.parseInt(st.nextToken());
            list.add(person);
        }

        list.sort(compare);

        int sum = 0;
        int max = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
            max += sum;
        }

        System.out.println(max);
    }
}
문제 링크 : https://www.acmicpc.net/problem/2109
문제 등급 : 
 

2109번: 순회강연

한 저명한 학자에게 n(0 ≤ n ≤ 10,000)개의 대학에서 강연 요청을 해 왔다. 각 대학에서는 d(1 ≤ d ≤ 10,000)일 안에 와서 강연을 해 주면 p(1 ≤ p ≤ 10,000)만큼의 강연료를 지불하겠다고 알려왔다.

www.acmicpc.net

문제 해결 프로세스

  1. 강연비와 강연 일자 중 강연비를 기준으로 내림차순 하여 정렬한다.
  2. 내가 강연할 수 있는 일자를 나열 한다.
  3. 가장 강연비가 많은 순서부터 차례대로 강연 일자를 지운다.
  4. 강연비를 더하여 최대 강연비를 구한다.

주요 로직

  1. Lower Bound를 이용하여 내가 강연할 수 있는 날짜를 하나씩 지워가면서 강연비를 얻어야 한다.
    1. Set을 이용하여 내가 강연할 수 있는 날짜를 나열한다.
    2. 강연비가 가장 많은 강연의 강연 일자를 set.lower(강연일+1)하여 강연할 수 있는 가장 가까운 일자를 찾는다.
    3. set.remove(강연 당일)을 이용하여 강연한 일자는 제거하고 강연비를 더한다.

 

아래와 같이 소스를 공유합니다.

public class 순회강연 {
    public static class Node {
        int d, p;
        Node(int d, int p) {
            this.d = d;
            this.p = p;
        }
    }
    public static int amount = 0;
    public static List<Node> list = new ArrayList<>();
    public static TreeSet<Integer> set = new TreeSet<>();
    public static Comparator<Node> compare = new Comparator<Node>() {
        @Override
        public int compare(Node o1, Node o2) {
            return Integer.compare(o2.p, o1.p);
        }
    };
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());
        int maxDeadLine = Integer.MIN_VALUE;
        for(int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int p = Integer.parseInt(st.nextToken());
            int d = Integer.parseInt(st.nextToken());
            list.add(new Node(d, p));
            maxDeadLine = Math.max(maxDeadLine, d);
        }

        list.sort(compare);

        for (int i = 0; i <= maxDeadLine; i++) set.add(i+1);
        for (int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            Integer idx = set.lower(node.d+1);
            if(idx != null) {
                set.remove(idx);
                amount += node.p;
            }
        }

        System.out.println(amount);
    }
}

 

문제 링크 : https://www.acmicpc.net/problem/13904
문제 등급 : 제공하지 않음
 

13904번: 과제

예제에서 다섯 번째, 네 번째, 두 번째, 첫 번째, 일곱 번째 과제 순으로 수행하고, 세 번째, 여섯 번째 과제를 포기하면 185점을 얻을 수 있다.

www.acmicpc.net

문제 해결 프로세스

  1. 마감일과 점수 중 점수를 기준으로 내림차순으로 정렬한다.
  2. Set을 이용하여 해결할 수 있는 과제를 해결하고 점수를 획득한다.
    1. Set으로 마감일을 마감일의 최댓값 + 1을 포함하여 하나씩 입력한다.
    2. Set의 lower함수를 이용하여 가장 높은 점수의 과제 마감일부터 내림차순으로 찾는다.
    3. lower함수를 이용하면 현재 과제의 마감일보다 작은 마감일을 리턴 받는다.
    4. 현재 과제 마감일보다 작은 마감일에 과제를 해결하고 점수를 획득한다.
  3. 획득한 최고 점수를 출력한다.

주요 로직

  1. Set의 lower함수는 lower bound를 이용한 찾고자 하는 값보다 작은 값을 리턴하는 함수이다.
    1. Set에 1, 2, 3, 4, 5, 6이 있을 때 set.lower(5)를 하면 4를 리턴해준다.
  2. 찾은 마감일(4)을 remove함수로 삭제하고 현재 과제의 점수를 더한다.

아래와 같이 소스를 공유합니다.

public class 과제 {
    public static class Node {
        int d, p;
        Node(int d, int p) {
            this.d = d;
            this.p = p;
        }
    }
    public static int point = 0;
    public static List<Node> list = new ArrayList<>();
    public static TreeSet<Integer> set = new TreeSet<>();
    public static Comparator<Node> compare = new Comparator<Node>() {
        @Override
        public int compare(Node o1, Node o2) {
            return Integer.compare(o2.p, o1.p);
        }
    };

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader((System.in)));

        int n = Integer.parseInt(br.readLine());
        int max = Integer.MIN_VALUE;

        for(int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int d = Integer.parseInt(st.nextToken());
            int p = Integer.parseInt(st.nextToken());

            list.add(new Node(d, p));
            max = Math.max(max, d);
        }

        list.sort(compare);

        for(int i = 0; i <= max; i++)  set.add(i+1);
        for(int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            // int type으로 lower를 받았을 경우 NullPointException이 발생하여 
            // Class로 받아 null인지 체크
            Integer idx = set.lower(node.d+1);
            if(idx != null) {
                set.remove(idx);
                point += node.p;
            }
        }

        System.out.println(point);
    }
}
문제 링크 : https://www.acmicpc.net/problem/1374
등급 : Gold IV
 

1374번: 강의실

첫째 줄에 강의의 개수 N(1≤N≤100,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 줄마다 세 개의 정수가 주어지는데, 순서대로 강의 번호, 강의 시작 시간, 강의 종료 시간을 의미한다. 강의 번

www.acmicpc.net

문제 해설

강의의 개수가 최대 100,000개이고 강의 시간은 최대 10억이다. 이 문제는 모든 강의를 정렬하면 시간 초과가 발생하는 문제이다. 그래서 모든 강의를 정렬하지 않고 이전 강의의 끝 시간과 다음 강의의 시작 시간을 비교하여 로직을 구현한다. 아래는 로직의 순서를 나열하였다.

  1. 강의의 시작 시간 순으로 정렬한다.
  2. 첫 강의가 끝나고 다음 강의가 시작된다면 다음 강의를 제거한다.
  3. 첫 강의가 끝나기 전에 강의가 시작된다면 강의실을 추가하고 강의실 개수를 갱신한다.
  4. 2번과 3번을 모든 강의가 진행될 때까지 반복한다.

아래와 같이 소스를 공유합니다.

public class 강의실 {
    public static class Node {
        int o, s, e;
        Node(int o, int s, int e) {
            this.o = o;
            this.s = s;
            this.e = e;
        }
    }
    public static List<Node> list = new ArrayList<>();
    public static PriorityQueue<Integer> que = new PriorityQueue<>();
    public static Comparator<Node> comparator = new Comparator<>() {
        @Override
        public int compare(Node o1, Node o2) {
            return Integer.compare(o1.s, o2.s);
        }
    };
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        for(int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int o = Integer.parseInt(st.nextToken());
            int s = Integer.parseInt(st.nextToken());
            int e = Integer.parseInt(st.nextToken());

            list.add(new Node(o, s, e));
        }

        Collections.sort(list, comparator);

        int max = Integer.MIN_VALUE;

        for(int i = 0; i < n; i++) {
            while(!que.isEmpty() && que.peek() <= list.get(i).s) {
                que.poll();
            }

            que.add(list.get(i).e);
            max = Math.max(max, que.size());
        }

        System.out.println(max);
    }
}

+ Recent posts