File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ #
2
+ # https://www.acmicpc.net/problem/2461
3
+
4
+ import sys
5
+ import heapq
6
+
7
+ input = sys .stdin .readline
8
+ N ,M = map (int , input ().split ())
9
+ classes = [list (map (int , input ().split ())) for _ in range (N )]
10
+ for n in range (N ):
11
+ heapq .heapify (classes [n ])
12
+
13
+ if N == 1 :
14
+ print (0 )
15
+ exit (0 )
16
+
17
+ INF = int (1e9 )
18
+ answer = INF
19
+ mins = INF
20
+ maxs = 0
21
+ participants = []
22
+ for n in range (N ):
23
+ val = heapq .heappop (classes [n ])
24
+ maxs = max (maxs , val )
25
+ mins = min (mins , val )
26
+ heapq .heappush (participants , (val , n ))
27
+
28
+ mins_flag = True
29
+ while True :
30
+ if mins_flag :
31
+ mins = max (mins , participants [0 ][0 ])
32
+
33
+ diff = maxs - mins
34
+ answer = min (answer , diff )
35
+
36
+ min_idx = None
37
+ while participants :
38
+ _ , min_idx = heapq .heappop (participants )
39
+ if classes [min_idx ]:
40
+ break
41
+ else :
42
+ mins_flag = False
43
+ else :
44
+ break
45
+
46
+ new_val = heapq .heappop (classes [min_idx ])
47
+ maxs = max (maxs , new_val )
48
+ heapq .heappush (participants , (new_val , min_idx ))
49
+
50
+ print (answer )
You can’t perform that action at this time.
0 commit comments