#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, a[2020][2020], tmp[2020][2020], d[2020];
bool solve(int val, int x, int y){
int mn = a[x][y];
for (int i=1;i<=n;i++){
for (d[i]=0;d[i]<=d[i-1];d[i]++){
if (d[i]==m || a[i][d[i]+1] - mn > val) break;
}
}
if (d[x] < y) return 0;
int mx = 0;
mn = 1e9;
for (int i=1;i<=n;i++){
for (int j=d[i]+1;j<=m;j++){
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
return mx - mn <= val;
}
int getans(int x, int y){
int l = 0, r = 1e9, ret = 1e9;
while(l<=r){
int m = (l+r)>>1;
if (solve(m, x, y)) ret = m, r = m-1;
else l = m+1;
}
//printf("YES: %d\n", ret);
return ret;
}
void flipx(int &x, int &y){
x = n+1-x;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
tmp[n+1-i][j] = a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
a[i][j] = tmp[i][j];
}
}
}
void flipy(int &x, int &y){
y = m+1-y;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
tmp[i][m+1-j] = a[i][j];
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
a[i][j] = tmp[i][j];
}
}
}
int main(){
scanf("%d %d", &n, &m);
d[0] = m;
int mn = 2e9, x, y;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
scanf("%d", a[i]+j);
if (mn > a[i][j]){
mn = a[i][j];
x = i, y = j;
}
}
}
int ans = getans(x, y); flipx(x, y);
ans = min(ans, getans(x, y)); flipy(x, y);
ans = min(ans, getans(x, y)); flipx(x, y);
ans = min(ans, getans(x, y));
printf("%d\n", ans);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joioi.cpp:75:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d", a[i]+j);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |