Skip to content

Commit 6ea3e61

Browse files
authored
[20251029] BOJ/ G5 / 점 모으기 / 이종환
1 parent 8476c9b commit 6ea3e61

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.Collections;
8+
import java.util.HashSet;
9+
import java.util.PriorityQueue;
10+
import java.util.StringTokenizer;
11+
12+
public class Main {
13+
14+
static int size, pointCnt,xIdx,yIdx;
15+
static int[] xArr,yArr;
16+
static long xSum, ySum,xCumul,yCumul, ansX, ansY;
17+
18+
19+
public static void main(String[] args) throws IOException {
20+
init();
21+
process();
22+
print();
23+
}
24+
25+
private static void init() throws IOException{
26+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));;
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
size = Integer.parseInt(st.nextToken());
29+
pointCnt = Integer.parseInt(st.nextToken());
30+
xIdx = 0;
31+
yIdx = 0;
32+
xSum = 0;
33+
ySum = 0;
34+
ansX = Long.MAX_VALUE;
35+
ansY = Long.MAX_VALUE;
36+
37+
xArr = new int[pointCnt];
38+
yArr = new int[pointCnt];
39+
40+
xCumul = 0;
41+
yCumul = 0;
42+
for (int i = 0; i < pointCnt; i++) {
43+
st = new StringTokenizer(br.readLine());
44+
int y = Integer.parseInt(st.nextToken());
45+
int x = Integer.parseInt(st.nextToken());
46+
xSum += x;
47+
ySum += y;
48+
xArr[i] = x;
49+
yArr[i] = y;
50+
}
51+
52+
53+
}
54+
55+
private static void process() {
56+
Arrays.sort(xArr);
57+
Arrays.sort(yArr);
58+
59+
int num = 1;
60+
while(num <= size) {
61+
long curSumX = 0;
62+
long curSumY = 0;
63+
while (xIdx < pointCnt && num >= xArr[xIdx]) {
64+
xCumul += xArr[xIdx++];
65+
}
66+
while (yIdx < pointCnt && num >= yArr[yIdx]) {
67+
yCumul += yArr[yIdx++];
68+
}
69+
70+
curSumX = num*xIdx - xCumul*2 + xSum - num * (pointCnt - xIdx);
71+
curSumY = num*yIdx - yCumul*2 + ySum - num * (pointCnt - yIdx);
72+
73+
ansX = Math.min(ansX, curSumX);
74+
ansY = Math.min(ansY, curSumY);
75+
num++;
76+
}
77+
}
78+
79+
80+
81+
private static void print() {
82+
System.out.println(ansY + ansX);
83+
}
84+
}
85+
86+
87+
```

0 commit comments

Comments
 (0)