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

Partially Committed

[๋ฐฑ์ค€ 5568/2559/1504/11066] ์ž๋ฐ” ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 5568/2559/1504/11066] ์ž๋ฐ”

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

(title: [๋ฐฑ์ค€ 5568/2559/1504/11066] ์ž๋ฐ”)

A. ์นด๋“œ๋†“๊ธฐ BOJ 5568

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

 

5568๋ฒˆ: ์นด๋“œ ๋†“๊ธฐ

์˜ˆ์ œ 1์˜ ๊ฒฝ์šฐ ์ƒ๊ทผ์ด๋Š” 11, 12, 21, 112, 121, 122, 212๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

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


public class Main {
    static int n;
    static int k;
    static TreeSet<String> treeSet = new TreeSet<>();
    static boolean [] visited;
    static int [] cards;
    static int [] arr;
    static void dfs(int depth){
        if(depth==k){
            StringBuilder sb = new StringBuilder();
            for(int i = 0 ; i < k ; i++){
                sb.append(String.valueOf(cards[arr[i]]));
            }
            treeSet.add(sb.toString());
            return;
        } else{
            for(int i = 0 ; i < n ; i++){
                if(!visited[i]){
                    visited[i] = true;
                    arr[depth] = i;
                    dfs(depth+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());
        // n ์žฅ์˜ ์ˆ˜ ์ค‘์—์„œ
        k = Integer.parseInt(br.readLine());
        // k ์žฅ์„ ์„ ํƒํ•˜์—ฌ ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.
        visited = new boolean[n];
        cards = new int[n];
        arr = new int[n];

        for(int i = 0 ; i < n ; i++){
            cards[i] = Integer.parseInt(br.readLine());
        }
        dfs(0);

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

 

 

 

 

B. ์ˆ˜์—ด BOJ 2559

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

 

2559๋ฒˆ: ์ˆ˜์—ด

์ฒซ์งธ ์ค„์—๋Š” ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ N๊ณผ K๊ฐ€ ํ•œ ๊ฐœ์˜ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ •์ˆ˜ N์€ ์˜จ๋„๋ฅผ ์ธก์ •ํ•œ ์ „์ฒด ๋‚ ์งœ์˜ ์ˆ˜์ด๋‹ค. N์€ 2 ์ด์ƒ 100,000 ์ดํ•˜์ด๋‹ค. ๋‘ ๋ฒˆ์งธ ์ •์ˆ˜ K๋Š” ํ•ฉ์„ ๊ตฌํ•˜๊ธฐ

www.acmicpc.net

๋‚œ์ด๋„ Silver 3
ํ’€์ด ์‹œ๊ฐ„ 15 ๋ถ„
๋ถ„๋ฅ˜ ๋ˆ„์ ํ•ฉ + ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ
์‹œ๊ฐ„๋ณต์žก๋„ O(N)
๊ณต๊ฐ„๋ณต์žก๋„  
import java.io.*;
import java.util.*;
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 k = Integer.parseInt(stk.nextToken());

        int [] base = new int[n+1];
        int [] prefix = new int[n+1];
        stk = new StringTokenizer(br.readLine());
        for(int i = 1 ; i <= n; i++){
            base[i] = Integer.parseInt(stk.nextToken());
        }
        prefix[0] = base[1];
        for(int i = 1 ; i<=n; i++){
            prefix[i] = prefix[i-1] + base[i];
        }

        int st = 1;
        int end = k;
        int max = -Integer.MAX_VALUE;
        while(end <= n){
            int sum = prefix[end] - prefix[st-1];
            max = Math.max(sum, max);
            st++;
            end++;
        }
        bw.write(max+"\n");
        bw.flush();
        bw.close();
    }
}

 

 

 

C. ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ BOJ 1504

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

 

1504๋ฒˆ: ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (2 ≤ N ≤ 800, 0 ≤ E ≤ 200,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ a, b, c๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, a๋ฒˆ ์ •์ ์—์„œ b๋ฒˆ ์ •์ ๊นŒ์ง€ ์–‘๋ฐฉํ–ฅ ๊ธธ์ด ์กด

www.acmicpc.net

๋‚œ์ด๋„ Gold 4
ํ’€์ด ์‹œ๊ฐ„ 1์‹œ๊ฐ„
๋ถ„๋ฅ˜ ๋‹ค์ต์ŠคํŠธ๋ผ
์‹œ๊ฐ„๋ณต์žก๋„ O(EogV)
๊ณต๊ฐ„๋ณต์žก๋„  

์ž…๋ ฅ์„ ์ž˜๋ชป ๋ฐ›์•„๋†“๊ณ , 40๋ถ„๋™์•ˆ ๋งž์™œํ‹€ํ–ˆ๋‹ค ๐Ÿ˜ข

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

public class Main {
    static ArrayList<node>[] graph;
    static int n;
    static int e;
    static int inf = 200000*1000+1;

    static class node implements Comparable<node> {
        int to;
        int w;

        public node(int to, int w) {
            this.to = to;
            this.w = w;
        }

        @Override
        public int compareTo(node o) {
            if (this.w > o.w) return -1;
            else return 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;
        stk = new StringTokenizer(br.readLine());
        n = Integer.parseInt(stk.nextToken());
        e = Integer.parseInt(stk.nextToken());
        graph = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int i = 0; i < e; i++) {
            stk = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(stk.nextToken());
            int b = Integer.parseInt(stk.nextToken());
            int c = Integer.parseInt(stk.nextToken());
            graph[a].add(new node(b, c));
            graph[b].add(new node(a, c));
        }
        stk = new StringTokenizer(br.readLine());
        int v1 = Integer.parseInt(stk.nextToken());
        int v2 = Integer.parseInt(stk.nextToken());

        int [] dist1 = new int[n+1];
        int [] dist2 = new int[n+1];
        int [] dist3 = new int[n+1];

        dijkstra(1, n, dist1);
        dijkstra(v1, v2, dist2);
        dijkstra(v2, n, dist3);

        long case1 = dist1[v1] + dist2[v2] + dist3[n];
        // case 2 : 1->v2->v1->n
        long case2 = dist1[v2] + dist3[v1] + dist2[n];
        long ans = Math.min(inf, case1);
        if(dist2[v2]==inf || ans==inf || dist3[n] == inf){
            //v1-n, v2-n ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๊ฒฝ๋กœ๊ฐ€ ์—†๋‹ค๋ฉด
            System.out.println("-1\n");
            return;
        }
        ans = Math.min(ans, case2);
        bw.write(ans+"\n");
        bw.flush();
        bw.close();
    }
    static void dijkstra(int start, int end, int [] dist){
        Arrays.fill(dist, inf);
        dist[start] = 0;

        PriorityQueue<node> pq = new PriorityQueue<>();
        pq.add(new node(start, 0));
        while(!pq.isEmpty()){
            node now = pq.poll();
            for(node cur : graph[now.to]){
                if(dist[cur.to] >= dist[now.to] + cur.w){
                    dist[cur.to] = dist[now.to] + cur.w;
                    pq.add(new node(cur.to, dist[cur.to]));
                }
            }
        }
    }
}

 

 

D. ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ BOJ 11066

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

 

11066๋ฒˆ: ํŒŒ์ผ ํ•ฉ์น˜๊ธฐ

์†Œ์„ค๊ฐ€์ธ ๊น€๋Œ€์ „์€ ์†Œ์„ค์„ ์—ฌ๋Ÿฌ ์žฅ(chapter)์œผ๋กœ ๋‚˜๋ˆ„์–ด ์“ฐ๋Š”๋ฐ, ๊ฐ ์žฅ์€ ๊ฐ๊ฐ ๋‹ค๋ฅธ ํŒŒ์ผ์— ์ €์žฅํ•˜๊ณค ํ•œ๋‹ค. ์†Œ์„ค์˜ ๋ชจ๋“  ์žฅ์„ ์“ฐ๊ณ  ๋‚˜์„œ๋Š” ๊ฐ ์žฅ์ด ์“ฐ์—ฌ์ง„ ํŒŒ์ผ์„ ํ•ฉ์ณ์„œ ์ตœ์ข…์ ์œผ๋กœ ์†Œ์„ค์˜ ์™„์„ฑ๋ณธ

www.acmicpc.net

๋‚œ์ด๋„ Gold3
ํ’€์ด ์‹œ๊ฐ„ 1์‹œ๊ฐ„ ์ •๋„ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์ ํ™”์‹์ด ๋„ˆ๋ฌด ์•ˆ๋– ์˜ฌ๋ผ์„œ, ๊ตฌ๊ธ€๋ง์˜ ๋„์›€์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค ๐Ÿ˜ข
๋ถ„๋ฅ˜ DP
์‹œ๊ฐ„๋ณต์žก๋„ O(N^3)
๊ณต๊ฐ„๋ณต์žก๋„  
[Bottom-up]
import java.io.*;
import java.util.*;

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 tc = Integer.parseInt(br.readLine());
        while (tc > 0) {
            tc--;
            int k = Integer.parseInt(br.readLine());
            int[] arr = new int[k + 1];
            stk = new StringTokenizer(br.readLine());
            for (int i = 1; i <= k; i++) {
                arr[i] = Integer.parseInt(stk.nextToken());
            }

            long ans = solve(arr, k);
            bw.write(ans + "\n");
        }

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

    private static int solve(int[] arr, int k) {
        int[][] dp = new int[k + 1][k + 1];
        int[] prefix = new int[k + 1];


        for (int i = 1; i <= k; i++) {
            prefix[i] = prefix[i - 1] + arr[i];
        } // ๋ˆ„์ ํ•ฉ

        // ํŒŒ์ผ ๊ฐœ์ˆ˜๊ฐ€ ์„ธ ๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ( mid ; ์ค‘๊ฐ„ ์ง€์  )
        // dp[i][j] = min( dp[i][j] , dp[i][mid] + dp[mid+1][j] + sum(i~j) )
        for (int div = 1; div < k; div++) {
            for (int i = 1; i+div <= k; i++) {
                int j = i + div;
                dp[i][j] = Integer.MAX_VALUE;
                for (int mid = i; mid < j; mid++) {
                    dp[i][j] = Math.min(dp[i][j], dp[i][mid] + dp[mid + 1][j] + getSum(i, j, prefix));
                }
            }
        }


        return dp[1][k];
    }

    private static int getSum(int st, int end, int[] prefix) {
        return prefix[end] - prefix[st - 1];
    }
}

[Top-down]

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

public class Main {
    static int[][] dp;
    static int[] arr;
    static int[] prefix;

    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 tc = Integer.parseInt(br.readLine());
        while (tc > 0) {
            tc--;
            int k = Integer.parseInt(br.readLine());
            arr = new int[k + 1];
            stk = new StringTokenizer(br.readLine());
            for (int i = 1; i <= k; i++) {
                arr[i] = Integer.parseInt(stk.nextToken());
            }

            dp = new int[k + 1][k + 1];
            prefix = new int[k + 1];

            for (int i = 1; i <= k; i++) {
                prefix[i] = prefix[i - 1] + arr[i];
            } // ๋ˆ„์ ํ•ฉ

            for(int i = 0 ; i <=k; i++){
                for(int j = 0 ; j <=k; j++){
                    dp[i][j] = Integer.MAX_VALUE;
                }
            }

            long ans = solve_topdown(1, k);
            bw.write(ans + "\n");
        }

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

    private static int solve_topdown(int left, int right) {
        if (dp[left][right] != Integer.MAX_VALUE) {
            return dp[left][right];
        }
        if (left == right) {
            return dp[left][right] = 0;
        }
        if (left + 1 == right) {
            return dp[left][right] = arr[left] + arr[right];
        }
        for (int mid = left; mid < right; ++mid) {
            int nleft = solve_topdown(left, mid);
            int nright = solve_topdown(mid + 1, right);
            dp[left][right] = Math.min(dp[left][right], nleft + nright);
        }
        return dp[left][right] += getSum(left, right, prefix);
    }

    private static int getSum(int st, int end, int[] prefix) {
        return prefix[end] - prefix[st - 1];
    }
}

 

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