Submission #1094117

#TimeUsernameProblemLanguageResultExecution timeMemory
1094117ASN49KClimbers (RMI18_climbers)C++14
100 / 100
153 ms196180 KiB
#include <bits/stdc++.h> using namespace std; using i64=long long; #define UNUSED -1 #define all(x) x.begin(),x.end() #define pb push_back #define int long long const int mod=1e9+7,inf=1e9+1; const i64 INF=1e18; struct node { int nod1,nod2; long long cost; bool operator <(const node& b) const { return cost>b.cost; } }; bool between(int a,int b,int c) { return (a<=b && b<=c) || (a>=b && b>=c); } void modify_h(vector<int>&h) { vector<int>sol; for(auto &c:h) { while(sol.size()>=2 && between(sol[sol.size()-2],sol.back(),c)) { sol.pop_back(); } sol.push_back(c); } h=sol; } main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; vector<int>h(n); for(auto &c:h) { cin>>c; } modify_h(h); n=(int)h.size(); priority_queue<node>q; vector<vector<i64>>dist(n,vector<i64>(n-1,INF)); dist[0][n-2]=0; q.push({0,n-2,0}); auto add=[&](int x,int y,i64 cost) { if(dist[x][y]>cost) { dist[x][y]=cost; q.push({x,y,cost}); } }; while(q.size()) { auto then=q.top(); q.pop(); int nod1=then.nod1,nod2=then.nod2; if(then.cost != dist[nod1][nod2]) { continue; } if(nod1==nod2 || nod1==nod2+1) { cout<<then.cost; return 0; } //we move to nod1+1 if(nod1+1<n && (between(h[nod1],h[nod1+1],h[nod2]) || between(h[nod1],h[nod1+1],h[nod2+1]))) { add(nod1+1,nod2,then.cost+abs(h[nod1+1]-h[nod1])); } if(nod1-1>=0 && between(h[nod1],h[nod1-1],h[nod2]) || between(h[nod1],h[nod1-1],h[nod2+1])) { add(nod1-1,nod2,then.cost+abs(h[nod1-1]-h[nod1])); } if(nod1+1<n && between(h[nod1],h[nod2],h[nod1+1])) { add(nod2,nod1,then.cost+abs(h[nod2]-h[nod1])); } if(nod1+1<n && between(h[nod1],h[nod2+1],h[nod1+1])) { add(nod2+1,nod1,then.cost+abs(h[nod2+1]-h[nod1])); } if(nod1>0 && between(h[nod1],h[nod2],h[nod1-1])) { add(nod2,nod1-1,then.cost+abs(h[nod2]-h[nod1])); } if(nod1>0 && between(h[nod1],h[nod2+1],h[nod1-1])) { add(nod2+1,nod1-1,then.cost+abs(h[nod2+1]-h[nod1])); } } cout<<"NO"; }

Compilation message (stderr)

climbers.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   38 | main()
      | ^~~~
climbers.cpp: In function 'int main()':
climbers.cpp:83:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   83 |         if(nod1-1>=0 && between(h[nod1],h[nod1-1],h[nod2]) || between(h[nod1],h[nod1-1],h[nod2+1]))
      |            ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...