// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 2000000000;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int H, W;
int mx = 0, mn = INF;
vector<vector<int> > pmx, pmn;
int sol(vector<vector<int> > &gr) {
pmx.assign(H + 3, vector<int>(W + 3, 0));
pmn.assign(H + 3, vector<int>(W + 3, INF));
for(int j = 1; j <= W; j++) {
for(int i = 1; i <= H; i++) {
pmx[i][j] = max(pmx[i - 1][j] , gr[i][j]);
}
for(int i = H; i>=1; i--) {
pmn[i][j] = min(pmn[i + 1][j] , gr[i][j]);
}
}
for(int i = 1; i <= H; i++) {
for(int j = W; j >= 1; j--) {
pmx[i][j] = max(pmx[i][j], pmx[i][j + 1]);
}
for(int j = 1; j <= W; j++) {
pmn[i][j] = min(pmn[i][j], pmn[i][j - 1]);
}
}
// for(int i = 1; i<= H; i++) {
// for(int j = 1; j <= W; j++) {
// cout << pmx[i][j] << " ";
// }
// cout << "\n";
// }
// cout << "\n";
// for(int i = 1; i<= H; i++) {
// for(int j = 1; j <= W; j++) {
// cout << pmn[i][j] << " ";
// }
// cout << "\n";
// }
int ans = 0;
int ind = 1;
for(int j = 1; j <= W; j++) {
int cur = INF, bst = ind;
for(int i = ind; i<= H + 1; i++) {
if(max(pmx[i - 1][j] - mn, mx - pmn[i][j]) < cur) {
cur = max(pmx[i - 1][j] - mn, mx - pmn[i][j]);
bst = i;
}
}
// cout << "cur:" << cur << "bst:" << bst << "\n";
ans = max(ans, cur);
ind = bst;
}
return ans;
}
inline void solve() {
cin >> H >> W;
vector<vector<int> > gr1(H + 3, vector<int>(W + 3, 0));
vector<vector<int> > gr2(H + 3, vector<int>(W + 3, 0));
for(int i = 1; i<=H; i++) {
for(int j = 1; j <= W; j++) {
cin >> gr1[i][j];
mn = min(mn, gr1[i][j]);
mx = max(mx, gr1[i][j]);
gr2[i][W - j + 1] = gr1[i][j];
}
}
// cout << mn << " mx:" << mx << "\n";
int ans = sol(gr1);
// cout << "ans:" << ans << "\n";
ans = min(ans, sol(gr2));
vector<vector<int> > tmp(H + 3, vector<int>(W + 3, 0));
for(int i = 1; i<=H; i++) {
for(int j = 1; j <= W; j++) {
tmp[H - i + 1][j] = gr1[i][j];
}
}
ans = min(ans, sol(tmp));
// cout << "ans:" << ans << "\n";
for(int i = 1; i<=H; i++) {
for(int j = 1; j <= W; j++) {
tmp[H - i + 1][j] = gr2[i][j];
}
}
ans = min(ans, sol(tmp));
cout << ans << "\n";
}
signed main() {
fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1116 KB |
Output is correct |
17 |
Correct |
6 ms |
1496 KB |
Output is correct |
18 |
Correct |
5 ms |
1368 KB |
Output is correct |
19 |
Correct |
6 ms |
1368 KB |
Output is correct |
20 |
Correct |
5 ms |
1368 KB |
Output is correct |
21 |
Correct |
6 ms |
1628 KB |
Output is correct |
22 |
Correct |
6 ms |
1880 KB |
Output is correct |
23 |
Correct |
6 ms |
1624 KB |
Output is correct |
24 |
Correct |
6 ms |
1384 KB |
Output is correct |
25 |
Correct |
7 ms |
1628 KB |
Output is correct |
26 |
Correct |
6 ms |
1492 KB |
Output is correct |
27 |
Correct |
7 ms |
1548 KB |
Output is correct |
28 |
Correct |
6 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
1 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
1116 KB |
Output is correct |
17 |
Correct |
6 ms |
1496 KB |
Output is correct |
18 |
Correct |
5 ms |
1368 KB |
Output is correct |
19 |
Correct |
6 ms |
1368 KB |
Output is correct |
20 |
Correct |
5 ms |
1368 KB |
Output is correct |
21 |
Correct |
6 ms |
1628 KB |
Output is correct |
22 |
Correct |
6 ms |
1880 KB |
Output is correct |
23 |
Correct |
6 ms |
1624 KB |
Output is correct |
24 |
Correct |
6 ms |
1384 KB |
Output is correct |
25 |
Correct |
7 ms |
1628 KB |
Output is correct |
26 |
Correct |
6 ms |
1492 KB |
Output is correct |
27 |
Correct |
7 ms |
1548 KB |
Output is correct |
28 |
Correct |
6 ms |
1628 KB |
Output is correct |
29 |
Correct |
972 ms |
97284 KB |
Output is correct |
30 |
Correct |
937 ms |
94968 KB |
Output is correct |
31 |
Correct |
1018 ms |
102484 KB |
Output is correct |
32 |
Correct |
1004 ms |
102480 KB |
Output is correct |
33 |
Correct |
841 ms |
89168 KB |
Output is correct |
34 |
Correct |
1026 ms |
102428 KB |
Output is correct |
35 |
Correct |
1002 ms |
118124 KB |
Output is correct |
36 |
Correct |
868 ms |
102968 KB |
Output is correct |
37 |
Correct |
996 ms |
118252 KB |
Output is correct |