1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 3


๐Ÿ“Œ ๋ฌธ์ œ

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

image


๐Ÿ“Œ ํ’€์ด

์ฒ˜์Œ ์ด ๋ฌธ์ œ๋ฅผ ๋ดค์„ ๋•Œ๋Š” ๋„ˆ๋ฌด๋‚˜๋„ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๊ฐ€ ์™œ ์‹ค๋ฒ„3์ผ๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ์ ˆ๋กœ ๋“ค์—ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋ง‰์ƒ ์ด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋‹ˆ, ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค.

ํ•˜์ง€๋งŒ ๋‚ด๊ฐ€ ์•„๋Š” ๊ทธ ์–ด๋– ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ๋„ ์ด ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค.
๊ทธ๋ž˜์„œ ๋ˆ„์  ํ•ฉ(Prefix Sum)์ด๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด๋ณด์•˜๋‹ค.

๊ณต๋ถ€๋ฅผ ํ•ด๋ณด๋‹ˆ๊นŒ ๋งค์šฐ ์‹ ๋ฐ•ํ•œ ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค.
์ˆซ์ž๋ฅผ ๋ฐ›์„๋•Œ, ๋ฏธ๋ฆฌ ์ˆซ์ž๋“ค์„ ๋”ํ•ด์„œ ๋ˆ„์  ํ•ฉ์— ๋Œ€ํ•œ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋†“๊ณ , ๊ฐ„๋‹จํ•˜๊ฒŒ start - end๋ฅผ ํ†ตํ•ด์„œ ์—ฌ๋Ÿฌ๋ฒˆ์˜ ๊ณ„์‚ฐ์—†์ด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์—ˆ๋‹ค.
์ด๋ฏธ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์—์„œ ์กฐ๊ธˆ๋งŒ ์†์„ ๋ณด๋‹ˆ, ๋งค์šฐ ๋นจ๋ผ์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋‚˜์™”๋‹ค.

์ด๋Ÿฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์‰ฝ์ง€๋งŒ, ๋ชจ๋ฅด๋ฉด ์ ˆ๋Œ€ ๋ชปํ‘ธ๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ ์•„์ง๋„ ๊ณต๋ถ€ํ•  ๊ฒƒ์ด ๋งŽ์ด ๋‚จ์•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“œ๋Š” ๋ฌธ์ œ์˜€๋‹ค.


๐Ÿ“Œ Code

package PS;

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

public class N11659_NS {
    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());

        int[] arr = new int[N + 1];
        arr[0] = 0;

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

        int result;
        for (int i = 0; i < M; i++) {
            result = 0;
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            System.out.println(arr[end] - arr[start - 1]);
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