Etc/알고리즘

[softeer] 9657

jjuni_96 2024. 7. 22. 22:24
728x90

문제

 

 

메모리 제약

 

제약조건

 

 

입/출력

 

문제 분석

간단하게 각 행별로 연산하면 쉬울 것 같음.
1. 각 행별 환경 파괴자를 합산.
2. 공격한 행들의 환경 파괴자를 -1
3. 최종적으로 각 행들의 합을 구함

 

풀이

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        /*
            * 행 크기의 idx를 만들어서 각 행의 합을 구해놓음
         */

        String[] input1 = bf.readLine().split(" ");
        int n = Integer.parseInt(input1[0]); // 열
        int m = Integer.parseInt(input1[1]); // 행

        // 행 초기값 셋팅
        int[] tree = new int[n];

        // 값 입력받기
        for (int i = 0; i < n; i++) {
            String[] inputs = bf.readLine().split(" ");
            for (int j = 0; j < m; j++) {
                tree[i] += Integer.parseInt(inputs[j]);
            }
        }



        // 첫번째 공격
        String[] firstAttack = bf.readLine().split(" ");
        int start = Integer.parseInt(firstAttack[0]);
        int end = Integer.parseInt(firstAttack[1]);
        for (int i = start; i <= end; i++) {
            if (tree[i-1] != 0) {tree[i-1] -= 1;}
        }


        String[] secondAttack = bf.readLine().split(" ");
        start = Integer.parseInt(secondAttack[0]);
        end = Integer.parseInt(secondAttack[1]);
        for (int i = start; i <= end; i++) {
            if (tree[i-1] != 0) {tree[i-1] -= 1;}
        }
        System.out.println(Arrays.stream(tree).sum());
    }
}

 

성공

 

 

링크

https://softeer.ai/practice/9657

 

Softeer - 현대자동차그룹 SW인재확보플랫폼

 

softeer.ai

 

728x90
반응형
LIST