Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 2580/14888/13305/10844] JAVA ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 2580/14888/13305/10844] JAVA

WonderJay 2023. 2. 13. 21:18
728x90
๋ฐ˜์‘ํ˜•
SMALL

A. ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ BOJ 14888

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

๋‚œ์ด๋„ Silver 1
ํ’€์ด ์‹œ๊ฐ„ 40 ๋ถ„
๋ถ„๋ฅ˜ ๋ฐฑํŠธ๋ž˜ํ‚น
์‹œ๊ฐ„๋ณต์žก๋„ O(4^n)
๊ณต๊ฐ„๋ณต์žก๋„ O(N+M)
import java.io.*;
import java.util.StringTokenizer;

public class Main {
    static int n;
    static int [] nums;
    static int [] oper = new int [4];
    static StringBuilder sb = new StringBuilder();
    static int min=Integer.MAX_VALUE;
    static int max= Integer.MIN_VALUE;
    static void dfs(int num, int idx){
        if(idx==n){
            min = Math.min(num, min);
            max = Math.max(num, max);
            return;
        } else {
            for(int i = 0 ; i < 4 ; i++){
                if(oper[i]>0){
                    oper[i]--;
                    switch(i){
                        case 0:
                            dfs(num+nums[idx], idx+1);
                            break;
                        case 1:
                            dfs(num-nums[idx], idx+1);
                            break;
                        case 2:
                            dfs(num*nums[idx], idx+1);
                            break;
                        case 3:
                            dfs(num/nums[idx], idx+1);
                            break;
                    }
                    oper[i]++;
                }
            }
        }
    }

    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;

        n = Integer.parseInt(br.readLine());
        nums = new int [n];
        stk = new StringTokenizer(br.readLine());
        for(int i = 0 ; i < n; i ++){
            nums[i] = Integer.parseInt(stk.nextToken());
        }
        stk = new StringTokenizer(br.readLine());
        for(int i = 0 ; i < 4 ; i++){
            oper[i] = Integer.parseInt(stk.nextToken());
        }

        dfs(nums[0], 1);

        bw.write(max+"\n");
        bw.write(min+"\n");

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

 

 

 

 

B. ์Šค๋„์ฟ  BOJ 2580

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

 

2580๋ฒˆ: ์Šค๋„์ฟ 

์Šค๋„์ฟ ๋Š” 18์„ธ๊ธฐ ์Šค์œ„์Šค ์ˆ˜ํ•™์ž๊ฐ€ ๋งŒ๋“  '๋ผํ‹ด ์‚ฌ๊ฐํ˜•'์ด๋ž‘ ํผ์ฆ์—์„œ ์œ ๋ž˜ํ•œ ๊ฒƒ์œผ๋กœ ํ˜„์žฌ ๋งŽ์€ ์ธ๊ธฐ๋ฅผ ๋ˆ„๋ฆฌ๊ณ  ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฐ€๋กœ, ์„ธ๋กœ ๊ฐ๊ฐ 9๊ฐœ์”ฉ ์ด 81๊ฐœ์˜ ์ž‘์€ ์นธ์œผ๋กœ ์ด๋ฃจ

www.acmicpc.net

๋‚œ์ด๋„ Gold 4
ํ’€์ด ์‹œ๊ฐ„ 50 ๋ถ„
๋ถ„๋ฅ˜ ๋ฐฑํŠธ๋ž˜ํ‚น, ๊ตฌํ˜„
์‹œ๊ฐ„๋ณต์žก๋„ O(N!)
๊ณต๊ฐ„๋ณต์žก๋„ O(9*9+9*10*3)
import java.io.*;
import java.util.StringTokenizer;

public class Main {
    static int[][] sudoku = new int[9][9];
    static boolean[][] rowCheck = new boolean[9][10];
    static boolean[][] colCheck = new boolean[9][10];
    static boolean[][] boxCheck = new boolean[9][10];
    static boolean isitdone;

    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    private static void dfs(int k) throws IOException {
        if(isitdone) return;
        int x_pos = k % 9;
        int y_pos = k / 9;
        if (k == 81) {
            isitdone=true;
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 9; j++) {
                    System.out.print(sudoku[i][j]+" ");
                }
                System.out.println();
            }
            return;
        }
        if (sudoku[y_pos][x_pos] == 0) {
            for (int t = 1; t <= 9; t++) {
                if(rowCheck[y_pos][t]==false && colCheck[x_pos][t]==false && boxCheck[(y_pos / 3) * 3 + x_pos / 3][t]==false){
                    rowCheck[y_pos][t] = true;
                    colCheck[x_pos][t] = true;
                    boxCheck[(y_pos / 3) * 3 + x_pos / 3][t] = true;
                    sudoku[y_pos][x_pos] = t;
                    dfs(k + 1);
                    sudoku[y_pos][x_pos] = 0;
                    rowCheck[y_pos][t] = false;
                    colCheck[x_pos][t] = false;
                    boxCheck[(y_pos / 3) * 3 + x_pos / 3][t] = false;
                }
            }
        } else dfs(k+1);
    }

    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;

        for (int i = 0; i < 9; i++) {
            stk = new StringTokenizer(br.readLine());
            for (int j = 0; j < 9; j++) {
                sudoku[i][j] = Integer.parseInt(stk.nextToken());
                rowCheck[i][sudoku[i][j]] = true;
                colCheck[j][sudoku[i][j]] = true;
                boxCheck[(i / 3) * 3 + j / 3][sudoku[i][j]] = true;
            }
        }

        dfs(0);


    }
}

 

 

 

