Skip to content

Commit 924c6bc

Browse files
authored
[20250416] BOJ / G1 / 좋은 수 / 권혁준
1 parent 350511d commit 924c6bc

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 class Node{
26+
long cnt, left, right, diff;
27+
int val;
28+
Node(long cnt, int val, long left, long right, long diff){
29+
this.cnt = cnt;
30+
this.val = val;
31+
this.left = left;
32+
this.right = right;
33+
this.diff = diff;
34+
}
35+
}
36+
37+
static int L, N;
38+
static int[] A;
39+
static int INF = 1010101010;
40+
static TreeSet<Integer> V;
41+
42+
public static void main(String[] args) throws Exception {
43+
44+
ready();
45+
solve();
46+
47+
bwEnd();
48+
49+
}
50+
51+
static void ready() throws Exception{
52+
53+
L = nextInt();
54+
A = new int[L];
55+
for(int i=0;i<L;i++) A[i] = nextInt();
56+
N = nextInt();
57+
V = new TreeSet<>();
58+
59+
}
60+
61+
static void solve() throws Exception{
62+
63+
PriorityQueue<Node> PQ = new PriorityQueue<>((a,b) -> {
64+
if(a.right == INF) {
65+
if(b.right == INF) return a.val-b.val;
66+
return 1;
67+
}
68+
if(b.right == INF) return -1;
69+
if(a.cnt == b.cnt) return a.val-b.val;
70+
if(a.cnt < b.cnt) return -1;
71+
return 1;
72+
});
73+
74+
Arrays.sort(A);
75+
List<Integer> zeros = new ArrayList<>();
76+
if(A[0] > 2) {
77+
PQ.add(new Node(cal(1,A[0]-1,1), 1, 1, A[0]-1, 1));
78+
PQ.add(new Node(cal(1,A[0]-1,A[0]-1), A[0]-1, 1, A[0]-1, -1));
79+
}
80+
else if(A[0] == 2) zeros.add(1);
81+
for(int i=0;i<L;i++) zeros.add(A[i]);
82+
PQ.add(new Node(Long.MAX_VALUE, A[L-1]+1, A[L-1]+1, INF, 1));
83+
for(int i=1;i<L;i++) {
84+
int l = A[i-1]+1, r = A[i]-1;
85+
int len = r-l;
86+
if(len < 0) continue;
87+
if(len == 0) zeros.add(l);
88+
else {
89+
PQ.add(new Node(cal(l,r,l), l, l, r, 1));
90+
PQ.add(new Node(cal(l,r,r), r, l, r, -1));
91+
}
92+
}
93+
Collections.sort(zeros);
94+
for(int i:zeros) {
95+
bw.write(i + " ");
96+
if(--N == 0) break;
97+
}
98+
99+
while(N>0) {
100+
Node now = PQ.poll();
101+
if(V.contains(now.val)) continue;
102+
V.add(now.val);
103+
bw.write(now.val + " ");
104+
N--;
105+
Node copy = new Node(now.cnt, now.val, now.left, now.right, now.diff);
106+
copy.val += copy.diff;
107+
if(copy.left > copy.val || copy.right < copy.val) continue;
108+
copy.cnt = cal(copy.left, copy.right, copy.val);
109+
PQ.add(copy);
110+
}
111+
112+
}
113+
114+
static long cal(long l, long r, long x) {
115+
return (x-l+1)*(r-x+1);
116+
}
117+
118+
}
119+
120+
```

0 commit comments

Comments
 (0)