이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
int rng() {
return abs((int)mrand());
}
// Current challenge: finish CSES problemset!!
constexpr int nax = 200200, lg = 20;
int n;
int h[nax];
int par[nax][2];
int partree[nax];
int sparse[lg][nax];
void init(int N, vector<int> H) {
n = N;
for (int i=0;i<n;i++) {
h[i] = H[i]-1;
par[i][0] = par[i][1] = n;
}
for (int i=0;i<n;i++) {
sparse[0][i] = h[i];
}
for (int pw=1;pw<lg;pw++) {
for (int i=0;i<n;i++) {
sparse[pw][i] = max(sparse[pw-1][i],sparse[pw-1][min(n-1,i+(1<<(pw-1)))]);
}
}
h[n] = n;
vector<int> stk;
for (int i=0;i<n;i++) {
while(!stk.empty() && h[stk.back()] < h[i]) { stk.pop_back(); }
if (!stk.empty()) par[i][0] = stk.back();
stk.push_back(i);
}
stk.clear();
for (int i=n-1;i>=0;i--) {
while(!stk.empty() && h[stk.back()] < h[i]) { stk.pop_back(); }
if (!stk.empty()) par[i][1] = stk.back();
stk.push_back(i);
}
for (int i=0;i<n;i++) {
if (h[i] == n-1) { partree[i] = -1; continue; }
assert(h[par[i][0]] != h[par[i][1]]);
if (par[i][0] == n) {
partree[i] = par[i][1];
} else if (par[i][1] == n) {
partree[i] = par[i][0];
} else {
partree[i] = (h[par[i][0]] > h[par[i][1]] ? par[i][0] : par[i][1]);
}
}
}
int jump(int a,int b,pair<int,vector<vector<int>>> &t) {
if (h[a] > h[b]) { return nax; }
int x = a;
int ans = 0;
int w = -1;
assert(a!=b);
int bit = 31 - __builtin_clz(b-a+1);
if (max(sparse[bit][a],sparse[bit][b-(1<<bit)+1]) > h[b]) {
return nax;
}
vector<int> m(1,x);
while(x != b) {
if (h[par[x][0]] > h[b]) {
x = par[x][1];
assert(w != 0);
w = 1;
ans++;
} else if (h[par[x][1]] > h[b]) {
x = par[x][0];
assert(w != 1);
w = 0;
ans++;
} else {
x = (h[par[x][0]] > h[par[x][1]] ? par[x][0] : par[x][1]);
ans++;
}
m.push_back(x);
}
if (cmin(t.first,ans)) {
t.second.clear();
}
if (t.first==ans) {
t.second.push_back(m);
}
return ans;
}
int minimum_jumps(int a,int b,int c,int d) {
vector<int> D(n+1,n);
D[n] = 0;
vector<int> stk;
for (int i=a;i<=b;i++) {
stk.push_back(i);
D[i] = 0;
}
for (int i=0;i<stk.size();i++) {
int x = stk[i];
if (cmin(D[par[x][0]],D[x]+1)) {
stk.push_back(par[x][0]);
}
if (cmin(D[par[x][1]],D[x]+1)) {
stk.push_back(par[x][1]);
}
}
int ans = n;
for (int i=c;i<=d;i++) {
cmin(ans,D[i]);
}
return (ans == n ? -1 : ans);
/*int ans = nax;
pair<int,vector<vector<int>>> paths;
paths.first = nax;
for (int A=a;A<=b;A++) {
for (int C=c;C<=d;C++) {
cmin(ans,jump(A,C,paths));
}
}
for (auto &v: paths.second) {
for (int &x: v)
cout << h[x]+1 << " ";
cout << "\n";
}
cout << "----------------------\n";
return (ans == nax ? -1 : ans);*/
}
컴파일 시 표준 에러 (stderr) 메시지
jumps.cpp: In function 'int minimum_jumps(int, int, int, int)':
jumps.cpp:109:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int i=0;i<stk.size();i++) {
| ~^~~~~~~~~~~
# | 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... |