답안 #1077167

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077167 2024-08-27T02:17:32 Z Evirir Railway Trip (JOI17_railway_trip) C++17
20 / 100
2000 ms 8912 KB
#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
using ll = long long;
using ii = pair<ll, ll>;

static constexpr int INF = 1e9;

int n,K,Q;
vector<int> a, lnxt, rnxt;
vector<vector<int>> adj;

vector<int> getLeft()
{
    vector<int> res(n, -1);
    vector<int> s;

    forn(i,0,n)
    {
        while (!s.empty() && a[s.back()] < a[i]) s.pop_back();
        if (!s.empty())
        {
            res[i] = s.back();
            if (a[s.back()] == a[i]) s.pop_back();
        }
        s.pb(i);
    }
    return res;
}

int bfs(int s, int t)
{
    vector<int> dist(n, INF);
    queue<int> q;
    dist[s] = 0;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front(); q.pop();
        for (const auto v : adj[u])
        {
            if (dist[v] < INF) continue;
            dist[v] = dist[u] + 1;
            q.push(v);
        }
    }
    assert(dist[t] != INF);
    return dist[t];
}

void connect(int u, int v)
{
    adj[u].pb(v);
    adj[v].pb(u);
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);

    cin>>n>>K>>Q;
    a.resize(n); adj.resize(n);
    forn(i,0,n)
    {
        cin>>a[i];
        a[i]--;
    }

    lnxt = getLeft();
    reverse(all(a));
    rnxt = getLeft();
    reverse(all(rnxt));
    reverse(all(a));
    forn(i,0,n)
    {
        if (rnxt[i] != -1) rnxt[i] = n - 1 - rnxt[i];
    }
    forn(i,0,n)
    {
        if (lnxt[i] != -1) connect(i, lnxt[i]);
        if (rnxt[i] != -1) connect(i, rnxt[i]);
    }

    forn(z,0,Q)
    {
        int u,v; cin>>u>>v; u--; v--;
        cout << bfs(u, v) - 1 << '\n';
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 119 ms 8284 KB Output is correct
3 Correct 120 ms 8400 KB Output is correct
4 Correct 128 ms 8300 KB Output is correct
5 Correct 115 ms 8540 KB Output is correct
6 Correct 112 ms 8540 KB Output is correct
7 Correct 114 ms 8784 KB Output is correct
8 Correct 46 ms 7636 KB Output is correct
9 Correct 44 ms 8528 KB Output is correct
10 Correct 42 ms 8144 KB Output is correct
11 Correct 99 ms 8536 KB Output is correct
12 Correct 98 ms 8540 KB Output is correct
13 Correct 105 ms 8528 KB Output is correct
14 Correct 99 ms 8644 KB Output is correct
15 Correct 99 ms 8536 KB Output is correct
16 Correct 101 ms 8540 KB Output is correct
17 Correct 116 ms 8796 KB Output is correct
18 Correct 114 ms 8792 KB Output is correct
19 Correct 62 ms 8776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2056 ms 8404 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2024 ms 8912 KB Time limit exceeded
2 Halted 0 ms 0 KB -