#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int nmax = 2005;
pii dp[nmax][nmax];
int last[nmax];
int mnx, mny;
int MN;
int n, m;
const int inf = 1e9 + 1;
int mn[nmax][nmax], mx[nmax][nmax];
int a[nmax][nmax];
void fil(){
for(int i = 1; i <= n; i++){
mn[i][m + 1] = inf;
mx[i][m + 1] = -inf;
for(int j = m; j >= 1; j--)
mx[i][j] = max(a[i][j], mx[i][j + 1]),
mn[i][j] = min(a[i][j], mn[i][j + 1]);
}
}
bool check(int X){
for(int i = 0; i <= n; i++){
for(int j = 0; j <= m; j++)
dp[i][j] = {inf, -inf};
if(i == 0) last[i] = m;
else last[i] = 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j] > MN + X) break;
last[i]++;
}
}
last[1] = min(last[1], m - 1);
for(int i= 1; i <= n; i++){
for(int j = 0; j <= last[i]; j++){
if(i == mnx && j < mny) {
dp[i][j] = {-inf, inf};
continue;
}
int u = min(j, last[i - 1]);
dp[i][j].f = min(dp[i - 1][u].f, mn[i][j + 1]);
dp[i][j].s = max(dp[i - 1][u].s, mx[i][j + 1]);
}
for(int j = last[i] + 1; j <= m; j++)
dp[i][j] = {-inf, inf};
}
for(int j = 1; j <= m; j++){
if(dp[n][j].s == -inf) continue;
if(dp[n][j].s - dp[n][j].f <= X) return true;
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= m; j++)
dp[i][j] = {inf, -inf};
}
;
for(int i = 1; i <= n; i++){
last[i] = 0;
for(int j = 1; j <= m; j++){
if(a[i][j] > MN + X) break;
last[i]++;
}
}
int mlast = m;
for(int i= 1; i <= n; i++){
mlast = min(mlast, last[i - 1]);
for(int j = 0; j <= last[i]; j++){
if(i == mnx && j < mny) {
dp[i][j] = {-inf, inf};
continue;
}
int u = max(j, mlast);
dp[i][j].f = min(dp[i - 1][u].f, mn[i][j + 1]);
dp[i][j].s = max(dp[i - 1][u].s, mx[i][j + 1]);
}
for(int j = last[i] + 1; j <= m; j++)
dp[i][j] = {-inf, inf};
}
/*for(int i = 1; i <= n; i++){
for(int j = 0; j <= m; j++){
cout << dp[i][j].f << ' ';
}
cout << "\n";
}*/
for(int j = 0; j < m; j++){
if(dp[n][j].s == -inf) continue;
if(dp[n][j].s - dp[n][j].f <= X) return true;
}
return false;
}
int main(){
cin >> n >> m;
MN = inf;
for(int i = 1; i <= n; i++){
for(int j= 1; j <= m; j++){
cin >> a[i][j];
if(a[i][j] < MN)
MN = a[i][j], mnx = i, mny = j;
}
}
fil();
int l = 0, r = 1e9+ 1;
while(l + 1 < r){
int mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
//cout << r;
for(int i = 1 ;i <= n; i++){
for(int j = 1; j <= m - j + 1; ++j)
swap(a[i][j], a[i][m - j + 1]);
}
mny = m - mny + 1;
int ans = r;
l = 0, r = 1e9 + 1;
fil();
while(l + 1 < r){
int mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
//cout << r;
cout << min(ans, r);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
25 ms |
4288 KB |
Output is correct |
17 |
Correct |
28 ms |
4548 KB |
Output is correct |
18 |
Correct |
27 ms |
4540 KB |
Output is correct |
19 |
Correct |
25 ms |
4540 KB |
Output is correct |
20 |
Correct |
25 ms |
4052 KB |
Output is correct |
21 |
Correct |
39 ms |
4672 KB |
Output is correct |
22 |
Correct |
30 ms |
4600 KB |
Output is correct |
23 |
Correct |
34 ms |
4728 KB |
Output is correct |
24 |
Correct |
31 ms |
4164 KB |
Output is correct |
25 |
Correct |
35 ms |
4684 KB |
Output is correct |
26 |
Correct |
34 ms |
4780 KB |
Output is correct |
27 |
Correct |
36 ms |
4660 KB |
Output is correct |
28 |
Correct |
33 ms |
4612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
444 KB |
Output is correct |
8 |
Correct |
1 ms |
440 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
440 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
25 ms |
4288 KB |
Output is correct |
17 |
Correct |
28 ms |
4548 KB |
Output is correct |
18 |
Correct |
27 ms |
4540 KB |
Output is correct |
19 |
Correct |
25 ms |
4540 KB |
Output is correct |
20 |
Correct |
25 ms |
4052 KB |
Output is correct |
21 |
Correct |
39 ms |
4672 KB |
Output is correct |
22 |
Correct |
30 ms |
4600 KB |
Output is correct |
23 |
Correct |
34 ms |
4728 KB |
Output is correct |
24 |
Correct |
31 ms |
4164 KB |
Output is correct |
25 |
Correct |
35 ms |
4684 KB |
Output is correct |
26 |
Correct |
34 ms |
4780 KB |
Output is correct |
27 |
Correct |
36 ms |
4660 KB |
Output is correct |
28 |
Correct |
33 ms |
4612 KB |
Output is correct |
29 |
Correct |
2437 ms |
96992 KB |
Output is correct |
30 |
Correct |
2383 ms |
100316 KB |
Output is correct |
31 |
Correct |
2695 ms |
101884 KB |
Output is correct |
32 |
Correct |
2431 ms |
101836 KB |
Output is correct |
33 |
Correct |
2181 ms |
89296 KB |
Output is correct |
34 |
Correct |
2453 ms |
102088 KB |
Output is correct |
35 |
Correct |
3150 ms |
117556 KB |
Output is correct |
36 |
Correct |
3009 ms |
111972 KB |
Output is correct |
37 |
Correct |
3079 ms |
117752 KB |
Output is correct |