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

11047번: 동전 0

첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000) 둘째 줄부터 N개의 줄에 동전의 가치 Ai가 오름차순으로 주어진다. (1 ≤ Ai ≤ 1,000,000, A1 = 1, i ≥ 2인 경우에 Ai는 Ai-1의 배수)

www.acmicpc.net

문제 해결 로직

  1. 동전의 종류를 내림차순으로 정렬한다.
  2. 동전을 내림차순으로 하나씩 총금액보다 작은지 비교한다.
  3. 총금액보다 적은 금액으로 몇 번 나눌 수 있는지 계산한다.
  4. 나눈 개수를 Count하고 개수 * 금액을 총금액에서 뺀다.
  5. 2~4번을 반복하여 필요한 동전의 수를 구한다.

 

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

public class 동전0 {
    public static List<Integer> coins = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] commends = br.readLine().split(" ");

        int n = Integer.parseInt(commends[0]);
        int tot = Integer.parseInt(commends[1]);

        for(int i = 0; i < n; i++) {
            int coin = Integer.parseInt(br.readLine());
            coins.add(coin);
        }

        coins.sort(Collections.reverseOrder());

        int cnt = 0;

        for(int i = 0; i < coins.size(); i++) {
            int unit = coins.get(i);

            if(unit <= tot) {
                int div = tot/unit;
                cnt += div;
                tot = tot - (div * unit);
            }
        }

        System.out.println(cnt);
    }
}
문제 링크 : 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);
    }
}

+ Recent posts