#include <bits/stdc++.h>
using namespace std;
int h, w;
int max_alt{0};
int min_alt(1e9);
bool check_kingdom(int x, vector<vector<int>>& country)
{
int border{w};
bool valid;
//min left top
if(country[0][0]-min_alt<=x)
{
valid=true;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(j<border)
{
if(country[i][j]-min_alt>x) {border=j; j--;}
}
else
{
if(max_alt-country[i][j]>x)
{
valid=false;
break;
}
}
}
if(not valid) break;
}
if(valid) return true;
}
//min left bottom
border=w;
if(country[h-1][0]-min_alt<=x)
{
valid=true;
for(int i=h-1; i>=0; i--)
{
for(int j=0; j<w; j++)
{
if(j<border)
{
if(country[i][j]-min_alt>x) {border=j; j--;}
}
else
{
if(max_alt-country[i][j]>x)
{
valid=false;
break;
}
}
}
if(not valid) break;
}
if(valid) return true;
}
//max left top
border=w;
if(max_alt-country[0][0]<=x)
{
valid=true;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
if(j<border)
{
if(max_alt-country[i][j]>x) {border=j; j--;}
}
else
{
if(country[i][j]-min_alt>x)
{
valid=false;
break;
}
}
}
if(not valid) break;
}
if(valid) return true;
}
//max left bottom
border=w;
if(max_alt-country[h-1][0]<=x)
{
valid=true;
for(int i=h-1; i>=0; i--)
{
for(int j=0; j<w; j++)
{
if(j<border)
{
if(max_alt-country[i][j]>x) {border=j; j--;}
}
else
{
if(country[i][j]-min_alt>x)
{
valid=false;
break;
}
}
}
if(not valid) break;
}
if(valid) return true;
}
return false;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>h>>w;
vector<int> row(w, 0);
vector<vector<int>> country(h, row);
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
cin>>country[i][j];
max_alt=max(max_alt, country[i][j]);
min_alt=min(min_alt, country[i][j]);
}
}
int high=max_alt-min_alt;
int low=-1;
int mid;
while(high-low>1)
{
mid=(high+low)/2;
if(check_kingdom(mid, country)) high=mid;
else low=mid;
}
cout<<high;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
724 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
8 ms |
852 KB |
Output is correct |
22 |
Correct |
5 ms |
852 KB |
Output is correct |
23 |
Correct |
5 ms |
852 KB |
Output is correct |
24 |
Correct |
5 ms |
808 KB |
Output is correct |
25 |
Correct |
6 ms |
848 KB |
Output is correct |
26 |
Correct |
9 ms |
856 KB |
Output is correct |
27 |
Correct |
8 ms |
852 KB |
Output is correct |
28 |
Correct |
8 ms |
856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
4 ms |
724 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
596 KB |
Output is correct |
21 |
Correct |
8 ms |
852 KB |
Output is correct |
22 |
Correct |
5 ms |
852 KB |
Output is correct |
23 |
Correct |
5 ms |
852 KB |
Output is correct |
24 |
Correct |
5 ms |
808 KB |
Output is correct |
25 |
Correct |
6 ms |
848 KB |
Output is correct |
26 |
Correct |
9 ms |
856 KB |
Output is correct |
27 |
Correct |
8 ms |
852 KB |
Output is correct |
28 |
Correct |
8 ms |
856 KB |
Output is correct |
29 |
Correct |
299 ms |
37428 KB |
Output is correct |
30 |
Correct |
296 ms |
36300 KB |
Output is correct |
31 |
Correct |
362 ms |
39196 KB |
Output is correct |
32 |
Correct |
338 ms |
39236 KB |
Output is correct |
33 |
Correct |
290 ms |
34004 KB |
Output is correct |
34 |
Correct |
314 ms |
39244 KB |
Output is correct |
35 |
Correct |
485 ms |
54712 KB |
Output is correct |
36 |
Correct |
426 ms |
47844 KB |
Output is correct |
37 |
Correct |
526 ms |
54836 KB |
Output is correct |