-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_2461.java
More file actions
93 lines (78 loc) · 2.79 KB
/
BOJ_2461.java
File metadata and controls
93 lines (78 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.*;
import java.util.*;
public class BOJ_2461 {
/*
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] arr = new int[N*M][2];
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
arr[M*i+j][0] = Integer.parseInt(st.nextToken());
arr[M*i+j][1] = i;
}
}
Arrays.sort(arr, (a, b) -> a[0] - b[0]);
int left = 0, right = 0;
int[] cnt = new int[N];
int count = 1;
cnt[arr[0][1]] = 1;
int min = Integer.MAX_VALUE;
while(left <= right) {
if(count == N) {
min = Math.min(min, arr[right][0] - arr[left][0]);
if(cnt[arr[left][1]] == 1) count--;
cnt[arr[left][1]]--;
left++;
} else {
right++;
if(right == N*M) break;
if(cnt[arr[right][1]] == 0) count++;
cnt[arr[right][1]]++;
}
}
System.out.println(min);
}
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[][] arr = new int[N][M];
for(int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0; j < M; j++) {
arr[i][j] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr[i]);
}
// System.out.println("arr = " + Arrays.deepToString(arr));
int[] nums = new int[N];
for(int i = 0; i < N; i++) {
nums[i] = arr[i][0];
}
int[] numsIdx = new int[N];
int answer = Integer.MAX_VALUE;
for (int time = 0; time < N*(M-1); time++) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int minIdx = -1;
for(int i = 0; i < N; i++) {
if(nums[i] <= min) {
min = nums[i];
minIdx = i;
}
max = Math.max(max, nums[i]);
}
answer = Math.min(answer, max - min);
numsIdx[minIdx]++;
if(numsIdx[minIdx] == M) break;
nums[minIdx] = arr[minIdx][numsIdx[minIdx]];
}
System.out.println(answer);
}
}