#include<bits/stdc++.h>
//IBOW
#define Task "KOJOIOI"
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define rep(i, b) for (int i = (1); i <= (b); ++i)
#define REP(i, b) for (int i = (b); i >= (1); --i)
#define pb push_back
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<long long> vll;
const int N = 2e3 + 10;
const int inf = 2e9 + 10;
const ll INF = 2e18 + 10;
const int MOD = 1e9 + 7;
int n, m, a[N][N], okA[N][N], okB[N][N], min_value = inf, max_value = 0, suf[N][N], pre[N][N];
bool ok(ll v)
{
fill(&suf[0][0], &suf[0][0] + N * N, 0);
fill(&pre[0][0], &pre[0][0] + N * N, 0);
rep(i, n) rep(j, m) okA[i][j] = okB[i][j] = 0;
rep(i, n) rep(j, m)
{
if (a[i][j] - min_value <= v) okA[i][j] = 1;
if (max_value <= v + a[i][j]) okB[i][j] = 1;
}
// rep(i, n) rep(j, m) cout << okA[i][j] << " \n"[j == m];
// cout << '\n';
// rep(i, n) rep(j, m) cout << okB[i][j] << " \n"[j == m];
{
//phần A ở trên phần B ở dưới
rep(i, n)
{
rep(j, m) pre[i][j] = pre[i][j - 1] + okA[i][j];
REP(j, m) suf[i][j] = suf[i][j + 1] + okB[i][j];
}
{
//phần A tăng dần
int past_height = 0;
rep(i, n)
{
bool flag = false;
if (i == n)
{
rep(j, m)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
} else
{
for (int j = 0; j <= m; ++j)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
}
if (flag)
{
if (i == n)
{
// cout << 1 << '\n';
return true;
}
continue;
}
else break;
}
}
{
//Phần A giảm dần
int past_height = 0;
REP(i, n)
{
bool flag = false;
if (i == 1)
{
rep(j, m)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
} else
{
for (int j = 0; j <= m; ++j)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
}
if (flag)
{
if (i == 1)
{
// cout << 2 << '\n';
return true;
}
continue;
}
else break;
}
}
}
{
//Phần B ở trên phần A ở dưới
fill(&suf[0][0], &suf[0][0] + N * N, 0);
fill(&pre[0][0], &pre[0][0] + N * N, 0);
rep(i, n)
{
rep(j, m) pre[i][j] = pre[i][j - 1] + okB[i][j];
REP(j, m) suf[i][j] = suf[i][j + 1] + okA[i][j];
}
{
//phần B tăng dần
int past_height = 0;
rep(i, n)
{
bool flag = false;
if (i == n)
{
rep(j, m)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
} else
{
for (int j = 0; j <= m; ++j)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
}
if (flag)
{
if (i == n)
{
// cout << 3 << '\n';
return true;
}
continue;
}
else break;
}
}
{
//Phần B giảm dần
int past_height = 0;
REP(i, n)
{
bool flag = false;
if (i == 1)
{
rep(j, m)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
} else
{
for (int j = 0; j <= m; ++j)
{
if (pre[i][j] == j && suf[i][j + 1] == m - j && j >= past_height)
{
flag = true;
past_height = j;
break;
}
}
}
if (flag)
{
if (i == 1)
{
// cout << 4 << '\n';
return true;
}
continue;
}
else break;
}
}
}
return false;
}
void DeoAClamcho()
{
cin >> n >> m;
rep(i, n) rep(j, m)
{
cin >> a[i][j];
min_value = min(min_value, a[i][j]);
max_value = max(max_value, a[i][j]);
}
ll L = 1, R = 1e9, ans = 0;
while (L <= R)
{
ll mid = (L + R) / 2;
// cout << mid << ":\n";
if (ok(mid)) R = mid - 1, ans = mid;
else L = mid + 1;
}
cout << ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(Task".inp", "r"))
{
freopen(Task".inp", "r", stdin);
// freopen(Task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t--) DeoAClamcho();
return 0;
}
Compilation message
joioi.cpp: In function 'int32_t main()':
joioi.cpp:255:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
255 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
32004 KB |
Output is correct |
2 |
Correct |
110 ms |
31948 KB |
Output is correct |
3 |
Correct |
171 ms |
32056 KB |
Output is correct |
4 |
Correct |
168 ms |
32040 KB |
Output is correct |
5 |
Correct |
206 ms |
32052 KB |
Output is correct |
6 |
Correct |
159 ms |
31948 KB |
Output is correct |
7 |
Correct |
210 ms |
32052 KB |
Output is correct |
8 |
Correct |
166 ms |
32036 KB |
Output is correct |
9 |
Correct |
162 ms |
32056 KB |
Output is correct |
10 |
Correct |
195 ms |
31948 KB |
Output is correct |
11 |
Correct |
206 ms |
32068 KB |
Output is correct |
12 |
Correct |
213 ms |
32048 KB |
Output is correct |
13 |
Correct |
179 ms |
32172 KB |
Output is correct |
14 |
Correct |
203 ms |
32064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
32004 KB |
Output is correct |
2 |
Correct |
110 ms |
31948 KB |
Output is correct |
3 |
Correct |
171 ms |
32056 KB |
Output is correct |
4 |
Correct |
168 ms |
32040 KB |
Output is correct |
5 |
Correct |
206 ms |
32052 KB |
Output is correct |
6 |
Correct |
159 ms |
31948 KB |
Output is correct |
7 |
Correct |
210 ms |
32052 KB |
Output is correct |
8 |
Correct |
166 ms |
32036 KB |
Output is correct |
9 |
Correct |
162 ms |
32056 KB |
Output is correct |
10 |
Correct |
195 ms |
31948 KB |
Output is correct |
11 |
Correct |
206 ms |
32068 KB |
Output is correct |
12 |
Correct |
213 ms |
32048 KB |
Output is correct |
13 |
Correct |
179 ms |
32172 KB |
Output is correct |
14 |
Correct |
203 ms |
32064 KB |
Output is correct |
15 |
Correct |
114 ms |
32076 KB |
Output is correct |
16 |
Correct |
133 ms |
34764 KB |
Output is correct |
17 |
Correct |
159 ms |
34884 KB |
Output is correct |
18 |
Correct |
166 ms |
34828 KB |
Output is correct |
19 |
Correct |
213 ms |
34832 KB |
Output is correct |
20 |
Correct |
200 ms |
34736 KB |
Output is correct |
21 |
Correct |
228 ms |
35268 KB |
Output is correct |
22 |
Correct |
234 ms |
35200 KB |
Output is correct |
23 |
Correct |
232 ms |
35212 KB |
Output is correct |
24 |
Correct |
189 ms |
34836 KB |
Output is correct |
25 |
Correct |
244 ms |
35220 KB |
Output is correct |
26 |
Correct |
178 ms |
35236 KB |
Output is correct |
27 |
Correct |
226 ms |
35220 KB |
Output is correct |
28 |
Correct |
208 ms |
35216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
32004 KB |
Output is correct |
2 |
Correct |
110 ms |
31948 KB |
Output is correct |
3 |
Correct |
171 ms |
32056 KB |
Output is correct |
4 |
Correct |
168 ms |
32040 KB |
Output is correct |
5 |
Correct |
206 ms |
32052 KB |
Output is correct |
6 |
Correct |
159 ms |
31948 KB |
Output is correct |
7 |
Correct |
210 ms |
32052 KB |
Output is correct |
8 |
Correct |
166 ms |
32036 KB |
Output is correct |
9 |
Correct |
162 ms |
32056 KB |
Output is correct |
10 |
Correct |
195 ms |
31948 KB |
Output is correct |
11 |
Correct |
206 ms |
32068 KB |
Output is correct |
12 |
Correct |
213 ms |
32048 KB |
Output is correct |
13 |
Correct |
179 ms |
32172 KB |
Output is correct |
14 |
Correct |
203 ms |
32064 KB |
Output is correct |
15 |
Correct |
114 ms |
32076 KB |
Output is correct |
16 |
Correct |
133 ms |
34764 KB |
Output is correct |
17 |
Correct |
159 ms |
34884 KB |
Output is correct |
18 |
Correct |
166 ms |
34828 KB |
Output is correct |
19 |
Correct |
213 ms |
34832 KB |
Output is correct |
20 |
Correct |
200 ms |
34736 KB |
Output is correct |
21 |
Correct |
228 ms |
35268 KB |
Output is correct |
22 |
Correct |
234 ms |
35200 KB |
Output is correct |
23 |
Correct |
232 ms |
35212 KB |
Output is correct |
24 |
Correct |
189 ms |
34836 KB |
Output is correct |
25 |
Correct |
244 ms |
35220 KB |
Output is correct |
26 |
Correct |
178 ms |
35236 KB |
Output is correct |
27 |
Correct |
226 ms |
35220 KB |
Output is correct |
28 |
Correct |
208 ms |
35216 KB |
Output is correct |
29 |
Correct |
1605 ms |
98824 KB |
Output is correct |
30 |
Correct |
1765 ms |
100704 KB |
Output is correct |
31 |
Correct |
1742 ms |
102248 KB |
Output is correct |
32 |
Correct |
1726 ms |
102288 KB |
Output is correct |
33 |
Correct |
1376 ms |
93480 KB |
Output is correct |
34 |
Correct |
1861 ms |
102276 KB |
Output is correct |
35 |
Correct |
1829 ms |
95240 KB |
Output is correct |
36 |
Correct |
1841 ms |
92508 KB |
Output is correct |
37 |
Correct |
2394 ms |
95412 KB |
Output is correct |