제출 #1176068

#제출 시각아이디문제언어결과실행 시간메모리
1176068VMaksimoski008Passport (JOI23_passport)C++17
48 / 100
1297 ms1114112 KiB
#include <bits/stdc++.h>
#define ar array
using namespace std;

signed main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);

    int n; cin >> n;
    vector<int> L(n+1), R(n+1);
    for(int i=1; i<=n; i++) cin >> L[i] >> R[i];

    vector<ar<int, 3> > G[n+1][n+1];
    int dp[n+1][n+1];

    for(int i=1; i<=n; i++) G[L[i]][R[i]].push_back({ i, i, 1 });
    for(int i=1; i<=n; i++) {
        for(int j=i+1; j<=n; j++) {
            G[i+1][j].push_back({ i, j, 0 });
            G[i][j-1].push_back({ i, j, 0 });
        } 
    }

    for(int i=1; i<=n; i++) {
        int mn = 1e9, mx = 0;
        for(int j=i; j<=n; j++) {
            mn = min(mn, L[j]);
            mx = max(mx, R[j]);
            G[mn][j].push_back({ i, j, 1 });
            G[i][mx].push_back({ i, j, 1 });
        }
    }

    priority_queue<ar<int, 3>, vector<ar<int, 3> >, greater<> > pq;
    for(int i=1; i<=n; i++)
        for(int j=i; j<=n; j++) dp[i][j] = 1e9;
    pq.push({ dp[1][n] = 0, 1, n });

    while(!pq.empty()) {
        auto [d, l, r] = pq.top(); pq.pop();
        if(dp[l][r] != d) continue;

        for(auto [l2, r2, w] : G[l][r])
            if(dp[l2][r2] > d + w) pq.push({ dp[l2][r2] = d + w, l2, r2 }); 
    }

    int q; cin >> q;
    while(q--) {
        int x; cin >> x;
        cout << (dp[x][x] == 1e9 ? -1 : dp[x][x]) << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...