#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define fi first
#define se second
#define sz(x) (x).size()
#define pll pair<ll,ll >
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endln '\n'
#define cont continue
const ll MaxN = 1e3 + 100;
const ll LogN = 20;
ll Inf = 1e18;
const ll dx[4] = { 0,1,0,-1 };
const ll dy[4] = { 1,0,-1,0 };
bool used[MaxN][MaxN];
ll Mt[MaxN][MaxN];
ll dp[MaxN][MaxN];
struct el{
ll c, x, y;
};
int main() {
#ifdef LOCAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
//ifstream cin("points.in");
//ofstream cout("points.out");
#endif
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll N, M;
cin>>N>>M;
vector<el > St;
for(int i = 1;i<=N;i++){
for(int j =1;j<=M;j++){
cin>>Mt[i][j];
used[i][j] = 1;
St.pb({Mt[i][j], i, j});
}
}
sort(all(St),[=](el a, el b){
return a.c < b.c;
});
ll Ans = -Inf;
memset(dp, -1, sizeof(dp));
for(el a : St){
//cerr<<a.c <<' ' <<a.x <<' '<<a.y<<endln;
ll k = -1;
for(int i =0;i<4;i++){
if(used[a.x + dx[i]][a.y + dy[i]]){
if(Mt[a.x + dx[i]][a.y + dy[i]] < a.c){
k = max(k,a.c + dp[a.x + dx[i]][a.y + dy[i]] - Mt[a.x + dx[i]][a.y + dy[i]] - 1 );
}
}
}
dp[a.x][a.y] = k;
for(int i =0;i<4;i++){
if(used[a.x + dx[i]][a.y + dy[i]]){
if(Mt[a.x + dx[i]][a.y + dy[i]] == a.c){
dp[a.x][a.y] = max(dp[a.x][a.y], dp[a.x + dx[i]][a.y + dy[i]] - 1);
dp[a.x + dx[i]][a.y + dy[i]] = max(dp[a.x][a.y] - 1, dp[a.x + dx[i]][a.y + dy[i]]);
//k = max(k,dp[a.x + dx[i]][a.y + dy[i]] - Mt[a.x + dx[i]][a.y + dy[i]] - 1 );
}
}
}
//dp[a.x][a.y] = max(dp[a.x + dx[i]][a.y + dy[i]], dp[a.x][a.y]);
Ans = max(dp[a.x][a.y], Ans);
}
cout<<Ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9876 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9876 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
10 ms |
9876 KB |
Output is correct |
3 |
Correct |
10 ms |
9848 KB |
Output is correct |
4 |
Incorrect |
10 ms |
9848 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |