Skip to content

Commit 8a65211

Browse files
authored
Merge pull request #396 from AlgorithmWithGod/khj20006
[20250628] BOJ / P5 / 퍼즐 자르기 / 권혁준
2 parents dda7a23 + 7ee4370 commit 8a65211

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N;
57+
static int[] r;
58+
static int[][] info;
59+
static long[] c;
60+
61+
static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));}
62+
63+
public static void main(String[] args) throws Exception {
64+
65+
io = new IOController();
66+
67+
init();
68+
solve();
69+
70+
io.close();
71+
72+
}
73+
74+
public static void init() throws Exception {
75+
76+
N = io.nextInt();
77+
r = new int[N];
78+
c = new long[N];
79+
info = new int[N][];
80+
for(int i=0;i<N;i++) {
81+
r[i] = i;
82+
int a = io.nextInt();
83+
info[i] = new int[]{a,i};
84+
c[i] = 1;
85+
}
86+
87+
}
88+
89+
static void solve() throws Exception {
90+
91+
boolean[] vis = new boolean[N];
92+
Arrays.sort(info, (a,b) -> b[0]-a[0]);
93+
94+
long ans = 0;
95+
96+
for(int[] f:info) {
97+
int h = f[0], i = f[1];
98+
if(i<N-1 && vis[i+1]) {
99+
int right = f(i+1), cur = f(i);
100+
c[cur] += c[right];
101+
r[right] = cur;
102+
}
103+
if(i-1>=0 && vis[i-1]) {
104+
int left = f(i-1), cur = f(i);
105+
c[cur] += c[left];
106+
r[left] = cur;
107+
}
108+
vis[i] = true;
109+
ans = Math.max(ans, c[f(i)] * h);
110+
}
111+
io.write(ans + "\n");
112+
113+
}
114+
115+
}
116+
```

0 commit comments

Comments
 (0)