File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+ private static Stack<Integer>[] stacks;
9+ private static int N;
10+
11+ public static void main(String[] args) throws IOException {
12+ init();
13+
14+ int answer = 0;
15+ for (int i = 0; i < 4; i++) {
16+ answer += stacks[i].size();
17+ }
18+
19+ if (answer-4 == N) bw.write("YES");
20+ else bw.write("NO");
21+
22+ bw.flush();
23+ bw.close();
24+ br.close();
25+ }
26+
27+ private static void init() throws IOException {
28+ stacks = new Stack[4];
29+
30+ for (int i = 0; i < 4; i++) {
31+ stacks[i] = new Stack<>();
32+ stacks[i].push(0);
33+ }
34+
35+ N = Integer.parseInt(br.readLine());
36+ StringTokenizer st = new StringTokenizer(br.readLine());
37+
38+ for (int i = 0; i < N; i++) {
39+ int num = Integer.parseInt(st.nextToken());
40+ int min = N;
41+ int index = -1;
42+
43+ for (int j = 0; j < 4; j++) {
44+ if (stacks[j].peek() == 0) {
45+ index = j;
46+ continue;
47+ }
48+ int temp = num - stacks[j].peek();
49+ if (temp < 0) continue;
50+ if (temp < min) {
51+ index = j;
52+ min = temp;
53+ }
54+ }
55+
56+ if (index > -1) stacks[index].push(num);
57+ }
58+ }
59+ }
60+ ```
You can’t perform that action at this time.
0 commit comments