#include <bits/stdc++.h>
#define fi first
#define se second
#define lo long long
#define inf 1000000000
#define md 1000000007
#define li 1004
#define mp make_pair
#define pb push_back
#define pii pair<int , pair<int , int> >
using namespace std;
struct node{
int val, x, y;
};
bool operator < (node a, node b){
return a.val < b.val;
}
int n, m, A[li][li], vis[li][li], ans = - inf;
int yon1[6] = {1, - 1, 0, 0};
int yon2[6] = {0, 0, 1, - 1};
queue<node> q;
vector< pair<int , pair<int, int> > > v;
int main(){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
scanf("%d", &A[i][j]);
vis[i][j] = - inf;
v.pb(mp(A[i][j], mp(i, j)));
}
}
sort(v.begin(), v.end());
q.push({v.back().fi, v.back().se.fi, v.back().se.se});
v.pop_back();
while(! q.empty()){
node temp = q.front();
q.pop();
int val = temp.val;
int x = temp.x;
int y = temp.y;
if(vis[x][y] > val) continue;
ans = max(ans, val - A[x][y] - 1);
while((int)v.size() && v.back().fi == val){
q.push({v.back().fi, v.back().se.fi, v.back().se.se});
v.pop_back();
}
for(int i = 0; i <= 3 ; i ++){
int dx = x + yon1[i];
int dy = y + yon2[i];
if(dx >= 1 && dx <= n && dy >= 1 && dy <= m && val - 1 > vis[dx][dy]){
vis[dx][dy] = val - 1;
q.push({val - 1, dx, dy});
}
}
}
printf("%d\n", ans);
return 0;
}
Compilation message
maxcomp.cpp: In function 'int main()':
maxcomp.cpp:24:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
maxcomp.cpp:27:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
628 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
672 KB |
Output is correct |
2 |
Correct |
3 ms |
700 KB |
Output is correct |
3 |
Correct |
2 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
628 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
636 KB |
Output is correct |
9 |
Correct |
3 ms |
1084 KB |
Output is correct |
10 |
Correct |
3 ms |
1104 KB |
Output is correct |
11 |
Correct |
3 ms |
1144 KB |
Output is correct |
12 |
Correct |
3 ms |
1296 KB |
Output is correct |
13 |
Correct |
4 ms |
1296 KB |
Output is correct |
14 |
Correct |
3 ms |
1296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
628 KB |
Output is correct |
6 |
Correct |
2 ms |
628 KB |
Output is correct |
7 |
Correct |
2 ms |
632 KB |
Output is correct |
8 |
Correct |
2 ms |
636 KB |
Output is correct |
9 |
Correct |
2 ms |
672 KB |
Output is correct |
10 |
Correct |
3 ms |
700 KB |
Output is correct |
11 |
Correct |
2 ms |
700 KB |
Output is correct |
12 |
Correct |
3 ms |
1084 KB |
Output is correct |
13 |
Correct |
3 ms |
1104 KB |
Output is correct |
14 |
Correct |
3 ms |
1144 KB |
Output is correct |
15 |
Correct |
3 ms |
1296 KB |
Output is correct |
16 |
Correct |
4 ms |
1296 KB |
Output is correct |
17 |
Correct |
3 ms |
1296 KB |
Output is correct |
18 |
Correct |
361 ms |
29256 KB |
Output is correct |
19 |
Correct |
365 ms |
37732 KB |
Output is correct |
20 |
Correct |
391 ms |
45364 KB |
Output is correct |
21 |
Correct |
359 ms |
54592 KB |
Output is correct |
22 |
Correct |
369 ms |
62992 KB |
Output is correct |
23 |
Correct |
366 ms |
71616 KB |
Output is correct |
24 |
Correct |
269 ms |
85424 KB |
Output is correct |