/*huypheu
4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10
*/
//https://oj.uz/problem/view/JOI17_joioi
#include <bits/stdc++.h>
using namespace std;
int mi[2007][3],ma[2007][3];
int a[2007][2007],ty[2007][2007],val[2007];
vector <int> v;
int n,m;
int mak,mik;
bool checko()
{
for(int i=1;i<=n;i++) mi[i][1]=0,ma[i][1]=0,mi[i][2]=m+1,ma[i][2]=m+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(ty[i][j]==1) mi[i][1]=j,ma[i][1]=j;
if(ty[i][j]==3) ma[i][1]=j;
if(ty[i][j]==2) break;
}
for(int j=m;j>=1;j--)
{
if(ty[i][j]==2) mi[i][2]=j,ma[i][2]=j;
if(ty[i][j]==3) ma[i][2]=j;
if(ty[i][j]==1) break;
}
if(ma[i][1]<ma[i][2]-1) return 0;
}
bool ok=0;
for(int i=1;i<=n;i++)
{
if(i==1) val[i]=mi[i][1]; else
{
int k=max(val[i-1],mi[i][1]);
if(k<=ma[i][1]) val[i]=k; else break;
}
if(i==n) ok=1;
}
if(ok) return 1;
for(int i=1;i<=n;i++)
{
if(i==1) val[i]=ma[i][1]; else
{
int k=min(val[i-1],ma[i][1]);
if(k>=mi[i][1]) val[i]=k; else break;
}
if(i==n) ok=1;
}
if(ok) return 1;
return 0;
}
bool check(int len)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]<=mik+len) ty[i][j]=1;
if(a[i][j]>=mak-len) ty[i][j]=2;
if(a[i][j]<=mik+len && a[i][j]>=mak-len) ty[i][j]=3;
if(a[i][j]>mik+len && a[i][j]<mak-len) return 0;
}
}
if(checko()) return 1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]>=mik+len) ty[i][j]=1;
if(a[i][j]<=mak-len) ty[i][j]=2;
if(a[i][j]<=mik+len && a[i][j]>=mak-len) ty[i][j]=3;
if(a[i][j]>mik+len && a[i][j]<mak-len) return 0;
}
}
if(checko()) return 1;
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin >> a[i][j];
}
}
mik=a[1][1],mak=a[1][1];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
mik=min(mik,a[i][j]);
mak=max(mak,a[i][j]);
}
}
int l=0,r=(mak-mik);
while(l<r)
{
int mid=(l+r)/2;
if(check(mid)) r=mid; else l=mid+1;
}
cout << l << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
33700 KB |
Output is correct |
2 |
Correct |
0 ms |
33700 KB |
Output is correct |
3 |
Correct |
0 ms |
33700 KB |
Output is correct |
4 |
Correct |
0 ms |
33700 KB |
Output is correct |
5 |
Correct |
0 ms |
33700 KB |
Output is correct |
6 |
Correct |
0 ms |
33700 KB |
Output is correct |
7 |
Correct |
0 ms |
33700 KB |
Output is correct |
8 |
Correct |
0 ms |
33700 KB |
Output is correct |
9 |
Correct |
0 ms |
33700 KB |
Output is correct |
10 |
Correct |
0 ms |
33700 KB |
Output is correct |
11 |
Correct |
0 ms |
33700 KB |
Output is correct |
12 |
Correct |
0 ms |
33700 KB |
Output is correct |
13 |
Correct |
0 ms |
33700 KB |
Output is correct |
14 |
Correct |
0 ms |
33700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
33700 KB |
Output is correct |
2 |
Correct |
0 ms |
33700 KB |
Output is correct |
3 |
Correct |
0 ms |
33700 KB |
Output is correct |
4 |
Correct |
0 ms |
33700 KB |
Output is correct |
5 |
Correct |
0 ms |
33700 KB |
Output is correct |
6 |
Correct |
0 ms |
33700 KB |
Output is correct |
7 |
Correct |
0 ms |
33700 KB |
Output is correct |
8 |
Correct |
0 ms |
33700 KB |
Output is correct |
9 |
Correct |
0 ms |
33700 KB |
Output is correct |
10 |
Correct |
0 ms |
33700 KB |
Output is correct |
11 |
Correct |
0 ms |
33700 KB |
Output is correct |
12 |
Correct |
0 ms |
33700 KB |
Output is correct |
13 |
Correct |
0 ms |
33700 KB |
Output is correct |
14 |
Correct |
0 ms |
33700 KB |
Output is correct |
15 |
Correct |
0 ms |
33700 KB |
Output is correct |
16 |
Correct |
0 ms |
33700 KB |
Output is correct |
17 |
Correct |
9 ms |
33700 KB |
Output is correct |
18 |
Correct |
3 ms |
33700 KB |
Output is correct |
19 |
Correct |
13 ms |
33700 KB |
Output is correct |
20 |
Correct |
9 ms |
33700 KB |
Output is correct |
21 |
Correct |
16 ms |
33700 KB |
Output is correct |
22 |
Correct |
6 ms |
33700 KB |
Output is correct |
23 |
Correct |
16 ms |
33700 KB |
Output is correct |
24 |
Correct |
0 ms |
33700 KB |
Output is correct |
25 |
Correct |
19 ms |
33700 KB |
Output is correct |
26 |
Correct |
9 ms |
33700 KB |
Output is correct |
27 |
Correct |
13 ms |
33700 KB |
Output is correct |
28 |
Correct |
13 ms |
33700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
33700 KB |
Output is correct |
2 |
Correct |
0 ms |
33700 KB |
Output is correct |
3 |
Correct |
0 ms |
33700 KB |
Output is correct |
4 |
Correct |
0 ms |
33700 KB |
Output is correct |
5 |
Correct |
0 ms |
33700 KB |
Output is correct |
6 |
Correct |
0 ms |
33700 KB |
Output is correct |
7 |
Correct |
0 ms |
33700 KB |
Output is correct |
8 |
Correct |
0 ms |
33700 KB |
Output is correct |
9 |
Correct |
0 ms |
33700 KB |
Output is correct |
10 |
Correct |
0 ms |
33700 KB |
Output is correct |
11 |
Correct |
0 ms |
33700 KB |
Output is correct |
12 |
Correct |
0 ms |
33700 KB |
Output is correct |
13 |
Correct |
0 ms |
33700 KB |
Output is correct |
14 |
Correct |
0 ms |
33700 KB |
Output is correct |
15 |
Correct |
0 ms |
33700 KB |
Output is correct |
16 |
Correct |
0 ms |
33700 KB |
Output is correct |
17 |
Correct |
9 ms |
33700 KB |
Output is correct |
18 |
Correct |
3 ms |
33700 KB |
Output is correct |
19 |
Correct |
13 ms |
33700 KB |
Output is correct |
20 |
Correct |
9 ms |
33700 KB |
Output is correct |
21 |
Correct |
16 ms |
33700 KB |
Output is correct |
22 |
Correct |
6 ms |
33700 KB |
Output is correct |
23 |
Correct |
16 ms |
33700 KB |
Output is correct |
24 |
Correct |
0 ms |
33700 KB |
Output is correct |
25 |
Correct |
19 ms |
33700 KB |
Output is correct |
26 |
Correct |
9 ms |
33700 KB |
Output is correct |
27 |
Correct |
13 ms |
33700 KB |
Output is correct |
28 |
Correct |
13 ms |
33700 KB |
Output is correct |
29 |
Correct |
793 ms |
33700 KB |
Output is correct |
30 |
Correct |
1069 ms |
33700 KB |
Output is correct |
31 |
Correct |
1356 ms |
33700 KB |
Output is correct |
32 |
Correct |
793 ms |
33700 KB |
Output is correct |
33 |
Correct |
376 ms |
33700 KB |
Output is correct |
34 |
Correct |
1223 ms |
33700 KB |
Output is correct |
35 |
Correct |
1089 ms |
33700 KB |
Output is correct |
36 |
Correct |
776 ms |
33700 KB |
Output is correct |
37 |
Correct |
2266 ms |
33700 KB |
Output is correct |