#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> grid;
void reverse_rows(){
for(auto &i : grid){
reverse(i.begin(), i.end());
}
}
void reverse_cols(){
reverse(grid.begin(), grid.end());
}
int good(int mini, int K){
int H = grid.size();
int W = grid[0].size();
int can_go = W;
int mind = 1e9, maxd = 0;
for(int i = 0; i < H; i ++){
for(int j = 0; j < can_go; j ++){
if(mini + K < grid[i][j])can_go = j;
}
for(int j = can_go; j < W; j ++){
maxd = max(grid[i][j], maxd);
mind = min(grid[i][j], mind);
}
}
return maxd-mind;
}
int check(int K, int mini){
int pass = 0;
reverse_rows();
pass |= (good(mini, K) <= K);
reverse_cols();
pass |= (good(mini, K) <= K);
reverse_rows();
pass |= (good(mini, K) <= K);
reverse_cols();
pass |= (good(mini, K) <= K);
return pass;
}
int main(){
int H, W;
cin >> H >> W;
int mini = 1e9;
grid.assign(H, vector<int>(W));
for(int i = 0; i < H; i ++){
for(int j = 0; j < W; j ++){
cin >> grid[i][j];
mini = min(grid[i][j], mini);
}
}
int L = 0, R = 1e9;
while(L < R){
int mid = (L + R) / 2;
int t = check(mid, mini);
if(t){
R = mid;
}
else{
L = mid + 1;
}
}
cout << L << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
9 ms |
448 KB |
Output is correct |
17 |
Correct |
16 ms |
340 KB |
Output is correct |
18 |
Correct |
14 ms |
456 KB |
Output is correct |
19 |
Correct |
14 ms |
340 KB |
Output is correct |
20 |
Correct |
12 ms |
444 KB |
Output is correct |
21 |
Correct |
16 ms |
460 KB |
Output is correct |
22 |
Correct |
22 ms |
460 KB |
Output is correct |
23 |
Correct |
17 ms |
464 KB |
Output is correct |
24 |
Correct |
15 ms |
340 KB |
Output is correct |
25 |
Correct |
17 ms |
460 KB |
Output is correct |
26 |
Correct |
17 ms |
456 KB |
Output is correct |
27 |
Correct |
17 ms |
460 KB |
Output is correct |
28 |
Correct |
16 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
9 ms |
448 KB |
Output is correct |
17 |
Correct |
16 ms |
340 KB |
Output is correct |
18 |
Correct |
14 ms |
456 KB |
Output is correct |
19 |
Correct |
14 ms |
340 KB |
Output is correct |
20 |
Correct |
12 ms |
444 KB |
Output is correct |
21 |
Correct |
16 ms |
460 KB |
Output is correct |
22 |
Correct |
22 ms |
460 KB |
Output is correct |
23 |
Correct |
17 ms |
464 KB |
Output is correct |
24 |
Correct |
15 ms |
340 KB |
Output is correct |
25 |
Correct |
17 ms |
460 KB |
Output is correct |
26 |
Correct |
17 ms |
456 KB |
Output is correct |
27 |
Correct |
17 ms |
460 KB |
Output is correct |
28 |
Correct |
16 ms |
464 KB |
Output is correct |
29 |
Correct |
1287 ms |
15232 KB |
Output is correct |
30 |
Correct |
1254 ms |
14864 KB |
Output is correct |
31 |
Correct |
1399 ms |
16008 KB |
Output is correct |
32 |
Correct |
1404 ms |
16024 KB |
Output is correct |
33 |
Correct |
1159 ms |
14004 KB |
Output is correct |
34 |
Correct |
1358 ms |
16016 KB |
Output is correct |
35 |
Correct |
1772 ms |
16012 KB |
Output is correct |
36 |
Correct |
1525 ms |
14036 KB |
Output is correct |
37 |
Correct |
1766 ms |
16016 KB |
Output is correct |