#include <bits/stdc++.h>
#define DIM 2010
#define DIMBUFF 30000000
#define INF 2000000000
using namespace std;
int a[DIM][DIM],b[DIM][DIM],viz[DIM][DIM];
struct idk{
int val,l,c;
} v[DIM*DIM];
deque <pair<int,int> > c;
int di[] = {-1,1,0,0};
int dj[] = {0,0,-1,1};
int n,m,i,j,sol,mini,maxi,k,pos;
char buff[DIMBUFF];
int verif (int val){
/// toate valorile < maxi - val trb puse in zona minimelor
/*for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
viz[i][j] = 0;*/
memset (viz,0,sizeof viz);
for (int i=1;i<=k;++i){
if (v[i].val >= maxi - val)
break;
int lin = v[i].l, col = v[i].c;
if (viz[lin][col])
continue;
c.clear();
c.push_back(make_pair(lin,col));
viz[lin][col] = 1;
while (!c.empty()){
int ic = c.front().first;
int jc = c.front().second;
c.pop_front();
for (int dir=0;dir<=3;++dir){
int iv = ic + di[dir];
int jv = jc + dj[dir];
if (iv >= lin && iv <= n && jv >= 1 && jv <= col && !viz[iv][jv]){
viz[iv][jv] = 1;
c.push_back(make_pair(iv,jv));
}}}}
int maxim = 0, maxim2 = 0, minim = INF, minim2 = INF;
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j){
if (viz[i][j]){
minim = min (minim,a[i][j]);
maxim = max (maxim,a[i][j]);
} else {
minim2 = min (minim2,a[i][j]);
maxim2 = max (maxim2,a[i][j]);
}}
if (max (maxim - minim, maxim2 - minim2) > val)
return 0;
return 1;
}
void solve (){
int st = 0, dr = maxi - mini, val;
while (st <= dr){
int mid = (st+dr)>>1;
if (verif (mid)){
val = mid;
dr = mid-1;
} else st = mid+1;
}
sol = min (sol,val);
}
inline int cmp (idk a, idk b){
return a.val < b.val;
}
void roteste(){
for (i=1;i<=n;++i)
for (j=1;j<=m;++j)
b[m-j+1][i] = a[i][j];
swap (n,m);
k = 0;
for (i=1;i<=n;++i)
for (j=1;j<=m;++j){
a[i][j] = b[i][j];
v[++k] = {a[i][j],i,j};
}
sort (v+1,v+k+1,cmp);
}
int get_nr(){
while (!(buff[pos] >= '0' && buff[pos] <= '9'))
++pos;
int nr = 0;
while (buff[pos] >= '0' && buff[pos] <= '9'){
nr = nr*10 + buff[pos] - '0';
++pos;
}
return nr;
}
int main (){
//FILE *fin = fopen ("date.in","r");
//FILE *fout = fopen ("date.out","w");
FILE *fin = stdin;
FILE *fout = stdout;
fread (buff,1,DIMBUFF,fin);
//cin>>n>>m;
n = get_nr(), m = get_nr();
mini = INF;
for (i=1;i<=n;++i)
for (j=1;j<=m;++j){
//cin>>a[i][j];
a[i][j] = get_nr();
mini = min (mini,a[i][j]);
maxi = max (maxi,a[i][j]);
v[++k] = {a[i][j],i,j};
}
sort (v+1,v+k+1,cmp);
sol = INF;
solve();
roteste();
solve();
roteste();
solve();
roteste();
solve();
fprintf(fout,"%d",sol);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:113:11: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
fread (buff,1,DIMBUFF,fin);
~~~~~~^~~~~~~~~~~~~~~~~~~~
joioi.cpp: In function 'void solve()':
joioi.cpp:64:35: warning: 'val' may be used uninitialized in this function [-Wmaybe-uninitialized]
int st = 0, dr = maxi - mini, val;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
16256 KB |
Output is correct |
2 |
Correct |
40 ms |
16256 KB |
Output is correct |
3 |
Correct |
241 ms |
16248 KB |
Output is correct |
4 |
Correct |
242 ms |
16376 KB |
Output is correct |
5 |
Correct |
236 ms |
16248 KB |
Output is correct |
6 |
Correct |
246 ms |
16248 KB |
Output is correct |
7 |
Correct |
245 ms |
16248 KB |
Output is correct |
8 |
Correct |
249 ms |
16376 KB |
Output is correct |
9 |
Correct |
246 ms |
16376 KB |
Output is correct |
10 |
Correct |
249 ms |
16248 KB |
Output is correct |
11 |
Correct |
245 ms |
16352 KB |
Output is correct |
12 |
Correct |
250 ms |
16376 KB |
Output is correct |
13 |
Correct |
243 ms |
16248 KB |
Output is correct |
14 |
Correct |
247 ms |
16376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
16256 KB |
Output is correct |
2 |
Correct |
40 ms |
16256 KB |
Output is correct |
3 |
Correct |
241 ms |
16248 KB |
Output is correct |
4 |
Correct |
242 ms |
16376 KB |
Output is correct |
5 |
Correct |
236 ms |
16248 KB |
Output is correct |
6 |
Correct |
246 ms |
16248 KB |
Output is correct |
7 |
Correct |
245 ms |
16248 KB |
Output is correct |
8 |
Correct |
249 ms |
16376 KB |
Output is correct |
9 |
Correct |
246 ms |
16376 KB |
Output is correct |
10 |
Correct |
249 ms |
16248 KB |
Output is correct |
11 |
Correct |
245 ms |
16352 KB |
Output is correct |
12 |
Correct |
250 ms |
16376 KB |
Output is correct |
13 |
Correct |
243 ms |
16248 KB |
Output is correct |
14 |
Correct |
247 ms |
16376 KB |
Output is correct |
15 |
Correct |
45 ms |
17792 KB |
Output is correct |
16 |
Correct |
61 ms |
18552 KB |
Output is correct |
17 |
Correct |
239 ms |
18936 KB |
Output is correct |
18 |
Correct |
247 ms |
18988 KB |
Output is correct |
19 |
Correct |
236 ms |
18936 KB |
Output is correct |
20 |
Correct |
235 ms |
18808 KB |
Output is correct |
21 |
Correct |
330 ms |
18936 KB |
Output is correct |
22 |
Correct |
334 ms |
19064 KB |
Output is correct |
23 |
Correct |
329 ms |
19064 KB |
Output is correct |
24 |
Correct |
333 ms |
18892 KB |
Output is correct |
25 |
Correct |
337 ms |
18936 KB |
Output is correct |
26 |
Correct |
334 ms |
18936 KB |
Output is correct |
27 |
Correct |
323 ms |
18936 KB |
Output is correct |
28 |
Correct |
322 ms |
19064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
16256 KB |
Output is correct |
2 |
Correct |
40 ms |
16256 KB |
Output is correct |
3 |
Correct |
241 ms |
16248 KB |
Output is correct |
4 |
Correct |
242 ms |
16376 KB |
Output is correct |
5 |
Correct |
236 ms |
16248 KB |
Output is correct |
6 |
Correct |
246 ms |
16248 KB |
Output is correct |
7 |
Correct |
245 ms |
16248 KB |
Output is correct |
8 |
Correct |
249 ms |
16376 KB |
Output is correct |
9 |
Correct |
246 ms |
16376 KB |
Output is correct |
10 |
Correct |
249 ms |
16248 KB |
Output is correct |
11 |
Correct |
245 ms |
16352 KB |
Output is correct |
12 |
Correct |
250 ms |
16376 KB |
Output is correct |
13 |
Correct |
243 ms |
16248 KB |
Output is correct |
14 |
Correct |
247 ms |
16376 KB |
Output is correct |
15 |
Correct |
45 ms |
17792 KB |
Output is correct |
16 |
Correct |
61 ms |
18552 KB |
Output is correct |
17 |
Correct |
239 ms |
18936 KB |
Output is correct |
18 |
Correct |
247 ms |
18988 KB |
Output is correct |
19 |
Correct |
236 ms |
18936 KB |
Output is correct |
20 |
Correct |
235 ms |
18808 KB |
Output is correct |
21 |
Correct |
330 ms |
18936 KB |
Output is correct |
22 |
Correct |
334 ms |
19064 KB |
Output is correct |
23 |
Correct |
329 ms |
19064 KB |
Output is correct |
24 |
Correct |
333 ms |
18892 KB |
Output is correct |
25 |
Correct |
337 ms |
18936 KB |
Output is correct |
26 |
Correct |
334 ms |
18936 KB |
Output is correct |
27 |
Correct |
323 ms |
18936 KB |
Output is correct |
28 |
Correct |
322 ms |
19064 KB |
Output is correct |
29 |
Execution timed out |
4066 ms |
114468 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |