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

Partially Committed

[๋ฐฑ์ค€ 2798/2231/14889/2606] ์ž๋ฐ” ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 2798/2231/14889/2606] ์ž๋ฐ”

WonderJay 2023. 2. 14. 10:51
728x90
๋ฐ˜์‘ํ˜•
SMALL

A. ๋ธ”๋ž™์žญ BOJ 2798

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

 

2798๋ฒˆ: ๋ธ”๋ž™์žญ

์ฒซ์งธ ์ค„์— ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N(3 ≤ N ≤ 100)๊ณผ M(10 ≤ M ≤ 300,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์นด๋“œ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ์ด ๊ฐ’์€ 100,000์„ ๋„˜์ง€ ์•Š๋Š” ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. ํ•ฉ์ด M์„ ๋„˜์ง€ ์•Š๋Š” ์นด๋“œ 3์žฅ

www.acmicpc.net

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


public class Main {
    static int ans = -1;
    static int m;
    static int n;
    static int [] arr;
    static int [] nums = new int[3];
    static boolean [] visited;
    private static void dfs(int k){
        if(k==3){
            int sum = 0;
            for(int i = 0 ; i < 3; i++){
                sum += nums[i];
            }
            if(sum > m) {
                return;
            } else { // sum <=m
                ans = Math.max(ans, sum);
                return;
            }
        } else {
            for(int i = 0 ; i < n ; i ++){
                if(!visited[i]){
                    visited[i] = true;
                    nums[k] = arr[i];
                    dfs(k+1);
                    visited[i] = false;
                }
            }
        }
    }
    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());
        n = Integer.parseInt(stk.nextToken());
        m = Integer.parseInt(stk.nextToken());

        arr = new int[n];
        visited = new boolean[n];
        stk = new StringTokenizer(br.readLine());
        for(int i = 0 ; i < n ; i ++){
            arr[i] = Integer.parseInt(stk.nextToken());
        }
        dfs(0);
        
        bw.write(ans+"\n");
        bw.flush();
        bw.close();
    }
}

 

 

 

B. ๋ถ„ํ•ดํ•ฉ BOJ 2231

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

 

2231๋ฒˆ: ๋ถ„ํ•ดํ•ฉ

์–ด๋–ค ์ž์—ฐ์ˆ˜ N์ด ์žˆ์„ ๋•Œ, ๊ทธ ์ž์—ฐ์ˆ˜ N์˜ ๋ถ„ํ•ดํ•ฉ์€ N๊ณผ N์„ ์ด๋ฃจ๋Š” ๊ฐ ์ž๋ฆฌ์ˆ˜์˜ ํ•ฉ์„ ์˜๋ฏธํ•œ๋‹ค. ์–ด๋–ค ์ž์—ฐ์ˆ˜ M์˜ ๋ถ„ํ•ดํ•ฉ์ด N์ธ ๊ฒฝ์šฐ, M์„ N์˜ ์ƒ์„ฑ์ž๋ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 245์˜ ๋ถ„ํ•ดํ•ฉ์€ 256(=245+2+4+5)์ด

www.acmicpc.net

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


public class Main {
    static int ans = -1;
    static int m;
    static int n;
    static int [] arr;
    static int [] nums = new int[3];
    static boolean [] visited;
    private static void dfs(int k){
        if(k==3){
            int sum = 0;
            for(int i = 0 ; i < 3; i++){
                sum += nums[i];
            }
            if(sum > m) {
                return;
            } else { // sum <=m
                ans = Math.max(ans, sum);
                return;
            }
        } else {
            for(int i = 0 ; i < n ; i ++){
                if(!visited[i]){
                    visited[i] = true;
                    nums[k] = arr[i];
                    dfs(k+1);
                    visited[i] = false;
                }
            }
        }
    }
    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());
        n = Integer.parseInt(stk.nextToken());

        int ans = n+1;
        for(int i = 1 ; i<=n-1; i++){
            int target = i;
            int save = target;
            int pSum=target;
            while(target>0){
                pSum += target%10;
                target = target/10;
            }
            if(pSum == n){
                ans = Math.min(ans, save);
            }
        }

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

 

 

 

C. ์Šคํƒ€ํŠธ์™€ ๋งํฌ BOJ 14889

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

 

14889๋ฒˆ: ์Šคํƒ€ํŠธ์™€ ๋งํฌ

