This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define qc ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rt return
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef long double ld;
const long long mod=1e9+7;
const long long inf=1e9+7;
using namespace std;
ll gcd(ll a,ll b){if(b==0)rt a;rt gcd(b,a%b);}
int n;
vector<vector<int>>gph,rch,pfx;
void init(int N,vector<int>h)
{
n=N;
gph.resize(n);
for(int i=0;i<n;i++){
for(int j=i-1;j>=0;j--){
if(h[j]>h[i]){
gph[i].push_back(j);
break;
}
}
for(int j=i+1;j<n;j++){
if(h[j]>h[i]){
gph[i].push_back(j);
break;
}
}
}
rch.resize(n,vector<int>(n,inf));
for(int i=0;i<n;i++){
queue<pair<int,int>>q;q.push({0,i});
while(!q.empty()){
pair<int,int> f=q.front();
q.pop();
rch[i][f.second]=min(f.first,rch[i][f.second]);
for(auto c:gph[f.second]){
q.push({f.first+1,c});
}
}
}
pfx.resize(n,vector<int>(n+1));
for(int i=0;i<n;i++){
pfx[i][0]=0;
for(int j=0;j<n;j++){
if(j<=i)pfx[i][j+1]=0;
else{
if(rch[i][j]==inf)pfx[i][j+1]=pfx[i][j];
else pfx[i][j+1]=pfx[i][j]+1;
}
}
}
rt;
}
int pcs(int i,int c,int d)
{
if(c==d)rt rch[i][c];
int mid=(c+d)/2;
if(pfx[i][mid+1]==pfx[i][c])rt pcs(i,mid+1,d);
else rt pcs(i,c,mid);
}
int minimum_jumps(int a,int b,int c,int d)
{
int ans=inf;
for(int i=a;i<=b;i++){
if(pfx[i][d+1]-pfx[i][c]==0)continue;
int num=pcs(i,c,d);
ans=min(ans,num);
}
if(ans==inf)rt -1;
rt ans;
}
/*
int main()
{
init(7, {3, 2, 1, 6, 4, 5, 7});
int ans=minimum_jumps(4, 4, 6, 6);
cout<<ans<<'\n';
for(auto c:pfx){
for(auto d:c)cout<<d<<" ";cout<<"\n";
}
cout<<"\n";
for(auto c:rch){
for(auto d:c)cout<<d<<" ";cout<<"\n";
}
rt 0;
}*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |