File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # [ Gold V] 신을 모시는 사당 - 27210
2+
3+ [ 문제 링크] ( https://www.acmicpc.net/problem/27210 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 23780 KB, 시간: 260 ms
8+
9+ ### 분류
10+
11+ 다이나믹 프로그래밍, 누적 합
12+
13+ ### 제출 일자
14+
15+ 2025년 7월 27일 20:10:47
16+
17+ ### 문제 설명
18+
19+ <p >신을 모시는 사당에는 신을 조각한 돌상 <em >N</em >개가 일렬로 놓여 있다. 각 돌상은 왼쪽 또는 오른쪽을 바라보고 서있다. 창영이는 연속한 몇 개의 돌상에 금칠을 하여 궁극의 깨달음을 얻고자 한다.</p >
20+
21+ <p >궁극의 깨달음을 얻기 위해서는 가능한 한 많은 금색 돌상들이 같은 방향을 바라보아야 한다. 방향이 다른 돌상은 깨달음에 치명적이다. 깨달음의 양은 아래와 같이 정의된다.</p >
22+
23+ <p style =" text-align : center ;" >| (<strong >왼쪽</strong >을 바라보는 금색 돌상의 개수) - (<strong >오른쪽</strong >을 바라보는 금색 돌상의 개수) |</p >
24+
25+ <p >창영이는 궁극의 깨달음을 얻을 수 있을까?</p >
26+
27+ ### 입력
28+
29+ <p >첫째 줄에 돌상의 개수 <em >N</em >이 주어진다.</p >
30+
31+ <p >둘째 줄에 돌상이 나열된 순서대로, 각 돌상이 바라보고 있는 방향이 주어진다. 입력의 편의상 왼쪽은 1, 오른쪽은 2라고 하자.</p >
32+
33+ ### 출력
34+
35+ <p >최대한 많은 깨달음을 얻기 위해 금을 칠하였을 때, 얻을 수 있는 깨달음의 양을 출력한다.</p >
36+
Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+ static int n ;
6+ static int [] one , two ;
7+ static StringTokenizer st ;
8+ static BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
9+ public static void main (String [] args ) throws Exception {
10+ inputSetting ();
11+ System .out .println (Math .max (kadane_algo (one ), kadane_algo (two )));
12+ }
13+
14+ static int kadane_algo (int [] arr ){
15+ int max = 0 ;
16+ int cur = 0 ;
17+
18+ for (int i = 0 ; i < n ; i ++){
19+ cur = Math .max (arr [i ], cur + arr [i ]);
20+ max = Math .max (max , cur );
21+ }
22+
23+ return max ;
24+ }
25+
26+ static void inputSetting () throws Exception {
27+ n = Integer .parseInt (br .readLine ());
28+ one = new int [n ];
29+ two = new int [n ];
30+
31+ int now ;
32+ st = new StringTokenizer (br .readLine ());
33+ for (int i = 0 ; i < n ; i ++){
34+ now = Integer .parseInt (st .nextToken ());
35+
36+ if (now == 1 ){
37+ one [i ] = 1 ;
38+ two [i ] = -1 ;
39+ } else {
40+ one [i ] = -1 ;
41+ two [i ] = 1 ;
42+ }
43+ }
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments