#include<bits/stdc++.h>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
//#define int ll
int n,h[5010];
int d[5010][5010];
multiset<pair<int, pii>> ms;
bool vr[5010][5010];
bool btwn(int p1, int p2, int p3){
return ( (h[p1]<=max(h[p2], h[p3])) && (h[p1]>=min(h[p2], h[p3]) ) );
}
void add_edge(int p, int l, int d0){
if( ((l>=n) || (p<=0))|| (vr[p][l]==true) || (!btwn(p, l, l+1)) || (l<=0) || (p>n) ){return;}
auto it=ms.find({d[p][l], {p, l} });
if(it!=ms.end()){ms.erase(it); d[p][l]=min(d[p][l], d0); }
else{d[p][l]=d0; }
ms.insert({d[p][l], {p, l} });
}
void edges(int p, int l){
if( (h[p]==h[l]) ){
add_edge(p, l-1, d[p][l]);
if(btwn(p+1, l-1, l) ){
add_edge(p+1, l-1, d[p][l]+abs(h[p+1]-h[p]) );
}
if(btwn(l-1, p, p+1)){
add_edge(l-1, p, d[p][l]+abs(h[l-1]-h[p]));
}
if(btwn(p-1, l, l-1)){
add_edge(p-1, l-1, d[p][l]+abs(h[p-1]-h[p]));
}
if(btwn(l-1, p-1, p) ){
add_edge(l-1, p-1, d[p][l]+abs(h[l-1]-h[p]));
}
}
if(h[p]==h[l+1]){
if(btwn(p+1, l+1, l+2)){
add_edge(p+1, l+1, d[p][l]+abs(h[p+1]-h[p]));
}
if(btwn(l+2, p, p+1)){
add_edge(l+2, p, d[p][l]+abs(h[l+2]-h[p]));
}
if(btwn(p-1, l+1, l+2)){
add_edge(p-1, l+1, d[p][l]+abs(h[p-1]-h[p]));
}
if(btwn(l+2, p, p-1)){
add_edge(l+2, p-1, d[p][l]+abs(h[l+2]-h[p]));
}
}
add_edge(p+1, l, d[p][l]+abs(h[p+1]-h[p]));
add_edge(l+1, p, d[p][l]+abs(h[l+1]-h[p]));
add_edge(l, p, d[p][l]+abs(h[l]-h[p]));
add_edge(p-1, l, d[p][l]+abs(h[p-1]-h[p]));
add_edge(l+1, p-1, d[p][l]+abs(h[l+1]-h[p]));
add_edge(l, p-1, d[p][l]+abs(h[l]-h[p]));
}
void dijkstra(){
d[n][1]=0;
ms.insert({0, {n, 1} } );
while(!ms.empty()){
auto f=*ms.begin(); ms.erase(ms.begin());
int x=f.sc.ft, y=f.sc.sc;
d[x][y]=f.ft;
vr[x][y]=true;
edges(x, y);
}
return;
}
int32_t main(){
INIT
cin>>n;
for(int i=1; i<=n; i++){
cin>>h[i];
for(int j=1; j<=n;j++){
d[i][j]=-1;
}
}
dijkstra();
int res=1e9;
for(int i=1; i<=n; i++){
if(d[i][i]!=-1){
res=min(res, d[i][i]);
}
}
cout<<res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1152 KB |
Output is correct |
2 |
Correct |
4 ms |
3200 KB |
Output is correct |
3 |
Correct |
21 ms |
13056 KB |
Output is correct |
4 |
Incorrect |
100 ms |
62456 KB |
Output isn't correct |
5 |
Incorrect |
232 ms |
122872 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
4 ms |
1664 KB |
Output is correct |
4 |
Correct |
3 ms |
1152 KB |
Output is correct |
5 |
Correct |
11 ms |
4352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1152 KB |
Output is correct |
2 |
Correct |
19 ms |
3200 KB |
Output is correct |
3 |
Execution timed out |
950 ms |
13308 KB |
Time limit exceeded |
4 |
Execution timed out |
1086 ms |
61944 KB |
Time limit exceeded |
5 |
Execution timed out |
1098 ms |
61048 KB |
Time limit exceeded |
6 |
Execution timed out |
1104 ms |
97528 KB |
Time limit exceeded |
7 |
Execution timed out |
1096 ms |
95096 KB |
Time limit exceeded |
8 |
Execution timed out |
1100 ms |
122744 KB |
Time limit exceeded |
9 |
Execution timed out |
1101 ms |
117768 KB |
Time limit exceeded |
10 |
Execution timed out |
1101 ms |
119032 KB |
Time limit exceeded |