์˜ˆ์ œ 2์˜ ๊ฒฝ์šฐ์— (1, 3, 6), (2, 4, 5)๋กœ ํŒ€์„ ๋‚˜๋ˆ„๋ฉด ๋˜๊ณ , ์˜ˆ์ œ 3์˜ ๊ฒฝ์šฐ์—๋Š” (1, 2, 4, 5), (3, 6, 7, 8)๋กœ ํŒ€์„ ๋‚˜๋ˆ„๋ฉด ๋œ๋‹ค.

www.acmicpc.net

๋‚œ์ด๋„ Silver 2
ํ’€์ด ์‹œ๊ฐ„ 30 ๋ถ„
๋ถ„๋ฅ˜ ๋ฐฑํŠธ๋ž˜ํ‚น
์‹œ๊ฐ„๋ณต์žก๋„ O(nC_n/2 + n^2)
๊ณต๊ฐ„๋ณต์žก๋„ O(20*20+20*2)

 

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


public class Main {
    static int n;
    static long ans = Integer.MAX_VALUE;
    static int[][] power;
    static int[] entry;
    static boolean[] visited;

    private static void getScore() {
        long ret = 0;

        long A = 0;
        long B = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i; j < n; j++) {
                if (visited[i] == true && visited[j] == true) {
                    A+=(power[i][j]+power[j][i]);
                } else if (visited[i] == false && visited[j] == false) {
                    B+=(power[i][j]+power[j][i]);
                }
            }
        }

        ans = Math.min(ans, Math.abs(A-B));
    }

    private static void dfs(int k, int idx) {
        if (k == n/2) {
            // ์ ์ˆ˜ ๊ณ„์‚ฐ
            getScore();
            return;
        } else {
            for (int i = idx; i < n; i++) {
                if (!visited[i]) {
                    visited[i] = true;
                    dfs(k + 1, i + 1);
                    visited[i] = false;
                }
            }
        }
    }

    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());
        power = new int[n][n];
        visited = new boolean[n];

        for (int i = 0; i < n; i++) {
            stk = new StringTokenizer(br.readLine());
            for (int j = 0; j < n; j++) {
                power[i][j] = Integer.parseInt(stk.nextToken());
            }
        }
        dfs(0, 0);

        bw.write(ans + "\n");
        bw.flush();
        bw.close();
    }
}

 

D. ๋ฐ”์ด๋Ÿฌ์Šค BOJ 2606

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

 

2606๋ฒˆ: ๋ฐ”์ด๋Ÿฌ์Šค

์ฒซ์งธ ์ค„์—๋Š” ์ปดํ“จํ„ฐ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋Š” 100 ์ดํ•˜์ด๊ณ  ๊ฐ ์ปดํ“จํ„ฐ์—๋Š” 1๋ฒˆ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด

www.acmicpc.net

๋‚œ์ด๋„ Silver 3
ํ’€์ด ์‹œ๊ฐ„ 5 ๋ถ„
๋ถ„๋ฅ˜ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
์‹œ๊ฐ„๋ณต์žก๋„ O(V+E)
๊ณต๊ฐ„๋ณต์žก๋„ O(V+E)

 

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class Main {
    static ArrayList<Integer>[] graph;
    static boolean [] visited;
    private static void dfs(int v){
        if(!visited[v])
            visited[v] = true;
        for(int cur : graph[v]){
            // ์—ฐ๊ฒฐ ๋…ธ๋“œ ํƒ์ƒ‰
            if(!visited[cur]){
                visited[cur] = true;
                dfs(cur);
            }
        }
    }
    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()); // ๋…ธ๋“œ์˜ ์ˆ˜
        int m = Integer.parseInt(br.readLine()); // ์—ฃ์ง€์˜ ์ˆ˜

        graph = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++)
            graph[i] = new ArrayList<>();

        for (int i = 0; i < m; i++) {
            stk = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(stk.nextToken());
            int b = Integer.parseInt(stk.nextToken());
            graph[a].add(b);
            graph[b].add(a);
        }

        visited = new boolean[n+1];
        dfs(1);

        int cnt = 0;
        for(int i = 2 ; i<=n; i++){
            if(visited[i]){
                cnt++;
            }
        }
        bw.write(cnt+"\n");
        bw.flush();
        bw.close();
    }
}

 

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments