#include <bits/stdc++.h>
#define DIMN 2010
using namespace std;
int mini , maxi , n , m , sol;
int a[DIMN][DIMN] , where[DIMN][DIMN] , h[DIMN];
priority_queue <pair <int , pair <int,int> > > up , down;
void solve(){
int i , j , maxis , minis , maxij , minij , val , ic , jc;
/// jos includ mini, sus includ maxi
for (i = 1 ; i <= n ; i++){
for (j = 1 ; j <= m ; j++)
where[i][j] = 0;
}
for (j = 1 ; j <= m ; j++){
for (i = 1 ; i <= n ; i++){
if (a[i][j] == mini)
break;
}
h[j] = max(n - i + 1 , h[j - 1]); /// fie iau exact cat trb fie mai mult
for (i = 1 ; i <= h[j] ; i++)
where[n - i + 1][j] = 1; /// apartine tarii de jos
}
minij = minis = 2000000000;
maxij = maxis = -2000000000;
/// e clar ca toate mini urile sunt jos
for (i = 1 ; i <= n ; i++){
for (j = 1 ; j <= m ; j++){
if (where[i][j] == 0){
minis = min (minis , a[i][j]);
maxis = max (maxis , a[i][j]);
}
else {
minij = min (minij , a[i][j]);
maxij = max (maxij , a[i][j]);
}
}
}
/// daca exista vreun maxi jos, nu e ok
if (maxij == maxi)
return;
/// altfel, toate mini urile sunt jos, toate maxi urile sunt sus
while (!down.empty())
down.pop();
while (!up.empty())
up.pop();
for (j = 1 ; j <= m ; j++){
if (j == m || h[j] < h[j + 1])
down.push( make_pair( -a[n - h[j]][j] , make_pair(n - h[j] , j)) );
}
/// in down pui candidatii
for (i = 1 ; i <= n ; i++){
for (j = 1 ; j <= m ; j++){
if (where[i][j] == 0){
up.push( make_pair( -a[i][j] , make_pair(i , j)) );
}
}
}
while (!down.empty()){
val = -down.top().first;
ic = down.top().second.first;
jc = down.top().second.second;
down.pop();
/// val = cea mai mica val cu care pot sa extind
maxij = max(maxij , val);
if (maxij == maxi)
return;
where[ic][jc] = 1;
h[jc]++;
if (h[jc - 1] + 1 == h[jc] && jc - 1 != 0 && n - h[jc - 1] > 0)
down.push( make_pair( -a[n - h[jc - 1]][jc - 1] , make_pair(n - h[jc - 1] , jc - 1)) );
if ((jc == m || h[jc] != h[jc + 1]) && n - h[jc] > 0)
down.push( make_pair( -a[n - h[jc]][jc] , make_pair(n - h[jc] , jc)) );
val = -up.top().first;
minis = val;
ic = up.top().second.first;
jc = up.top().second.second;
while (where[ic][jc]){
up.pop();
if (up.empty())
break;
val = -up.top().first;
ic = up.top().second.first;
jc = up.top().second.second;
minis = val;
}
if (!up.empty())
sol = min(sol , max(maxis - minis , maxij - minij));
else break;
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int i , j , st , dr;
fscanf (fin,"%d%d",&n,&m);
mini = 2000000000;
maxi = -2000000000;
for (i = 1 ; i <= n ; i++){
for (j = 1 ; j <= m ; j++){
fscanf (fin,"%d",&a[i][j]);
mini = min(mini , a[i][j]);
maxi = max(maxi , a[i][j]);
}
}
sol = maxi - mini;
solve();
for (i = 1 ; i <= n ; i++){
st = 1;
dr = m;
while (st < dr){
swap(a[i][st] , a[i][dr]);
st++;
dr--;
}
}
solve();
for (j = 1 ; j <= m ; j++){
st = 1;
dr = n;
while (st < dr){
swap(a[st][j] , a[dr][j]);
st++;
dr--;
}
}
solve();
for (i = 1 ; i <= n ; i++){
st = 1;
dr = m;
while (st < dr){
swap(a[i][st] , a[i][dr]);
st++;
dr--;
}
}
solve();
fprintf (fout,"%d",sol);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:133:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d",&n,&m);
~~~~~~~^~~~~~~~~~~~~~~~~~
joioi.cpp:140:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d",&a[i][j]);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
10 ms |
2304 KB |
Output is correct |
17 |
Correct |
29 ms |
3452 KB |
Output is correct |
18 |
Correct |
32 ms |
3452 KB |
Output is correct |
19 |
Correct |
34 ms |
3452 KB |
Output is correct |
20 |
Correct |
28 ms |
3196 KB |
Output is correct |
21 |
Correct |
33 ms |
3452 KB |
Output is correct |
22 |
Correct |
25 ms |
3452 KB |
Output is correct |
23 |
Correct |
34 ms |
3452 KB |
Output is correct |
24 |
Correct |
30 ms |
3196 KB |
Output is correct |
25 |
Correct |
30 ms |
3452 KB |
Output is correct |
26 |
Correct |
28 ms |
3452 KB |
Output is correct |
27 |
Correct |
28 ms |
3452 KB |
Output is correct |
28 |
Correct |
30 ms |
3452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
10 ms |
2304 KB |
Output is correct |
17 |
Correct |
29 ms |
3452 KB |
Output is correct |
18 |
Correct |
32 ms |
3452 KB |
Output is correct |
19 |
Correct |
34 ms |
3452 KB |
Output is correct |
20 |
Correct |
28 ms |
3196 KB |
Output is correct |
21 |
Correct |
33 ms |
3452 KB |
Output is correct |
22 |
Correct |
25 ms |
3452 KB |
Output is correct |
23 |
Correct |
34 ms |
3452 KB |
Output is correct |
24 |
Correct |
30 ms |
3196 KB |
Output is correct |
25 |
Correct |
30 ms |
3452 KB |
Output is correct |
26 |
Correct |
28 ms |
3452 KB |
Output is correct |
27 |
Correct |
28 ms |
3452 KB |
Output is correct |
28 |
Correct |
30 ms |
3452 KB |
Output is correct |
29 |
Correct |
1151 ms |
79860 KB |
Output is correct |
30 |
Correct |
1218 ms |
81300 KB |
Output is correct |
31 |
Correct |
1297 ms |
81376 KB |
Output is correct |
32 |
Correct |
1197 ms |
81372 KB |
Output is correct |
33 |
Correct |
1169 ms |
77428 KB |
Output is correct |
34 |
Correct |
1287 ms |
81256 KB |
Output is correct |
35 |
Correct |
3156 ms |
81312 KB |
Output is correct |
36 |
Correct |
3490 ms |
81200 KB |
Output is correct |
37 |
Execution timed out |
4074 ms |
81380 KB |
Time limit exceeded |