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

Partially Committed

[CH01] ๊ตฌ๊ฐ„ํ•ฉ ๋ณธ๋ฌธ

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

[CH01] ๊ตฌ๊ฐ„ํ•ฉ

WonderJay 2022. 9. 9. 13:04
728x90
๋ฐ˜์‘ํ˜•
SMALL

Algorithm || DataStructure

#01. ๊ตฌ๊ฐ„ํ•ฉ

Date : 2022/09/09

ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ์„ ์‹œ, ์ง€์ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


1. ๋ฐฐ์—ด๊ณผ ๋ฆฌ์ŠคํŠธ

๋ฐฐ์—ด์˜ ํŠน์ง•

  • ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์›์†Œ์— O(1) ์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ์ƒˆ๋กœ์šด ๊ฐ’ ์‚ฝ์ž… ๋ฐ ํŠน์ • ์œ„์น˜์˜ ๊ฐ’ ์‚ญ์ œ๊ฐ€ ๋น„ํšจ์œจ์ . (๋•ก๊ธฐ๊ฑฐ๋‚˜ ๋ฐ€์–ด์ค˜์•ผํ•จ)
  • ์„ ์–ธํ•  ๋•Œ ํ•œ๋ฒˆ ์ง€์ •ํ•œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ผ ์ˆ˜ ์—†๋‹ค.

๋ฆฌ์ŠคํŠธ์˜ ํŠน์ง•

  • ๊ฐ’์— ์ ‘๊ทผํ•˜๋ ค๋ฉด O(N) ์ด ํ•„์š”ํ•จ.
  • ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž… ์‚ญ์ œ๊ฐ€ O(1) ์— ๊ฐ€๋Šฅ
  • ๊ฐ€๋ณ€ ๊ธธ์ด์ž„

์˜ˆ์ œ : ์ˆซ์ž์˜ ํ•ฉ

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

 

11720๋ฒˆ: ์ˆซ์ž์˜ ํ•ฉ

์ฒซ์งธ ์ค„์— ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆซ์ž N๊ฐœ๊ฐ€ ๊ณต๋ฐฑ์—†์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

์ˆซ์ž๋ฅผ String ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, ๊ฐ๊ฐ์˜ ์›์†Œ๋ฅผ ๋ˆ„์ ํ•˜์—ฌ ๋”ํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        String number = sc.next();
        int ans = 0;

        for(int i = 0 ; i < t ; i++){
            ans += (number.charAt(i)-'0');
        }
        System.out.println(ans);
    }
}

์˜ˆ์ œ : ํ‰๊ท  ๊ตฌํ•˜๊ธฐ

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

 

1546๋ฒˆ: ํ‰๊ท 

์ฒซ์งธ ์ค„์— ์‹œํ—˜ ๋ณธ ๊ณผ๋ชฉ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ฐ’์€ 1000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„์— ์„ธ์ค€์ด์˜ ํ˜„์žฌ ์„ฑ์ ์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ฐ’์€ 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ด๊ณ , ์ ์–ด๋„ ํ•˜๋‚˜์˜ ๊ฐ’์€ 0๋ณด

www.acmicpc.net

๊ฐ๊ฐ์˜ ์ ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•œ ๋’ค ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ๋Œ€๋กœ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค.

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tc = sc.nextInt();
        int[] arr = new int[1000];

        for(int i = 0 ; i < tc; i++){
            arr[i] = sc.nextInt();
        }
        long max = 0;
        double avg = 0.0;
        long sum = 0;

        for(int i = 0 ; i < tc; i ++){
            if(max < arr[i]) max = arr[i];
            sum += arr[i];
        }

        avg = ((sum*100.0)/max)/tc;
        System.out.println(avg);
    }
}

 

2. ๊ตฌ๊ฐ„ ํ•ฉ

  ํ•ฉ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ์ค„์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋จผ์ € ๋ฐฐ์—ด A ์— ๋Œ€ํ•œ ํ•ฉ ๋ฐฐ์—ด S ๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. ํ•ฉ๋ฐฐ์—ด์€ S[i] = A[0] + A[1] + A[2] ... + A[i-1] + A[i] ์™€ ๊ฐ™์ด ์ •์˜ํ•œ๋‹ค. S ๋ฅผ ์ •์˜ํ•จ์œผ๋กœ ์จ, ๊ธฐ์กด ๋ฐฐ์—ด A ์—์„œ range sum ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ O(N) -> O(1) ์œผ๋กœ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

  A[2]~A[4] ๊ฐ„์˜ ๊ตฌ๊ฐ„ํ•ฉ์„ ๊ตฌํ•˜๋ ค๋ฉด S[4]-S[1] ์„ ๊ณ„์‚ฐํ•˜๋ฉด ๋˜๊ณ  ์ด๋Š” 15-8 = 7 ์ด๋‹ค.

 

์˜ˆ์ œ : ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ

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

 

11659๋ฒˆ: ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 4

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N๊ณผ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ˆ˜๋Š” 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ตฌ๊ฐ„ i์™€ j

www.acmicpc.net

  ์ˆ˜์˜ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 100,000 ์ด๊ณ  ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜์˜ ์ตœ๋Œ€๋Š” 100,000 ์ด๋‹ˆ ์ตœ์•…์˜ ๊ฒฝ์šฐ 100,000 * 100,000 = 10,000,000,000 ์œผ๋กœ 1์–ต ์ด์ƒ์˜ ์—ฐ์‚ฐ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ ์ œํ•œ ์‹œ๊ฐ„์ธ 1์ดˆ ๋‚ด์— ์ ˆ๋Œ€๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋‚ผ ์ˆ˜ ์—†๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์— ๊ตฌ๊ฐ„ํ•ฉ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ ์ตœ์ ํ™”๋ฅผ ํ•˜๋ฉด O(M) ์— ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int tc = sc.nextInt(); // testcase

        int[] arr = new int[n+1];
        int[] s = new int[n+1];

        for(int i = 1 ; i <= n ; i++){
            arr[i] = sc.nextInt();
        }

        s[1] = arr[1];
        for(int i = 2; i<=n; i++){
            s[i] = s[i-1] + arr[i];
        }

        for(int i = 0 ; i < tc; i++){
            int start = sc.nextInt();
            int end = sc.nextInt();
            int range_sum = s[end] - s[start-1];
            System.out.println(range_sum);
        }
    }
}

์˜ˆ์ œ : ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 2

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

 

11660๋ฒˆ: ๊ตฌ๊ฐ„ ํ•ฉ ๊ตฌํ•˜๊ธฐ 5

์ฒซ์งธ ์ค„์— ํ‘œ์˜ ํฌ๊ธฐ N๊ณผ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1024, 1 ≤ M ≤ 100,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ํ‘œ์— ์ฑ„์›Œ์ ธ ์žˆ๋Š” ์ˆ˜๊ฐ€ 1ํ–‰๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๋„ค

www.acmicpc.net

2์ฐจ์› ๋ฐฐ์—ด์—์„œ ๋ˆ„์ ํ•ฉ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ํ•ต์‹ฌ ์•„์ด๋””์–ด๋Š”,

dp[x][y] = dp[x-1][y] + dp[x][y-1] - dp[x][y-1] + dp[x-1][y-1] ์ธ ๊ฒƒ์ด๊ณ 

(x1, y1) ~ (x2, y2) ๊นŒ์ง€์˜ ํ•ฉ ans = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1] ๋ผ๋Š” ๊ฒƒ์ด๋‹ค. 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int size = Integer.parseInt(st.nextToken());
        int tc = Integer.parseInt(st.nextToken());

        int[][] arr = new int[size+1][size+1];
        int[][] dp = new int[size+1][size+1];

        for(int i = 1 ; i <= size; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 1 ; j <= size; j++){
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        dp[1][1] = arr[1][1];
        for(int i = 1 ; i<= size; i++){
            for(int j = 1 ; j <= size; j++){
                dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] + arr[i][j];
            }
        }

        for(int i = 0 ; i < tc; i++){
            st = new StringTokenizer(br.readLine());
            int x1 = Integer.parseInt(st.nextToken());
            int y1 = Integer.parseInt(st.nextToken());
            int x2 = Integer.parseInt(st.nextToken());
            int y2 = Integer.parseInt(st.nextToken());
            // (x1, y1) ~ (x2, y2)
            int ans = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1];
            System.out.println(ans);
        }
    }
}

์˜ˆ์ œ : ๋‚˜๋จธ์ง€ํ•ฉ

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

 

10986๋ฒˆ: ๋‚˜๋จธ์ง€ ํ•ฉ

์ˆ˜ N๊ฐœ A1, A2, ..., AN์ด ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, ์—ฐ์†๋œ ๋ถ€๋ถ„ ๊ตฌ๊ฐ„์˜ ํ•ฉ์ด M์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ๊ตฌ๊ฐ„์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ฆ‰, Ai + ... + Aj (i ≤ j) ์˜ ํ•ฉ์ด M์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” (i, j)

www.acmicpc.net

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken()); // ์ˆซ์ž ๊ฐœ์ˆ˜
        int m = Integer.parseInt(st.nextToken());

        long ans = 0;
        long[] s = new long[n];
        long[] r = new long[m];

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

        for(int i = 0 ; i < n ;i++){
            int rem = (int)(s[i]%m);
            if(rem == 0) ans++;
            r[rem]++;
        }

        for(int i = 0 ; i < m ;i++){
            if(r[i]>0){
                ans+=(r[i]*(r[i]-1))/2;
            }
        }
        System.out.println(ans);
    }
}

 


References

1. http://www.yes24.com/Product/Goods/108571508

 

Do it! ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ์ž๋ฐ” ํŽธ - YES24

IT ๊ธฐ์—… ์ทจ์—…๊ณผ ์ด์ง์˜ ํ•„์ˆ˜ ๋‹จ๊ณ„์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ!์ถœ์ œ ๊ฒฝํ–ฅ์„ ์™„๋ฒฝํ•˜๊ฒŒ ๋ฐ˜์˜ํ•œ ํ•ต์‹ฌ 100์ œ๋กœ ํ•œ ๋ฒˆ์— ํ•ฉ๊ฒฉํ•œ๋‹ค!“์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋Š” ์–ด๋–ป๊ฒŒ ์ค€๋น„ํ•ด์•ผ ํ• ๊นŒ?” ๊ณง ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์•ž๋‘๊ณ  ์žˆ๊ฑฐ

www.yes24.com

 

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