알고리즘 풀이/백준

[백준][Java] 3020번 개똥벌레 (누적합)

배게 2021. 10. 13. 19:20
728x90

태그에는 이분탐색도 있는데 여기서 이분탐색을 어떻게 쓰라는건지는 일단 모르겠고

Line:18의 해당 구간의 장애물 개수를 구해준 정수형 배열 int[] obstacle는

따로 구할 필요가 없었다 최소값과 그 최소값을 가진 구간의 수 2개만 필요하기 때문에

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
 
 
 
public class Main {
        
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
    public static void main(String[] args) throws IOException{
        String[] str = br.readLine().split(" ");
        int N = stoi(str[0]);
        int H = stoi(str[1]);
        int[] obstacle = new int[H+2];
        int[] obsAccum = new int[H+2];
        
        boolean isJongyu = true;
        for(int i=1; i<=N; i++) {
            int len = stoi(br.readLine());
            
            if(isJongyu) {
                obsAccum[1]++;
                obsAccum[1+len]--;
                
                isJongyu=!isJongyu;
            }
            else {
                obsAccum[H+1-len]++;
                obsAccum[H+1]--;
                
                isJongyu=!isJongyu;
            }
        }
        
        int accum = 0;
        int minValue = Integer.MAX_VALUE;
        int minNum = 0;
        
        for(int i=1; i<=H; i++) {
            accum += obsAccum[i];
            obstacle[i] += accum;
            
            if(accum<minValue) {
                minValue = accum;
                minNum = 1;
            }
            else if(accum==minValue)
                minNum++;
        }
        
        System.out.println(minValue+" "+minNum);
        
//        for (int i : obstacle) {
//            System.out.println(i+ " ");
//        }
        
        
        
//        bw.write("");
//        bw.flush();
//        bw.close();
    }
 
    private static int stoi(String input) {
        return Integer.parseInt(input);
    }
}
cs