C. ์ฃผ์œ ์†Œ BOJ 13305

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

 

13305๋ฒˆ: ์ฃผ์œ ์†Œ

ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‹ค์Œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ๋„์‹œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ N(2 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” ์ธ์ ‘ํ•œ ๋‘ ๋„์‹œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ๊ธธ์ด๊ฐ€ ์ œ์ผ ์™ผ์ชฝ ๋„๋กœ๋ถ€ํ„ฐ N-1

www.acmicpc.net

๋‚œ์ด๋„ Silver 3
ํ’€์ด ์‹œ๊ฐ„ 15 ๋ถ„
๋ถ„๋ฅ˜ ๊ทธ๋ฆฌ๋””
์‹œ๊ฐ„๋ณต์žก๋„ O(N)
๊ณต๊ฐ„๋ณต์žก๋„ O(N)
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()); // ๋„์‹œ์˜ ๊ฐœ์ˆ˜
        long[] dosi = new long[n];
        long[] dist = new long[n];
        long total_cost = 0;
        stk = new StringTokenizer(br.readLine());
        for (int i = 0; i < n - 1; i++) {
            dist[i] = Long.parseLong(stk.nextToken());
        }
        stk = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            dosi[i] = Long.parseLong(stk.nextToken());
        }

        long min = Long.MAX_VALUE;
        for (int i = 0; i < n-1; i++) {
            min = Math.min(min, dosi[i]);
            total_cost += min*dist[i];
        }
        bw.write(total_cost+"\n");

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

 

 

 

D. ์‰ฌ์šด ๊ณ„๋‹จ ์ˆ˜ BOJ 10844

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

๋‚œ์ด๋„ Silver 1
ํ’€์ด ์‹œ๊ฐ„ 30 ๋ถ„
๋ถ„๋ฅ˜ 2์ฐจ์› DP
์‹œ๊ฐ„๋ณต์žก๋„ O(N)
๊ณต๊ฐ„๋ณต์žก๋„ O(101x10)
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());

        long[][] dp = new long[101][10];

        dp[1][0] = 0;
        for (int i = 1; i <= 9; i++) {
            dp[1][i] = 1;
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j <= 9; j++) {
                if (j == 0) {
                    dp[i][j] = dp[i - 1][j + 1] % 1000000000;
                } else if (j == 9) {
                    dp[i][j] = dp[i - 1][j - 1] % 1000000000;
                } else { // j : 1~8
                    dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1]) % 1000000000;
                }
            }
        }
        long ans = 0;
        for (int i = 0; i <= 9; i++) {
            ans += (dp[n][i] % 1000000000);
        }

        bw.write(ans%1000000000 + "\n");

        bw.flush();
        bw.close();
    }
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments