#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e12+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 |
1 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 |
512 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 |
1 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 |
512 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 |
14 ms |
1784 KB |
Output is correct |
17 |
Correct |
56 ms |
1976 KB |
Output is correct |
18 |
Correct |
50 ms |
1956 KB |
Output is correct |
19 |
Correct |
56 ms |
1976 KB |
Output is correct |
20 |
Correct |
44 ms |
1784 KB |
Output is correct |
21 |
Correct |
71 ms |
2096 KB |
Output is correct |
22 |
Correct |
72 ms |
2080 KB |
Output is correct |
23 |
Correct |
79 ms |
2096 KB |
Output is correct |
24 |
Correct |
97 ms |
1896 KB |
Output is correct |
25 |
Correct |
82 ms |
2280 KB |
Output is correct |
26 |
Correct |
78 ms |
2092 KB |
Output is correct |
27 |
Correct |
73 ms |
2096 KB |
Output is correct |
28 |
Correct |
73 ms |
2096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
512 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 |
14 ms |
1784 KB |
Output is correct |
17 |
Correct |
56 ms |
1976 KB |
Output is correct |
18 |
Correct |
50 ms |
1956 KB |
Output is correct |
19 |
Correct |
56 ms |
1976 KB |
Output is correct |
20 |
Correct |
44 ms |
1784 KB |
Output is correct |
21 |
Correct |
71 ms |
2096 KB |
Output is correct |
22 |
Correct |
72 ms |
2080 KB |
Output is correct |
23 |
Correct |
79 ms |
2096 KB |
Output is correct |
24 |
Correct |
97 ms |
1896 KB |
Output is correct |
25 |
Correct |
82 ms |
2280 KB |
Output is correct |
26 |
Correct |
78 ms |
2092 KB |
Output is correct |
27 |
Correct |
73 ms |
2096 KB |
Output is correct |
28 |
Correct |
73 ms |
2096 KB |
Output is correct |
29 |
Execution timed out |
4082 ms |
142628 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |