/** - dwuy -
/> フ
| _ _|
/`ミ _x ノ
/ |
/ ヽ ?
/ ̄| | | |
| ( ̄ヽ__ヽ_)_)
\二つ
**/
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define TASK "test"
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MX = 2005;
int n, m;
int mxvalue, mnvalue;
int a[MX][MX];
bool f[MX][MX];
pii f1[MX][MX];
pii f2[MX][MX];
pii f3[MX][MX];
pii f4[MX][MX];
void nhap(){
cin >> n >> m;
cin.ignore();
for(int i=1; i<=n; i++){
string s; getline(cin, s); s += ' ';
for(int j=0, t=1; j<len(s); j++) if(s[j] != ' '){
a[i][t] = a[i][t]*10 + s[j] - '0';
if(s[j + 1] == ' ') t++;
}
}
}
bool ok(int mid){
for(int i=0; i<=n+1; i++) f[i][0] = f[i][m+1] = 0;
/// case 1
/// X O
/// O O
for(int i=1; i<=n; i++){
for(int j=m; j>=0; j--){
f[i][j] = f[i][j+1] | ((i == 1 || f[i-1][j]) && f1[i][j].se <= mnvalue + mid && f4[i][j+1].fi >= mxvalue - mid);
}
}
if(f[n][0]) return 1;
/// case 2
/// O X
/// O O
for(int i=1; i<=n; i++){
for(int j=1; j<=m+1; j++){
f[i][j] = f[i][j-1] | ((i == 1 || f[i-1][j]) && f2[i][j].se <= mnvalue + mid && f3[i][j-1].fi >= mxvalue - mid);
}
}
if(f[n][m+1]) return 1;
for(int i=0; i<=n+1; i++) f[i][0] = f[i][m+1] = 0;
/// case 3
/// O O
/// X O
for(int i=n; i>=1; i--){
for(int j=m; j>=0; j--){
f[i][j] = f[i][j+1] | ((i == n || f[i+1][j]) && f3[i][j].se <= mnvalue + mid && f2[i][j+1].fi >= mxvalue - mid);
}
}
if(f[1][0]) return 1;
/// case 4
/// O O
/// O X
for(int i=n; i>=1; i--){
for(int j=1; j<=m+1; j++){
f[i][j] = f[i][j-1] | ((i == n || f[i+1][j]) && f4[i][j].se <= mnvalue + mid && f1[i][j-1].fi >= mxvalue - mid);
}
}
if(f[1][m+1]) return 1;
return 0;
}
pii combine(pii a, pii b){
return {min(a.fi, b.fi), max(a.se, b.se)};
}
void solve(){
for(int i=0; i<=n+1; i++){
for(int j=0; j<=m+n; j++){
f1[i][j] = f2[i][j] = f3[i][j] = f4[i][j] = {INF, -INF};
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
f1[i][j] = combine(combine(f1[i-1][j], f1[i][j-1]), {a[i][j], a[i][j]});
}
for(int j=m; j>=1; j--){
f2[i][j] = combine(combine(f2[i-1][j], f2[i][j+1]), {a[i][j], a[i][j]});
}
}
for(int i=n; i>=1; i--){
for(int j=1; j<=m; j++){
f3[i][j] = combine(combine(f3[i+1][j], f3[i][j-1]), {a[i][j], a[i][j]});
}
for(int j=m; j>=1; j--){
f4[i][j] = combine(combine(f4[i+1][j], f4[i][j+1]), {a[i][j], a[i][j]});
}
}
mnvalue = f1[n][m].first;
mxvalue = f1[n][m].second;
int res = -1;
for(int lo=1, hi=1e9; lo<=hi;){
int mid = (lo + hi)>>1;
if(ok(mid)) res = mid, hi = mid - 1;
else lo = mid + 1;
}
cout << res;
}
int32_t main(){
fastIO;
//file(TASK);
nhap();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
6 ms |
8796 KB |
Output is correct |
17 |
Correct |
9 ms |
8796 KB |
Output is correct |
18 |
Correct |
10 ms |
10584 KB |
Output is correct |
19 |
Correct |
11 ms |
8796 KB |
Output is correct |
20 |
Correct |
10 ms |
7864 KB |
Output is correct |
21 |
Correct |
12 ms |
8796 KB |
Output is correct |
22 |
Correct |
13 ms |
10552 KB |
Output is correct |
23 |
Correct |
11 ms |
8796 KB |
Output is correct |
24 |
Correct |
9 ms |
8028 KB |
Output is correct |
25 |
Correct |
10 ms |
8792 KB |
Output is correct |
26 |
Correct |
12 ms |
8796 KB |
Output is correct |
27 |
Correct |
11 ms |
8796 KB |
Output is correct |
28 |
Correct |
13 ms |
8988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
0 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
2652 KB |
Output is correct |
5 |
Correct |
0 ms |
2652 KB |
Output is correct |
6 |
Correct |
0 ms |
2652 KB |
Output is correct |
7 |
Correct |
0 ms |
2652 KB |
Output is correct |
8 |
Correct |
0 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
0 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
0 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
6 ms |
8796 KB |
Output is correct |
17 |
Correct |
9 ms |
8796 KB |
Output is correct |
18 |
Correct |
10 ms |
10584 KB |
Output is correct |
19 |
Correct |
11 ms |
8796 KB |
Output is correct |
20 |
Correct |
10 ms |
7864 KB |
Output is correct |
21 |
Correct |
12 ms |
8796 KB |
Output is correct |
22 |
Correct |
13 ms |
10552 KB |
Output is correct |
23 |
Correct |
11 ms |
8796 KB |
Output is correct |
24 |
Correct |
9 ms |
8028 KB |
Output is correct |
25 |
Correct |
10 ms |
8792 KB |
Output is correct |
26 |
Correct |
12 ms |
8796 KB |
Output is correct |
27 |
Correct |
11 ms |
8796 KB |
Output is correct |
28 |
Correct |
13 ms |
8988 KB |
Output is correct |
29 |
Correct |
737 ms |
139160 KB |
Output is correct |
30 |
Correct |
784 ms |
145924 KB |
Output is correct |
31 |
Correct |
771 ms |
145580 KB |
Output is correct |
32 |
Correct |
824 ms |
145944 KB |
Output is correct |
33 |
Correct |
597 ms |
128632 KB |
Output is correct |
34 |
Correct |
858 ms |
145808 KB |
Output is correct |
35 |
Correct |
1077 ms |
145748 KB |
Output is correct |
36 |
Correct |
771 ms |
145236 KB |
Output is correct |
37 |
Correct |
1174 ms |
145936 KB |
Output is correct |