#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
using namespace std;
#define INF 1000000000000000000
#define int long long int
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#ifdef wiwihorz
#define print(a...) cerr << "Line " << __LINE__ << ": ", kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
void kout() { cerr << endl; }
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
namespace solver {
int n, m;
vector<vector<int>> a, dis, vis;
vector<int> dirx = {0, 0, 1, -1};
vector<int> diry = {1, -1, 0, 0};
void init_(int _n, int _m) {
n = _n, m = _m;
a.assign(n, vector<int>(m, 0));
}
void update(int x, int y) {
vis[x][y] = 1;
rep(i, 0, 3) {
int nx = x + dirx[i], ny = y + diry[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
dis[x][y] = min(dis[x][y], dis[nx][ny] + 1);
}
queue<int> q;
q.push(x * m + y);
while(q.size()) {
int cur = q.front(); q.pop();
int cx = cur / m, cy = cur % m;
rep(i, 0, 3) {
int nx = x + dirx[i], ny = y + diry[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
if(dis[cx][cy] + 1 < dis[nx][ny]) {
dis[nx][ny] = dis[cx][cy] + 1;
q.push(nx * m + ny);
}
}
}
return;
}
int solve() {
vector<pii> b;
rep(i, 0, n - 1) rep(j, 0, m - 1) {
b.push_back({a[i][j], i * m + j});
}
sort(all(b));
int sz = b.size(), ans = 0;
rep(i, 0, sz - 1) {
int x = b[i].second / m, y = b[i].second % m;
dis.assign(n, vector<int>(m, INF));
vis.assign(n, vector<int>(m, 0));
dis[x][y] = 0, vis[x][y] = 1;
rep(j, i + 1, sz - 1) {
x = b[j].second / m, y = b[j].second % m;
update(x, y);
ans = max(ans, b[j].first - b[i].first - dis[x][y] - 1);
}
}
return ans;
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
init_(n, m);
rep(i, 0, n - 1) rep(j, 0, m - 1) {
cin >> a[i][j];
}
cout << solve() << "\n";
return 0;
}
Compilation message
maxcomp.cpp:4: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
4 | #pragma loop-opt(on)
|
maxcomp.cpp:20:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
maxcomp.cpp:20:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |