Notice
Recent Posts
Recent Comments
Today
Total
01-11 01:45
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 7568/1904/1018/25682/2110] ์ž๋ฐ” ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 7568/1904/1018/25682/2110] ์ž๋ฐ”

WonderJay 2023. 2. 15. 16:53
728x90
๋ฐ˜์‘ํ˜•
SMALL

(title: [๋ฐฑ์ค€ 7568/1904/1018/25682/2110] ์ž๋ฐ”)

A. ๋ฉ์น˜ BOJ 7568 

https://www.acmicpc.net/problem/7568

 

7568๋ฒˆ: ๋ฉ์น˜

์šฐ๋ฆฌ๋Š” ์‚ฌ๋žŒ์˜ ๋ฉ์น˜๋ฅผ ํ‚ค์™€ ๋ชธ๋ฌด๊ฒŒ, ์ด ๋‘ ๊ฐœ์˜ ๊ฐ’์œผ๋กœ ํ‘œํ˜„ํ•˜์—ฌ ๊ทธ ๋“ฑ์ˆ˜๋ฅผ ๋งค๊ฒจ๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์–ด๋–ค ์‚ฌ๋žŒ์˜ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ x kg์ด๊ณ  ํ‚ค๊ฐ€ y cm๋ผ๋ฉด ์ด ์‚ฌ๋žŒ์˜ ๋ฉ์น˜๋Š” (x, y)๋กœ ํ‘œ์‹œ๋œ๋‹ค. ๋‘ ์‚ฌ๋žŒ A ์™€ B์˜ ๋ฉ

www.acmicpc.net

๋‚œ์ด๋„ Silver 5
ํ’€์ด ์‹œ๊ฐ„ 9 ๋ถ„
๋ถ„๋ฅ˜ ๋ธŒ๋ฃจํŠธํฌ์Šค, ๊ตฌํ˜„
์‹œ๊ฐ„๋ณต์žก๋„ O(N^2)
๊ณต๊ฐ„๋ณต์žก๋„ O(50+50)
import java.io.*;
import java.util.StringTokenizer;


public class Main {
    private static class info {
        int w;
        int h;
        int idx;

        public info(int w, int h) {
            this.w = w;
            this.h = h;
        }
    }

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        int n = Integer.parseInt(br.readLine());
        info[] arr = new info[n];
        int[] rank = new int[n];

        for (int i = 0; i < n; i++) {
            stk = new StringTokenizer(br.readLine());
            int w = Integer.parseInt(stk.nextToken());
            int h = Integer.parseInt(stk.nextToken());
            arr[i] = new info(w,h);
        }
        for(int i = 0 ; i < n ; i++){
            int cnt = 0;
            info now = arr[i];
            for(int j = 0; j<n; j++){
                info cur = arr[j];
                if(now.w<cur.w&&now.h<cur.h){
                    cnt++;
                }
            }
            rank[i] = cnt+1;
        }

        for(int i = 0; i <n; i++){
            bw.write(rank[i]+" ");
        }

        bw.flush();
        bw.close();
    }
}

 

 

B. 01ํƒ€์ผ BOJ 1904

https://www.acmicpc.net/problem/1904

 

1904๋ฒˆ: 01ํƒ€์ผ

์ง€์›์ด์—๊ฒŒ 2์ง„ ์ˆ˜์—ด์„ ๊ฐ€๋ฅด์ณ ์ฃผ๊ธฐ ์œ„ํ•ด, ์ง€์›์ด ์•„๋ฒ„์ง€๋Š” ๊ทธ์—๊ฒŒ ํƒ€์ผ๋“ค์„ ์„ ๋ฌผํ•ด์ฃผ์…จ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ๊ฐ์˜ ํƒ€์ผ๋“ค์€ 0 ๋˜๋Š” 1์ด ์“ฐ์—ฌ ์žˆ๋Š” ๋‚ฑ์žฅ์˜ ํƒ€์ผ๋“ค์ด๋‹ค. ์–ด๋Š ๋‚  ์ง“๊ถ‚์€ ๋™์ฃผ๊ฐ€ ์ง€์›์ด

www.acmicpc.net

๋‚œ์ด๋„ Silver 3
ํ’€์ด ์‹œ๊ฐ„ 20 ๋ถ„
๋ถ„๋ฅ˜ DP
์‹œ๊ฐ„๋ณต์žก๋„ O(N)
๊ณต๊ฐ„๋ณต์žก๋„ O(1000000)
import java.io.*;
import java.util.StringTokenizer;


public class Main {
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;
        int n = Integer.parseInt(br.readLine());
        final int mod = 15746;
        long [] dp = new long[n+1];

        if(n==1){
            bw.write(1+"\n");
            bw.flush();
            bw.close();
            return;
        }

        dp[1] = 1;
        dp[2] = 2;
        for(int i = 3; i<=n; i++){
            dp[i] = (dp[i-1] + dp[i-2])%mod;
        }

        bw.write((dp[n])%mod+"\n");


        bw.flush();
        bw.close();
    }
}

 

 

 

C. ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ BOJ 1018

https://www.acmicpc.net/problem/1018

 

1018๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 8๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ณด๋“œ์˜ ๊ฐ ํ–‰์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. B๋Š” ๊ฒ€์€์ƒ‰์ด๋ฉฐ, W๋Š” ํฐ์ƒ‰์ด๋‹ค.

www.acmicpc.net

๋‚œ์ด๋„ Silver 4
ํ’€์ด ์‹œ๊ฐ„ 50 ๋ถ„
๋ถ„๋ฅ˜ ๊ตฌํ˜„
์‹œ๊ฐ„๋ณต์žก๋„ O(N^4)
๊ณต๊ฐ„๋ณต์žก๋„ O(50*50)

 

import java.io.*;
import java.util.StringTokenizer;


public class Main {
    static int n;
    static int m;
    static int[][] board;
    static final int black = 1;
    static final int white = 0;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        stk = new StringTokenizer(br.readLine());
        int row = Integer.parseInt(stk.nextToken());
        int col = Integer.parseInt(stk.nextToken());


        board = new int[row][col];
        for (int i = 0; i < row; i++) {
            char[] in = br.readLine().toCharArray();
            for (int j = 0; j < col; j++) {
                if (in[j] == 'W') {
                    board[i][j] = 0;
                } else {
                    board[i][j] = 1;
                }
            }
        }

        int count = 0; int ans = Integer.MAX_VALUE;
        for (int i = 0; i < row-7; i++) {
            for (int j = 0; j < col-7; j++) { // ์‹œ์ž‘์  ํƒ์ƒ‰
                // (j, i) ์— ๋Œ€ํ•˜์—ฌ
                count = 0;

                int leftTop = board[i][j];
                for (int y = i; y < i + 8; y++) {
                    for (int x = j; x < j + 8; x++) {
                        if((x+y)%2==0){
                            // leftTop ๊ณผ ์ƒ‰์ด ๋™์ผํ•ด์•ผํ•จ
                            if(leftTop != board[y][x]){
                                // ๊ทผ๋ฐ ๋‹ค๋ฅด๋ฉด?
                                count++;
                            }
                        } else{
                            // leftTop ๊ณผ ์ƒ‰์ด ๋‹ฌ๋ผ์•ผ ํ•จ
                            if(leftTop==board[y][x]){
                                // ๊ทผ๋ฐ ๊ฐ™์œผ๋ฉด?
                                count++;
                            }
                        }
                    }
                }


                int rev = 64-count;

                ans = Math.min(Math.min(count, rev), ans);
            }
        }
        bw.write(ans + "\n");
        bw.flush();
        bw.close();
    }
}

 

 

D. ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ2 BOJ 25682

https://www.acmicpc.net/problem/25682

 

25682๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ 2

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N, M, K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ณด๋“œ์˜ ๊ฐ ํ–‰์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. B๋Š” ๊ฒ€์€์ƒ‰์ด๋ฉฐ, W๋Š” ํฐ์ƒ‰์ด๋‹ค.

www.acmicpc.net

๋‚œ์ด๋„ Gold 5
ํ’€์ด ์‹œ๊ฐ„ 50 ๋ถ„
๋ถ„๋ฅ˜ ๋ˆ„์ ํ•ฉ
์‹œ๊ฐ„๋ณต์žก๋„ O(NM)
๊ณต๊ฐ„๋ณต์žก๋„ O(2000*2000*2)
 
import java.io.*;
import java.util.StringTokenizer;


public class Main {
    static int [][] dp;
    static int[][] board;
    static int row;
    static int col;
    static int k;

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        stk = new StringTokenizer(br.readLine());
        row = Integer.parseInt(stk.nextToken());
        col = Integer.parseInt(stk.nextToken());
        k = Integer.parseInt(stk.nextToken());

