Skip to content

Commit 350511d

Browse files
authored
[20250415] BOJ / G1 / 순열 그래프의 연결성 판별 / 권혁준
1 parent 19e3c40 commit 350511d

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
while(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int N;
26+
static int[] A, max;
27+
28+
public static void main(String[] args) throws Exception {
29+
30+
ready();
31+
solve();
32+
33+
bwEnd();
34+
35+
}
36+
37+
static void ready() throws Exception{
38+
39+
N = nextInt();
40+
A = new int[N+1];
41+
for(int i=1;i<=N;i++) A[i] = nextInt();
42+
max = new int[N+1];
43+
44+
}
45+
46+
static void solve() throws Exception{
47+
48+
for(int i=1;i<=N;i++) max[i] = Math.max(max[i-1], A[i]);
49+
50+
int min = -1;
51+
Stack<Integer> ans = new Stack<>();
52+
ans.add(N+1);
53+
for(int i=N;i>=1;) {
54+
int j = i, tempMin = A[i];
55+
while(j>=1 && max[j] == max[i]) {
56+
tempMin = Math.min(tempMin, A[j]);
57+
j--;
58+
}
59+
if(min == -1) min = tempMin;
60+
else {
61+
if(max[i] <= min) ans.add(i+1);
62+
min = Math.min(min, tempMin);
63+
}
64+
i = j;
65+
}
66+
if(!ans.isEmpty() && ans.peek() != 1) ans.add(1);
67+
68+
bw.write((ans.size()-1) + "\n");
69+
int now = ans.pop();
70+
while(!ans.isEmpty()) {
71+
int next = ans.pop();
72+
bw.write((next-now) + " ");
73+
for(int i=now;i<next;i++) bw.write(i + " ");
74+
bw.write("\n");
75+
now = next;
76+
}
77+
78+
}
79+
80+
}
81+
82+
```

0 commit comments

Comments
 (0)