이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "jumps.h"
#include<queue>
#include <vector>
#include <iostream>
using namespace std;
std::vector<int> H;
vector<int> nxt;
vector<int> prv;
bool subtask1=1;
void init(int N, std::vector<int> nH)
{
for(int i = 0; i < N; i++){
if(nH[i]!=i+1) subtask1=0;
}
H = nH;
nxt.resize(N);
prv.resize(N);
{
vector<int> kmon;
for (int i = 0; i < N; i++)
{
if (kmon.empty())
{
kmon.push_back(i);
prv[i] = -1;
continue;
}
while (H[kmon.back()] < H[i])
{
kmon.pop_back();
if (kmon.empty())
{
kmon.push_back(i);
prv[i] = -1;
goto nd;
}
}
prv[i] = kmon.back();
kmon.push_back(i);
nd:;
}
}
{
vector<int> kmon;
for (int i = N-1; i>=0; i--)
{
if (kmon.empty())
{
kmon.push_back(i);
nxt[i] = -1;
continue;
}
while (H[kmon.back()] < H[i])
{
kmon.pop_back();
if (kmon.empty())
{
kmon.push_back(i);
nxt[i] = -1;
goto nd2;
}
}
nxt[i] = kmon.back();
kmon.push_back(i);
nd2:;
}
}
}
int minimum_jumps(int A, int B, int C, int D)
{
if (subtask1) return C-B;
int mcd = 0;
for (int i = C; i <= D; i++)
{
mcd = max(mcd, H[i]);
}
int bs = 0;
for (int i = B; i >= A; i--)
{
// cerr << H[i] << ' ';
if (H[i] < mcd)
bs = max(bs, i);
else
break;
}
// cerr << bs << '\n';
vector<bool> visited(H.size(), 0);
queue<pair<int, int>> q;
for(int i = A; i <=B; i++)
q.push({i, 0});
while(!q.empty()){
auto x = q.front();
q.pop();
if(visited[x.first]) continue;
visited[x.first] = 1;
if(x.first >= C && x.first <= D) return x.second;
if(nxt[x.first]!=-1) q.push({nxt[x.first], x.second+1});
if(prv[x.first]!=-1) q.push({prv[x.first], x.second+1});
}
return -1;
}
# | 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... |