        board = new int[row][col];

        for (int i = 0; i < row; i++) {
            char[] in = br.readLine().toCharArray();
            for (int j = 0; j < col; j++) {
                board[i][j] = in[j];
            }
        }

        int ans = Math.min(getCnt('W'), getCnt('B'));

        bw.write(ans+"\n");
        bw.flush();
        bw.close();
    }
    private static int getCnt(int color){
        dp = new int[row+1][col+1];
        int ret = Integer.MAX_VALUE;
        int val = 0;
        for(int i = 0 ; i < row; i++){
            for(int j = 0 ; j < col; j++){
                if((i+j)%2==0){
                    val = color!=board[i][j] ? 1 : 0;
                } else {
                    val = color==board[i][j] ? 1 : 0;
                }
                dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j] - dp[i][j] + val;
            } // calc 2-d prefix
        }

        for(int i = 1 ; i <= row-k+1; i++){
            for(int j = 1; j <= col-k+1; j++){
                ret = Math.min(ret, dp[i+k-1][j+k-1] - dp[i+k-1][j-1] - dp[i-1][j+k-1] + dp[i-1][j-1]);
            }
        }

        return ret;
    }
}

 

E. ๊ณต์œ ๊ธฐ ์„ค์น˜ BOJ 2110

https://www.acmicpc.net/problem/2110

 

2110๋ฒˆ: ๊ณต์œ ๊ธฐ ์„ค์น˜

์ฒซ์งธ ์ค„์— ์ง‘์˜ ๊ฐœ์ˆ˜ N (2 ≤ N ≤ 200,000)๊ณผ ๊ณต์œ ๊ธฐ์˜ ๊ฐœ์ˆ˜ C (2 ≤ C ≤ N)์ด ํ•˜๋‚˜ ์ด์ƒ์˜ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘์˜ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” xi (0 ≤ xi ≤ 1,000,000,000)๊ฐ€

www.acmicpc.net

๋‚œ์ด๋„ Gold 4
ํ’€์ด ์‹œ๊ฐ„ 20๋ถ„
๋ถ„๋ฅ˜ parametric search, ์ด๋ถ„ ํƒ์ƒ‰
์‹œ๊ฐ„๋ณต์žก๋„ O(nlogn)
๊ณต๊ฐ„๋ณต์žก๋„ O(200000)
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;


public class Main {

    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;

        stk = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(stk.nextToken());
        int c = Integer.parseInt(stk.nextToken());

        int[] house = new int[n];
        for (int i = 0; i < n; i++) {
            house[i] = Integer.parseInt(br.readLine());
        }
        Arrays.sort(house);

        int low = 1;
        int high = house[n - 1] - house[0] + 1;
        int mid = 0;
        int cnt = 0;


        while (low < high) {
            mid = (low + high) / 2;
            cnt = getCnt(mid, house); // mid ๊ฐ„๊ฒฉ์œผ๋กœ ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ๊ณต์œ ๊ธฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐ
            if (cnt < c) {
                // mid ๊ฐ„๊ฒฉ์œผ๋กœ ์„ค์น˜ํ•˜๋ฉด, ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณต์œ ๊ธฐ๋ณด๋‹ค ๋” ์ ๊ฒŒ ์„ค์น˜ํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค๋ฉด
                // ๊ฐ„๊ฒฉ์„ ์ค„์—ฌ์•ผ ํ•œ๋‹ค.
                high = mid;
            } else {
                // mid ๊ฐ„๊ฒฉ์œผ๋กœ ์„ค์น˜ํ•˜๋ฉด, ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณต์œ ๊ธฐ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฑฐ๋‚˜
                // ๋” ํ•„์š”ํ•œ ์ƒํ™ฉ์ด๋ผ๋ฉด ๊ฐ„๊ฒฉ์„ ๋Š˜๋ ค์•ผ ํ•œ๋‹ค.
                low = mid + 1;
            }
        }

        bw.write((low - 1) + "\n");
        bw.flush();
        bw.close();
    }

    private static int getCnt(int diff, int[] house) {
        int ret = 0;
        int cnt = 1;
        int t = house[0];
        for (int i = 1; i < house.length; i++) {
            if (diff <= house[i] - t) {
                cnt++;
                t = house[i];
            }
        }
        return ret = cnt;
    }
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments