문제 링크 : https://www.acmicpc.net/problem/2217
문제 등급 : Silver IV
 

2217번: 로프

N(1 ≤ N ≤ 100,000)개의 로프가 있다. 이 로프를 이용하여 이런 저런 물체를 들어올릴 수 있다. 각각의 로프는 그 굵기나 길이가 다르기 때문에 들 수 있는 물체의 중량이 서로 다를 수도 있다. 하

www.acmicpc.net

문제 해결 로직

  1. 로프의 길이를 오름 차순으로 나열하여 병렬로 세운다.
  2. 가장 작은 로프부터 for문을 돌면서 자기 자신의 길이만큼 남아 있는 루프에서 할당받아 병렬로 물건을 들어 올린다.
    1. 5, 4, 3, 2, 1의 로프가 있다.
    2. 1번의 로프를 활용하면 5개의 로프 모두 1만큼 사용할 수 있다. (1 * 5) = 5
    3. 2번의 로프를 활용하면 4개의 로프를 2만큼 사용할 수 있다. (2 * 4) = 8
    4. 3번의 로프를 활용하면 3개의 로프를 3만큼 사용할 수 있다. (3 * 3) = 9 
    5. 4번의 로프를 활용하면 2개의 로프를 4만큼 사용할 수 있다. (4 * 2) = 8
    6. 5번의 로프를 활용하면 1개의 로프를 5만큼 사용할 수 있다. (5 * 1) = 5
  3. 2번은 반복하면서 가장 큰 무게를 구한다.

 

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

public class 로프 {
    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());

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

        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        });

        int max = Integer.MIN_VALUE;
        for (int i = 0; i < list.size(); i++) {
            int r = list.get(i);

            max = Math.max(max, r * (list.size() - i));
        }

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

1541번: 잃어버린 괄호

첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. 그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다

www.acmicpc.net

문제 해결 로직

  1. 문자열을 '-' 기준으로 자른다.
  2. 자른 배열의 요소를 '+' 기준으로 자른다.
  3. +배열을 하나씩 더하고 - 를 한다.

이 문제는 +를 먼저 하고 -을 나중에 하여 큰 수를 빼주면 가장 작은 수를 구할 수 있는 문제이다.

 

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

public class 잃어버린 괄호 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

        long res = Integer.MAX_VALUE;

        for(int i = 0; i < str.length; i++) {
            String[] s = str[i].split("\\+");
            
            long temp = 0;
            
            for(int j = 0; j < s.length; j++) {
                temp += Long.parseLong(s[j]);
            }
            
            if(res == Integer.MAX_VALUE) {
                res = temp;
            }else {
                res -= temp;
            }
        }

        System.out.println(res);
    }
}
문제 링크 : https://www.acmicpc.net/problem/1931
문제 등급 : Silver II
 

1931번: 회의실 배정

(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.

www.acmicpc.net

문제 해결 로직

  1. 회의가 끝나는 시간을 기준으로 내림 차순 정렬하고 끝나는 시간이 동일하다면 시작 시간을 오름차순으로 정렬한다.
  2. 정렬된 끝나는 시간으로 회의의 수만큼 반복하면서 정렬된 첫 회의 끝나는 시간보다 같거나 큰 시간의 회의 시작시간을 찾아 회의를 진행한다.
  3. 회의의 끝나는 시간보다 먼저 시작하는 회의는 다른 회의실을 사용해야 하므로 제외한다.
  4. 회의를 진행 할때 마다 회의의 진행 수를 Count 한다.

 

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

public class 회의실배정 {
    public static class Node {
        int s, e;
        Node(int s, int e) {
            this.s = s;
            this.e = e;
        }
    }
    public static List<Node> times = new ArrayList<>();
    public static Comparator<Node> compare = new Comparator<Node>() {
        @Override
        public int compare(Node o1, Node o2) {
            return Integer.compare(o1.e, o2.e) == 0 ? Integer.compare(o1.s, o2.s) : Integer.compare(o1.e, o2.e);
        }
    };
    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 s = Integer.parseInt(st.nextToken());
            int e = Integer.parseInt(st.nextToken());

            times.add(new Node(s, e));
        }

        times.sort(compare);

        Node node = times.get(0);

        int cnt = 1;
        for (int i = 1; i < times.size(); i++) {
            Node nextNode = times.get(i);
            if(node.e <= nextNode.s) {
                node = nextNode;
                cnt++;
            }
        }

        System.out.println(cnt);
    }
}

.

문제 링크 : 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);
    }
}

+ Recent posts