#include<bits/stdc++.h>
using namespace std;
using ll = int;
const ll INF = 1e9+1;
bool possible(vector<vector<ll>>& grid, ll M, ll L, ll R) {
int h = grid.size(), w = grid[0].size();
vector<vector<bool>> dp(h, vector<bool>(w+1));
dp[0][0] = 1;
for(int i=0; i<h; ++i) {
ll mx = L;
if(i!=0) {
dp[i][0] = dp[i-1][0];
}
for(int j=0; j<w; ++j) {
mx = max(mx, grid[i][j]);
if(i!=0) {
dp[i][j+1] = dp[i][j+1] | dp[i-1][j+1];
}
dp[i][j+1] = dp[i][j+1] | dp[i][j];
if(mx-L > M) dp[i][j+1] = 0;
}
ll mini = R;
for(int j=w; j>=0; --j) {
if(R-mini > M) dp[i][j] = 0;
if(j-1>=0) mini = min(mini, grid[i][j-1]);
}
}
bool res = 0;
for(int i=0; i<w; ++i) {
if(dp[h-1][i+1]) res = 1;
}
return res;
}
ll solve(vector<vector<ll>>& grid) {
int h = grid.size(), w = grid[0].size();
ll L = INF, R = 0;
for(int i=0; i<h; ++i) {
for(int j=0; j<w; ++j) {
L = min(L, grid[i][j]);
R = max(R, grid[i][j]);
}
}
ll lo = 0, hi = R - L;
ll res = INF;
while(lo<=hi) {
ll M = (lo+hi)/2;
if(possible(grid, M, L, R)) {
hi = M - 1;
res = M;
}
else {
lo = M + 1;
}
}
return res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int h, w; cin>>h>>w;
vector<vector<ll>> grid(h, vector<ll>(w));
for(int i=0; i<h; ++i) {
for(int j=0; j<w; ++j) {
cin>>grid[i][j];
}
}
ll ans = INF;
auto G = grid;
ans = min(ans, solve(G));
reverse(G.begin(), G.end());
ans = min(ans, solve(G));
for(int i=0; i<h; ++i) {
reverse(G[i].begin(), G[i].end());
}
ans = min(ans, solve(G));
reverse(G.begin(), G.end());
ans = min(ans, solve(G));
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
11 ms |
640 KB |
Output is correct |
17 |
Correct |
40 ms |
732 KB |
Output is correct |
18 |
Correct |
41 ms |
640 KB |
Output is correct |
19 |
Correct |
40 ms |
640 KB |
Output is correct |
20 |
Correct |
36 ms |
640 KB |
Output is correct |
21 |
Correct |
57 ms |
728 KB |
Output is correct |
22 |
Correct |
57 ms |
640 KB |
Output is correct |
23 |
Correct |
57 ms |
640 KB |
Output is correct |
24 |
Correct |
53 ms |
640 KB |
Output is correct |
25 |
Correct |
58 ms |
640 KB |
Output is correct |
26 |
Correct |
58 ms |
760 KB |
Output is correct |
27 |
Correct |
58 ms |
640 KB |
Output is correct |
28 |
Correct |
57 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
11 ms |
640 KB |
Output is correct |
17 |
Correct |
40 ms |
732 KB |
Output is correct |
18 |
Correct |
41 ms |
640 KB |
Output is correct |
19 |
Correct |
40 ms |
640 KB |
Output is correct |
20 |
Correct |
36 ms |
640 KB |
Output is correct |
21 |
Correct |
57 ms |
728 KB |
Output is correct |
22 |
Correct |
57 ms |
640 KB |
Output is correct |
23 |
Correct |
57 ms |
640 KB |
Output is correct |
24 |
Correct |
53 ms |
640 KB |
Output is correct |
25 |
Correct |
58 ms |
640 KB |
Output is correct |
26 |
Correct |
58 ms |
760 KB |
Output is correct |
27 |
Correct |
58 ms |
640 KB |
Output is correct |
28 |
Correct |
57 ms |
640 KB |
Output is correct |
29 |
Correct |
3230 ms |
31088 KB |
Output is correct |
30 |
Correct |
3149 ms |
51816 KB |
Output is correct |
31 |
Correct |
3384 ms |
55832 KB |
Output is correct |
32 |
Correct |
3417 ms |
55672 KB |
Output is correct |
33 |
Correct |
2971 ms |
48872 KB |
Output is correct |
34 |
Correct |
3410 ms |
55896 KB |
Output is correct |
35 |
Execution timed out |
4103 ms |
71488 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |