Submission #310229

# Submission time Handle Problem Language Result Execution time Memory
310229 2020-10-06T10:35:26 Z bigDuck Climbers (RMI18_climbers) C++14
100 / 100
754 ms 220728 KB
#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


/*ifstream fin("climbers.in");
#define cin fin
*/
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){
if(h[p3]>=h[p2]){
    return (h[p1]>=h[p2] && h[p1]<=h[p3]);
}
else{
    return (h[p1]>=h[p3] && h[p1]<=h[p2]);
}
}

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()){if(d0<d[p][l] ){d[p][l]=d0;ms.erase(it);  ms.insert({d[p][l], {p, l} }); } }
    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+1, l-1, 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(p-1, l-1, d[p][l]+abs(h[p-1]-h[p]));
        add_edge(l-1, p-1, d[p][l]+abs(h[l-1]-h[p]));

}

if(h[p]==h[l+1]){
        add_edge(p+1, l+1, d[p][l]+abs(h[p+1]-h[p]));
        add_edge(l+2, p, d[p][l]+abs(h[l+2]-h[p]));
        add_edge(p-1, l+1, d[p][l]+abs(h[p-1]-h[p]));
        add_edge(l+2, p-1, d[p][l]+abs(h[l+2]-h[p]));
}


if(p==l){
    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]));
    return;
}


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]));
}







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;
    }
}
int res=((int)1e17);
//cout<<res<<"\n"<<flush;
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;
    if(x==y){
        cout<<d[x][y]<<"\n"; return 0;
    }
    vr[x][y]=true;
    edges(x, y);
}

for(int i=1; i<=n; i++){
    if(d[i][i]!=(-1) ){
        res=min(res, d[i][i]);
    }
}
if(res==((int)1e17) ){cout<<"-1"; return 0;}
cout<<res;

return 0;
}



# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 3 ms 3456 KB Output is correct
3 Correct 13 ms 16512 KB Output is correct
4 Correct 65 ms 96504 KB Output is correct
5 Correct 179 ms 220728 KB Output is 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 2 ms 1792 KB Output is correct
4 Correct 1 ms 1280 KB Output is correct
5 Correct 4 ms 4864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1280 KB Output is correct
2 Correct 5 ms 3488 KB Output is correct
3 Correct 244 ms 16760 KB Output is correct
4 Correct 432 ms 96508 KB Output is correct
5 Correct 606 ms 96884 KB Output is correct
6 Correct 580 ms 160396 KB Output is correct
7 Correct 662 ms 159736 KB Output is correct
8 Correct 571 ms 220536 KB Output is correct
9 Correct 686 ms 219512 KB Output is correct
10 Correct 754 ms 219896 KB Output is correct