Algorithm/Baekjoon

[Algorithm/Baekjoon] ํƒœ์ƒ์ด์˜ ํ›ˆ๋ จ์†Œ ์ƒํ™œ - 19951 (G5/JAVA)

dpdms2148 2025. 8. 6. 21:43
728x90

๐Ÿ“‘๋ฌธ์ œ๋งํฌ

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

๐Ÿ’ป์ฝ”๋“œ

import java.io.*;
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());

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

        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken()) - 1;
            int b = Integer.parseInt(st.nextToken());
            int k = Integer.parseInt(st.nextToken());
            directions[a] += k;
            directions[b] -= k;
        }
        for (int i = 0; i < N; i++) {
            directions[i + 1] += directions[i];
            System.out.print((ground[i] += directions[i]) + " ");
        }
        br.close();
    }

}

โณํšŒ๊ณ 

  • ์š”์ฒญ์„ ๊ทธ๋•Œ๊ทธ๋•Œ ์ฒ˜๋ฆฌํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ๋ ์ธ๋ฑ์Šค์— ์š”์ฒญ ๊ฐ’์„ ์ €์žฅํ•ด ๋‘๊ณ  ํ•œ๋ฒˆ์— ์—ฐ์‚ฐํ•ด ์ค˜์•ผ ํ•œ๋‹ค.
728x90