#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define len(a) (int)a.size()
#define fi first
#define sc second
#define d1(w) cerr<<#w<<":"<<w<<endl;
#define d2(w,c) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<endl;
#define d3(w,c,z) cerr<<#w<<":"<<w<<" "<<#c<<":"<<c<<" "<<#z<<":"<<z<<endl;
#define left ind+ind
#define right ind+ind+1
#define mid (l+r)/2
#define FAST_IO ios_base::sync_with_stdio(false);
#define endl '\n'
const int maxn = 620;
const long long LINF = 1e18;
const int LOG = 31;
const int INF = 1e9;
const int N = 1e3 + 5;
const int M = 4;
const int SQ = 350;
const int MOD = 998244353;
typedef pair <int,int> pii;
struct node {
int val,x,y;
};
bool operator < (node a ,node b) {
return a.val < b.val;
}
vector <pair<int,pii> > v;
priority_queue <node> Q;
int n,m,mat[N][N],vis[N][N],ans = -INF,way[4][2] = {{1,0},{0,1},{0,-1},{-1,0}},mx,my;
int main() {
scanf("%d %d",&n,&m);
for (short int i = 1 ; i <= n ; i++)
for (short int j = 1 ; j <= m ; j++) {
scanf("%d",&mat[i][j]);
vis[i][j] = -INF;
v.pb(mp(mat[i][j],mp(i,j)));
}
sort(v.begin(),v.end());
Q.push({v.back().fi,v.back().sc.fi,v.back().sc.sc});
v.pop_back();
while(len(Q)) {
auto temp = Q.top();
Q.pop();
int val = temp.val,x = temp.x,y = temp.y;
if (vis[x][y] > val) continue;
ans = max(ans,val - mat[x][y] - 1);
while(len(v) && v.back().fi == val) {
Q.push({v.back().fi,v.back().sc.fi,v.back().sc.sc});
v.pop_back();
}
for (short int i = 0 ; i < 4 ; i++) {
short int nx = x + way[i][0] , ny = y + way[i][1];
if (nx > 0 && nx <= n && ny > 0 && ny <= m && val - 1 > vis[nx][ny]) {
vis[nx][ny] = val - 1;
Q.push({val - 1,nx,ny});
}
}
}
printf("%d\n",ans);
}
Compilation message
maxcomp.cpp: In function 'int main()':
maxcomp.cpp:44: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:47:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&mat[i][j]);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
532 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
644 KB |
Output is correct |
6 |
Correct |
2 ms |
644 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
3 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
684 KB |
Output is correct |
2 |
Correct |
2 ms |
684 KB |
Output is correct |
3 |
Correct |
3 ms |
684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
532 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
644 KB |
Output is correct |
6 |
Correct |
2 ms |
644 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
3 ms |
684 KB |
Output is correct |
9 |
Correct |
3 ms |
996 KB |
Output is correct |
10 |
Correct |
4 ms |
996 KB |
Output is correct |
11 |
Correct |
3 ms |
996 KB |
Output is correct |
12 |
Correct |
4 ms |
996 KB |
Output is correct |
13 |
Correct |
4 ms |
996 KB |
Output is correct |
14 |
Correct |
3 ms |
996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
2 ms |
532 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
2 ms |
644 KB |
Output is correct |
6 |
Correct |
2 ms |
644 KB |
Output is correct |
7 |
Correct |
2 ms |
684 KB |
Output is correct |
8 |
Correct |
3 ms |
684 KB |
Output is correct |
9 |
Correct |
3 ms |
684 KB |
Output is correct |
10 |
Correct |
2 ms |
684 KB |
Output is correct |
11 |
Correct |
3 ms |
684 KB |
Output is correct |
12 |
Correct |
3 ms |
996 KB |
Output is correct |
13 |
Correct |
4 ms |
996 KB |
Output is correct |
14 |
Correct |
3 ms |
996 KB |
Output is correct |
15 |
Correct |
4 ms |
996 KB |
Output is correct |
16 |
Correct |
4 ms |
996 KB |
Output is correct |
17 |
Correct |
3 ms |
996 KB |
Output is correct |
18 |
Correct |
473 ms |
20368 KB |
Output is correct |
19 |
Correct |
475 ms |
20368 KB |
Output is correct |
20 |
Correct |
488 ms |
20368 KB |
Output is correct |
21 |
Correct |
425 ms |
20368 KB |
Output is correct |
22 |
Correct |
440 ms |
20428 KB |
Output is correct |
23 |
Correct |
495 ms |
20428 KB |
Output is correct |
24 |
Correct |
475 ms |
30848 KB |
Output is correct |