매일 Algorithm

신재원·2023년 3월 11일
0

Algorithm

목록 보기
62/243

백준 22864번 (그리디)

import java.util.Scanner;

public class problem178 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int time = 24;

        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        int d = in.nextInt();

        int count = 0;
        int max = 0;

        while (time != 0) {
            // 피로도가 번아웃이 온경우
            if(a > d){
                break;
            } else if(max + a <= d){
                count += b;
                max += a;
            }else{
                max -= c;
                // 현재 피로도가 음수일 경우
                if (max < 0){
                    max = 0;
                }
            }
            time--;
        }
        System.out.print(count);
    }
}

백준 1049번 (그리디)

import java.util.Arrays;
import java.util.Scanner;

public class problem179 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        int cut = in.nextInt();
        int n = in.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt(); // 패키지 가격
            b[i] = in.nextInt(); // 낱개당 가격
        }

        Arrays.sort(a);
        Arrays.sort(b);

        int min = Integer.MAX_VALUE;

        // 가장 싼 패키지 vs 가장 싼 낱개 vs (가장 싼 패키지 + 가장 싼 낱개)

        // +1 해주는 이유는 6개 (패키지) 이하의 
        // 갯수가 입력될수있음 (최대 패키지사용)
        min = Math.min(((cut / 6) + 1) * a[0], cut * b[0]);

        // +1을 안해주는 이유는 위 수식에서 한번 걸렀기때문 
        // (최소 패키지 + 나머지는 낱개로)
        min = Math.min(min, ((cut / 6) * a[0] + (cut % 6) * b[0]));

        System.out.print(min);

    }
}

백준 2437번 (그리디)

import java.util.Arrays;
import java.util.Scanner;

public class problem180 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int size = in.nextInt();

        int[] arr = new int[size];

        int sum = 0;
        for (int i = 0; i < size; i++) {
            arr[i] = in.nextInt();

        }
        Arrays.sort(arr);
        for(int j = 0 ; j < size; j++){
            // 1 1 2 3 6 7 30 입력값이면 
            // 차례대로 더하면 추 를 다사용하고 21에서 값을 만들어내지 못한다.
            if (sum + 1 < arr[j]) {
                break;
            }
            sum += arr[j];
        }

        System.out.print(sum + 1);
    }
}

백준 2012번 (그리디)


import java.util.Arrays;
import java.util.Scanner;

public class problem181 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int size = in.nextInt();

        int[] arr = new int [size];

        for(int i = 0; i < size; i++){
            arr[i] = in.nextInt();
        }

        Arrays.sort(arr);
        
/**
문제에서 N의 범위가 500,000 인데, 만약 모든 학생이 자신의 등수를 1로 예상할경우,

|1-500,000| x 500,500 이 되어서 int의 범위를 벗어난다.
long 으로 선언
**/
        long sum = 0;

        // 절대값 = Math.abs();
        for(int j = 0; j < arr.length; j++){
            sum += Math.abs(arr[j] - (j + 1));
        }
        System.out.print(sum);
    }
}

0개의 댓글