관리 메뉴

Partially Committed

[λ°±μ€€ 1436/10815/11054/16139/16928] μžλ°” λ³Έλ¬Έ

πŸ”₯ Algorithm || λ¬Έμ œν’€μ΄/PS

[λ°±μ€€ 1436/10815/11054/16139/16928] μžλ°”

WonderJay 2023. 2. 16. 14:30
728x90
λ°˜μ‘ν˜•
SMALL

(title: [λ°±μ€€ 1436/10815/11054/16139/16928] μžλ°”)

A. μ˜ν™”κ°λ…  BOJ 1436

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

 

1436번: μ˜ν™”κ°λ… 숌

666은 쒅말을 λ‚˜νƒ€λ‚΄λŠ” 수라고 ν•œλ‹€. λ”°λΌμ„œ, λ§Žμ€ λΈ”λ‘λ²„μŠ€ν„° μ˜ν™”μ—μ„œλŠ” 666이 λ“€μ–΄κ°„ 제λͺ©μ„ 많이 μ‚¬μš©ν•œλ‹€. μ˜ν™”κ°λ… μˆŒμ€ μ„Έμƒμ˜ 쒅말 μ΄λΌλŠ” μ‹œλ¦¬μ¦ˆ μ˜ν™”μ˜ 감독이닀. 쑰지 λ£¨μΉ΄μŠ€λŠ” μŠ€νƒ€μ›Œ

www.acmicpc.net

λ‚œμ΄λ„ Silver 5
풀이 μ‹œκ°„ 10 λΆ„
λΆ„λ₯˜ κ΅¬ν˜„
μ‹œκ°„λ³΅μž‘λ„ 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());

        int cnt = 0; long ans = 1; String answer = "";
        while(cnt!=n){
            String str = String.valueOf(ans);
            if(str.contains("666")){
                cnt++;
                answer = str;
            }
            ans++;
        }
        bw.write(answer+"\n");

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

}

 

 

 

B. μˆ«μžμΉ΄λ“œ BOJ 10815

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

 

10815번: 숫자 μΉ΄λ“œ

첫째 쀄에 상근이가 가지고 μžˆλŠ” 숫자 μΉ΄λ“œμ˜ 개수 N(1 ≤ N ≤ 500,000)이 주어진닀. λ‘˜μ§Έ μ€„μ—λŠ” 숫자 μΉ΄λ“œμ— μ ν˜€μžˆλŠ” μ •μˆ˜κ°€ 주어진닀. 숫자 μΉ΄λ“œμ— μ ν˜€μžˆλŠ” μˆ˜λŠ” -10,000,000보닀 ν¬κ±°λ‚˜ κ°™κ³ , 10,

www.acmicpc.net

λ‚œμ΄λ„ Silver 5
풀이 μ‹œκ°„ 5λΆ„
λΆ„λ₯˜ Binary Search
μ‹œκ°„λ³΅μž‘λ„ O(nlogn)
κ³΅κ°„λ³΅μž‘λ„ O(n)
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;

        int n = Integer.parseInt(br.readLine());
        int[] card = new int[n];
        stk = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            card[i] = Integer.parseInt(stk.nextToken());
        }
        Arrays.sort(card);
        int m = Integer.parseInt(br.readLine());
        stk = new StringTokenizer(br.readLine());
        for(int i = 0 ; i < m ; i++){
            int target= Integer.parseInt(stk.nextToken());
            if(binary_search(card, target)!=-1)
                bw.write("1 ");
            else bw.write("0 ");
        }
        bw.flush();
        bw.close();
    }

    private static int binary_search(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;
        int midVal = 0;
        while (left <= right) {
            mid = (left + right) / 2;
            midVal = arr[mid];
            if(midVal < target){
                left = mid+1;
            } else if(midVal > target){
                right = mid-1;
            } else { // midVal == target
                return mid;
            }
        }
        return -1;
    }
}

 

 

 

C. κ°€μž₯ κΈ΄ 바이토닉 λΆ€λΆ„ μˆ˜μ—΄ BOJ 11054

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

 

11054번: κ°€μž₯ κΈ΄ 바이토닉 λΆ€λΆ„ μˆ˜μ—΄

첫째 쀄에 μˆ˜μ—΄ A의 크기 N이 주어지고, λ‘˜μ§Έ μ€„μ—λŠ” μˆ˜μ—΄ Aλ₯Ό 이루고 μžˆλŠ” Aiκ°€ 주어진닀. (1 ≤ N ≤ 1,000, 1 ≤ Ai ≤ 1,000)

www.acmicpc.net

λ‚œμ΄λ„ Gold 4
풀이 μ‹œκ°„ 1μ‹œκ°„
λΆ„λ₯˜ DP, LIS
μ‹œκ°„λ³΅μž‘λ„ O(N^2)
κ³΅κ°„λ³΅μž‘λ„ O(1000*2)
import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
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;

        int n = Integer.parseInt(br.readLine()); // μˆ˜μ—΄μ˜ 크기가 주어진닀.
        int[] arr = new int[n+1];
        int[] dp = new int[n+1];
        for (int i = 0; i <= n; i++) {
            dp[i] = 1;
        }
        stk = new StringTokenizer(br.readLine());
        for (int i = 1; i <= n; i++) {
            arr[i] = Integer.parseInt(stk.nextToken());
        }

        int max = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < i; j++) {
                if (arr[j] < arr[i]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
        }

        int[] revdp = new int[n+1];
        for (int i = 0; i <= n; i++) {
            revdp[i] = 1;
        }

        max = 0;
        for (int i = n; i >= 0; i--) {
            for (int j = n; j > i; j--) {
                if (arr[j] < arr[i]) {
                    revdp[i] = Math.max(revdp[i], revdp[j] + 1);
                }
            }
        }

        int ans = 0;
        for(int i = 1; i<=n; i++){
            ans = Math.max(ans, dp[i]+revdp[i]);
        }

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

}

 

D. μΈκ°„-컴퓨터 μƒν˜Έμž‘μš© BOJ 16139

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

 

16139번: 인간-컴퓨터 μƒν˜Έμž‘μš©

첫 쀄에 λ¬Έμžμ—΄ $S$κ°€ 주어진닀. λ¬Έμžμ—΄μ˜ κΈΈμ΄λŠ” $200,000$자 μ΄ν•˜μ΄λ©° μ•ŒνŒŒλ²³ μ†Œλ¬Έμžλ‘œλ§Œ κ΅¬μ„±λ˜μ—ˆλ‹€. 두 번째 μ€„μ—λŠ” 질문의 수 $q$κ°€ 주어지며, 문제의 μˆ˜λŠ” $1\leq q\leq 200,000$을 λ§Œμ‘±ν•œλ‹€. μ„Έ 번째

www.acmicpc.net

λ‚œμ΄λ„ Silver 1
풀이 μ‹œκ°„ 30 λΆ„
λΆ„λ₯˜ λˆ„μ ν•©
μ‹œκ°„λ³΅μž‘λ„ O(N*26)  ~= O(N)
κ³΅κ°„λ³΅μž‘λ„ O(26*200001)
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;

        String input = br.readLine();
        int [][] dp = new int[26][200001];
        int [][] arr = new int[26][200001];
        for(int i = 0 ; i < input.length(); i++){
            int idx = input.charAt(i)-'a';
            arr[idx][i]++;
        }

        for(int i = 0; i<26; i++){
            dp[i][0] = arr[i][0];
            for(int j = 1; j<=input.length(); j++){
                dp[i][j] = dp[i][j-1] + arr[i][j-1];
            }
        }

        int q = Integer.parseInt(br.readLine());
        while(q>0){
            q--;
            stk = new StringTokenizer(br.readLine());
            String st = stk.nextToken();

            int idx = st.charAt(0)-'a';
            int left = Integer.parseInt(stk.nextToken());
            int right = Integer.parseInt(stk.nextToken());
            int sum = dp[idx][right+1] - dp[idx][left];
            bw.write(sum+"\n");
        }

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

}

 

E. λ±€κ³Ό 사닀리 κ²Œμž„ BOJ 16928

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

λ‚œμ΄λ„ Gold 5
풀이 μ‹œκ°„ 50λΆ„
λΆ„λ₯˜ κ·Έλž˜ν”„ 탐색(DFS/BFS)
μ‹œκ°„λ³΅μž‘λ„ O(V+E=200)
κ³΅κ°„λ³΅μž‘λ„ .
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;


public class Main {
    static int[] info;
    static int[] dist;

    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;

        info = new int[101];
        dist = new int[101];

        stk = new StringTokenizer(br.readLine());
        int ladder = Integer.parseInt(stk.nextToken());
        int snake = Integer.parseInt(stk.nextToken());

        for (int i = 0; i < ladder + snake; i++) {
            stk = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(stk.nextToken());
            int end = Integer.parseInt(stk.nextToken());
            info[start] = end;
        }

        Queue<Node> q = new LinkedList<>();

        q.add(new Node(1, 0));
        while (!q.isEmpty()) {
            Node now_node = q.poll();
            int pos = now_node.pos;
            int cnt = now_node.cnt;

            for (int i = 1; i <= 6; i++) {
                int next_pos = pos+i;
                if(next_pos==100){
                    // 도착지점에 λ„λ‹¬ν•œ 경우
                    System.out.println(cnt+1);
                    return;
                } else if(next_pos<100){
                    while(info[next_pos]!=0){
                        // μ—°κ²°λœ 지점을 탐색
                        // 있으면 거기둜 이동
                        next_pos = info[next_pos];
                    }
                    if(dist[next_pos]==0){ // 처음 λ°©λ¬Έν•œ 곳이면
                        q.add(new Node(next_pos, cnt+1));
                        dist[next_pos] = 1; // λ°©λ¬Έ
                    }
                }
            }
        }
//        bw.flush();
//        bw.close();
    }

    static class Node {
        int pos;
        int cnt;

        public Node(int pos, int cnt) {
            this.pos = pos;
            this.cnt = cnt;
        }
    }
}
728x90
λ°˜μ‘ν˜•
LIST
Comments