#include "jumps.h"
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
stack <int>st;
int v[200005];
int dpst[200005];
int dpdr[200005];
int nextst[200005];
int nextdr[200005];
int aint[4][400005];
int n;
int querry(int node,int st,int dr,int qst,int qdr,int tip)
{
if(st>qdr || qst>dr)
{
return 10000000;
}
else if(qst<=st && dr<=qdr)
{
return aint[tip][node];
}
else
{
int mij=(st+dr)/2,val1,val2;
val1=querry(node*2,st,mij,qst,qdr,tip);
val2=querry(node*2+1,mij+1,dr,qst,qdr,tip);
return min(val1,val2);
}
}
void build(int node,int st,int dr)
{
if(st==dr)
{
aint[0][node]=dpst[st];
aint[1][node]=dpdr[st];
return;
}
else if(st!=dr)
{
int mij=(st+dr)/2;
build(node*2,st,mij);
build(node*2+1,mij+1,dr);
aint[0][node]=min(aint[0][node*2],aint[0][node*2+1]);
aint[1][node]=min(aint[1][node*2],aint[1][node*2+1]);
return;
}
}
void init(int N, vector<int> H)
{
int i;
n=N;
for(i=1; i<=n; i++)
{
v[i]=H[i-1];
}
v[0]=1e8;
st.push(0);
dpst[0]=0;
for(i=1; i<=n; i++)
{
while(v[i]>v[st.top()])
{
st.pop();
}
nextst[i]=st.top();
dpst[i]=dpst[st.top()]+1;
st.push(i);
}
while(st.size())
{
st.pop();
}
st.push(n+1);
v[n+1]=1e8;
dpdr[n+1]=0;
for(i=n; i>=1; i--)
{
while(v[i]>v[st.top()])
{
st.pop();
}
nextdr[i]=st.top();
dpdr[i]=dpdr[st.top()]+1;
st.push(i);
}
build(1,1,n);
}
int minimum_jumps(int a, int b, int c, int d)
{
int i,k,curr;
a++;
b++;
c++;
d++;
if(a>b)
{
swap(a,b);
}
if(a<=c && b>=c)
{
return 0;
}
if(c<a)
{
if(nextdr[c]>a)
{
b=min(b,nextdr[c]-1);
return querry(1,1,n,a,b,0)-dpst[c];
}
else
{
return -1;
}
}
if(c>b)
{
if(nextst[c]<b)
{
a=max(a,nextst[c]+1);
return querry(1,1,n,a,b,1)-dpdr[c];
}
else
{
return -1;
}
}
return -1;
}
/*vector<int>vect;
int main ()
{
int ne,i,q,a,b,c,d,x;
cin>>ne;
for(i=1;i<=ne;i++)
{
cin>>x;
vect.push_back(x);
}
init(ne,vect);
cin>>q;
for(i=1;i<=q;i++)
{
cin>>a>>b>>c>>d;
cout<<minimum_jumps(a,b,c,d);
}
}*/
Compilation message
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:94:9: warning: unused variable 'i' [-Wunused-variable]
94 | int i,k,curr;
| ^
jumps.cpp:94:11: warning: unused variable 'k' [-Wunused-variable]
94 | int i,k,curr;
| ^
jumps.cpp:94:13: warning: unused variable 'curr' [-Wunused-variable]
94 | int i,k,curr;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Incorrect |
122 ms |
8896 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Incorrect |
276 ms |
4888 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Incorrect |
276 ms |
4888 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
336 KB |
Output is correct |
3 |
Incorrect |
122 ms |
8896 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |