#include<bits/stdc++.h>
using namespace std;
int n, m, a[2002][2002], mxA[2002], mnA[2002], mxB[2002], mnB[2002], mx, mn;
bool okA[2002][2002], okB[2002][2002];
bool chk(int vdif)
{
for(int i = 1; i <= m; ++i)
{
mnA[i] = mnB[i] = n+1;
mxA[i] = mxB[i] = 0;
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
if(a[i][j] - mn <= vdif)
okA[i][j] = 1;
else
okA[i][j] = 0;
if(mx - a[i][j] <= vdif)
okB[i][j] = 1;
else
okB[i][j] = 0;
if(!okA[i][j] && !okB[i][j])
return 0;
if(okA[i][j] && !okB[i][j])
{
mxA[j] = max(mxA[j], i);
mnA[j] = min(mnA[j], i);
}
if(okB[i][j] && !okA[i][j])
{
mxB[j] = max(mxB[j], i);
mnB[j] = min(mnB[j], i);
}
}
// cout << vdif << '\n';
bool ok = 1;
bool ok2 = 1;
int mxxA = 0, mxxB = 0;
for(int i = 1; i <= m; ++i)
{
/*
cout << mxA[i] << " " << mnA[i] << " " << mxB[i] << " " << mnB[i] << '\n';
cout << mxxA << " " << mxxB << '\n';
cout << ok << " " << ok2 << '\n';
*/
if(mxA[i] > mnB[i])
ok = 0;
if(mxB[i] > mnA[i])
ok2 = 0;
if(mnB[i] - 1 < mxxA)
ok = 0;
if(mnA[i] - 1 < mxxB)
ok2 = 0;
mxxA = max(mxxA, mxA[i]);
mxxB = max(mxxB, mxB[i]);
}
if(ok)
return 1;
if(ok2)
return 1;
ok = 1;
ok2 = 1;
mxxA = n+1, mxxB = n+1;
for(int i = 1; i <= m; ++i)
{
/*
cout << mxA[i] << " " << mnA[i] << " " << mxB[i] << " " << mnB[i] << '\n';
cout << mxxA << " " << mxxB << '\n';
cout << ok << " " << ok2 << '\n';
*/
if(mxA[i] > mnB[i])
ok = 0;
if(mxB[i] > mnA[i])
ok2 = 0;
if(mxA[i] > mxxA)
ok = 0;
if(mxB[i] > mxxB)
ok2 = 0;
mxxA = min(mxxA, mnB[i] - 1);
mxxB = min(mxxB, mnA[i] - 1);
}
if(ok)
return 1;
if(ok2)
return 1;
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
mn = 1000000001;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
int st = 0;
int dr = mx - mn;
int ans = dr;
while(st <= dr)
{
int mid = (st + dr) / 2;
if(chk(mid))
ans = mid, dr = mid - 1;
else
st = mid + 1;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
504 KB |
Output is correct |
16 |
Correct |
9 ms |
2172 KB |
Output is correct |
17 |
Correct |
13 ms |
2424 KB |
Output is correct |
18 |
Correct |
11 ms |
2424 KB |
Output is correct |
19 |
Correct |
14 ms |
2424 KB |
Output is correct |
20 |
Correct |
14 ms |
2168 KB |
Output is correct |
21 |
Correct |
15 ms |
2552 KB |
Output is correct |
22 |
Correct |
16 ms |
2424 KB |
Output is correct |
23 |
Correct |
19 ms |
2552 KB |
Output is correct |
24 |
Correct |
13 ms |
2296 KB |
Output is correct |
25 |
Correct |
19 ms |
2552 KB |
Output is correct |
26 |
Correct |
15 ms |
2556 KB |
Output is correct |
27 |
Correct |
15 ms |
2552 KB |
Output is correct |
28 |
Correct |
15 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
380 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
380 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
504 KB |
Output is correct |
16 |
Correct |
9 ms |
2172 KB |
Output is correct |
17 |
Correct |
13 ms |
2424 KB |
Output is correct |
18 |
Correct |
11 ms |
2424 KB |
Output is correct |
19 |
Correct |
14 ms |
2424 KB |
Output is correct |
20 |
Correct |
14 ms |
2168 KB |
Output is correct |
21 |
Correct |
15 ms |
2552 KB |
Output is correct |
22 |
Correct |
16 ms |
2424 KB |
Output is correct |
23 |
Correct |
19 ms |
2552 KB |
Output is correct |
24 |
Correct |
13 ms |
2296 KB |
Output is correct |
25 |
Correct |
19 ms |
2552 KB |
Output is correct |
26 |
Correct |
15 ms |
2556 KB |
Output is correct |
27 |
Correct |
15 ms |
2552 KB |
Output is correct |
28 |
Correct |
15 ms |
2552 KB |
Output is correct |
29 |
Correct |
665 ms |
28280 KB |
Output is correct |
30 |
Correct |
730 ms |
39308 KB |
Output is correct |
31 |
Correct |
909 ms |
29560 KB |
Output is correct |
32 |
Correct |
664 ms |
29432 KB |
Output is correct |
33 |
Correct |
470 ms |
41208 KB |
Output is correct |
34 |
Correct |
827 ms |
29560 KB |
Output is correct |
35 |
Correct |
899 ms |
43384 KB |
Output is correct |
36 |
Correct |
694 ms |
40488 KB |
Output is correct |
37 |
Correct |
1210 ms |
43256 KB |
Output is correct |