이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 1e5 + 5;
int n, q, k;
int a[Nmax], dist[Nmax], st[Nmax];
vector<int> v[Nmax];
int dijkstra(int x, int y)
{
queue<int> q;
int i;
for(i=1; i<=n; ++i) dist[i] = -1;
dist[x] = 0;
q.push(x);
while(q.size())
{
int node = q.front();
q.pop();
for(auto it : v[node])
if(dist[it] == -1)
{
dist[it] = dist[node] + 1;
q.push(it);
}
}
return dist[y];
}
void add_edge(int x, int y)
{
v[x].push_back(y);
v[y].push_back(x);
}
void brute()
{
int i, nr = 0;
for(i=1; i<=n; ++i)
{
while(nr && a[st[nr]] < a[i]) --nr;
if(nr) add_edge(i, st[nr]);
st[++nr] = i;
}
nr = 0;
for(i=n; i; --i)
{
while(nr && a[st[nr]] < a[i]) --nr;
if(nr) add_edge(i, st[nr]);
st[++nr] = i;
}
while(q--)
{
int x, y;
cin >> x >> y;
cout << dijkstra(x, y) - 1 << '\n';
}
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false);
int i;
cin >> n >> k >> q;
for(i=1; i<=n; ++i) cin >> a[i];
if(q <= 50) brute();
// else if(k<=20) solve0();
return 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... |