#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const LL llinf=9000000000000000000;
const int inf=2000000000;
int h, w;
int arr[2010][2010];
int minnum=inf;
int maxnum;
bool ch(int val){
int st=0, dr=w;
int cresc=1, desc=1;
for(int i=1; i<=h&&(cresc||desc); i++){
int r=1, l=w;
while(arr[i][r]-minnum<=val&&r<=w)r++;
r--;
while(maxnum-arr[i][l]<=val&&l>0)l--;
st=max(st, l);
dr=min(r, dr);
if(st>r)cresc=0;
if(dr<l)desc=0;
}
return (cresc||desc);
}
int f(int st, int fin){
if(st==fin)return st;
int mid=(st+fin)/2;
if(ch(mid))return f(st, mid);
return f(mid+1, fin);
}
int main()
{
scanf("%d %d", &h, &w);
for(int i=1; i<=h; i++){
for(int j=1; j<=w; j++){
scanf("%d", &arr[i][j]);
minnum=min(minnum, arr[i][j]);
maxnum=max(maxnum, arr[i][j]);
}
}
int ans=f(0, 1000000000);
for(int i=1; i<=h; i++){
for(int j=1; j<=w/2; j++){
swap(arr[i][j], arr[i][w-j+1]);
}
}
ans=min(ans, f(0, 1000000000));
printf("%d", ans);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &h, &w);
~~~~~^~~~~~~~~~~~~~~~~
joioi.cpp:44:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &arr[i][j]);
~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
404 KB |
Output is correct |
16 |
Correct |
13 ms |
1308 KB |
Output is correct |
17 |
Correct |
14 ms |
1656 KB |
Output is correct |
18 |
Correct |
14 ms |
1532 KB |
Output is correct |
19 |
Correct |
14 ms |
1528 KB |
Output is correct |
20 |
Correct |
12 ms |
1400 KB |
Output is correct |
21 |
Correct |
14 ms |
1656 KB |
Output is correct |
22 |
Correct |
13 ms |
1656 KB |
Output is correct |
23 |
Correct |
13 ms |
1656 KB |
Output is correct |
24 |
Correct |
13 ms |
1528 KB |
Output is correct |
25 |
Correct |
13 ms |
1656 KB |
Output is correct |
26 |
Correct |
14 ms |
1656 KB |
Output is correct |
27 |
Correct |
14 ms |
1656 KB |
Output is correct |
28 |
Correct |
14 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
400 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
404 KB |
Output is correct |
16 |
Correct |
13 ms |
1308 KB |
Output is correct |
17 |
Correct |
14 ms |
1656 KB |
Output is correct |
18 |
Correct |
14 ms |
1532 KB |
Output is correct |
19 |
Correct |
14 ms |
1528 KB |
Output is correct |
20 |
Correct |
12 ms |
1400 KB |
Output is correct |
21 |
Correct |
14 ms |
1656 KB |
Output is correct |
22 |
Correct |
13 ms |
1656 KB |
Output is correct |
23 |
Correct |
13 ms |
1656 KB |
Output is correct |
24 |
Correct |
13 ms |
1528 KB |
Output is correct |
25 |
Correct |
13 ms |
1656 KB |
Output is correct |
26 |
Correct |
14 ms |
1656 KB |
Output is correct |
27 |
Correct |
14 ms |
1656 KB |
Output is correct |
28 |
Correct |
14 ms |
1656 KB |
Output is correct |
29 |
Correct |
1095 ms |
37724 KB |
Output is correct |
30 |
Correct |
959 ms |
37808 KB |
Output is correct |
31 |
Correct |
1036 ms |
39388 KB |
Output is correct |
32 |
Correct |
1046 ms |
39392 KB |
Output is correct |
33 |
Correct |
890 ms |
34296 KB |
Output is correct |
34 |
Correct |
1014 ms |
39508 KB |
Output is correct |
35 |
Correct |
988 ms |
54988 KB |
Output is correct |
36 |
Correct |
854 ms |
49912 KB |
Output is correct |
37 |
Correct |
980 ms |
55160 KB |
Output is correct |