# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1094117 |
2024-09-28T12:59:49 Z |
ASN49K |
Climbers (RMI18_climbers) |
C++14 |
|
153 ms |
196180 KB |
#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
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 time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
4 ms |
8284 KB |
Output is correct |
4 |
Correct |
33 ms |
70740 KB |
Output is correct |
5 |
Correct |
95 ms |
196180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
35 ms |
7700 KB |
Output is correct |
4 |
Correct |
80 ms |
64592 KB |
Output is correct |
5 |
Correct |
100 ms |
64004 KB |
Output is correct |
6 |
Correct |
125 ms |
122192 KB |
Output is correct |
7 |
Correct |
126 ms |
115076 KB |
Output is correct |
8 |
Correct |
146 ms |
176340 KB |
Output is correct |
9 |
Correct |
153 ms |
179756 KB |
Output is correct |
10 |
Correct |
153 ms |
175608 KB |
Output is correct |