less than 1 minute read

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

๐Ÿ’ง Platinum 5


๐Ÿ“Œ ๋ฌธ์ œ

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


image


๐Ÿ“Œ ํ’€์ด

์ด ๋ฌธ์ œ๋Š” ์ด์ „


๐Ÿ“Œ Code

package BOJ.DS;

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

public class N6549 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        Stack<Long[]> stack = new Stack<>();

        long num = Long.parseLong(st.nextToken());
        while (num != 0) {
            long max = 0;
            stack.add(new Long[]{Long.parseLong(st.nextToken()), 0l});
            for (long i = 1; i < num; i++) {
                long value = Long.parseLong(st.nextToken());
                if (value > stack.peek()[0]) {
                    stack.add(new Long[]{value, i});
                } else if (value == stack.peek()[0]) {
                    //์•„๋ฌด๊ฒƒ๋„ ์•ˆํ•œ๋‹ค.
                } else {
                    Long[] arr = stack.pop();
                    max = Math.max(arr[0] * (i - arr[1]), max);
                    while (!stack.isEmpty() && value < stack.peek()[0]) {
                        arr = stack.pop();
                        max = Math.max(arr[0] * (i - arr[1]), max);
                    }
                    stack.add(new Long[]{value, arr[1]});
                }
            }
            while (!stack.isEmpty()) {
                Long[] arr = stack.pop();
                max = Math.max(arr[0] * (num - arr[1]), max);
            }
            st = new StringTokenizer(br.readLine());
            num = Integer.parseInt(st.nextToken());
            System.out.println(max);
        }
    }
}

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



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