답안 #1063355

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1063355 2024-08-17T17:09:57 Z Unforgettablepl Railway Trip (JOI17_railway_trip) C++17
45 / 100
2000 ms 95252 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 1e15;

void main2(int n,int k){
    int q;
    cin >> q;
    vector<int> l(n+1);
    vector<vector<int>> idxes(k+1);
    for(int i=1;i<=n;i++)cin>>l[i];
    vector<vector<int>> adj(n+1);
    stack<pair<int,int>> s;
    for(int i=1;i<=n;i++) {
        while(!s.empty() and s.top().first<l[i])s.pop();
        if(!s.empty()) {
            adj[s.top().second].emplace_back(i);
            adj[i].emplace_back(s.top().second);
        }
        s.emplace(l[i],i);
    }
    while(!s.empty())s.pop();
    for(int i=n;i;i--){
        while(!s.empty() and s.top().first<l[i])s.pop();
        if(!s.empty()) {
            adj[s.top().second].emplace_back(i);
            adj[i].emplace_back(s.top().second);
        }
        s.emplace(l[i],i);
    }
    for(int i=1;i<=q;i++) {
        int a,b;
        cin >> a >> b;
        vector<bool> visited(n+1);
        queue<pair<int,int>> qu;
        qu.emplace(-1,a);
        while(!qu.empty()) {
            auto [dist,idx] = qu.front();qu.pop();
            if(visited[idx])continue;
            visited[idx]=true;
            if(idx==b) {
                cout << dist << '\n';
                break;
            }
            for(int&x:adj[idx])if(!visited[x])qu.emplace(dist+1,x);
        }
    }
    exit(0);
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,k;
    cin >> n >> k;
    if(k>20)main2(n,k);
    int q;
    cin >> q;
    vector<int> l(n+2);
    for(int i=1;i<=n;i++)cin>>l[i];
    l[0]=l[n+1]=k+1;
    vector pref(k+1,vector(n+1,0));
    for(int i=1;i<=k;i++) {
        for(int j=1;j<=n;j++) {
            pref[i][j]=pref[i][j-1];
            if(l[j]>=i)pref[i][j]++;
        }
    }
    vector nearest_left(k+1,vector<pair<int,int>>(n+1,{INF,0}));
    vector nearest_right(k+1,vector<pair<int,int>>(n+1,{INF,0}));
    vector<vector<int>> adj(n+1);
    stack<pair<int,int>> s;
    for(int i=1;i<=n;i++) {
        while(!s.empty() and s.top().first<l[i])s.pop();
        if(!s.empty()) {
            adj[s.top().second].emplace_back(i);
            adj[i].emplace_back(s.top().second);
        }
        s.emplace(l[i],i);
    }
    while(!s.empty())s.pop();
    for(int i=n;i;i--){
        while(!s.empty() and s.top().first<l[i])s.pop();
        if(!s.empty()) {
            adj[s.top().second].emplace_back(i);
            adj[i].emplace_back(s.top().second);
        }
        s.emplace(l[i],i);
    }
    for(int i=1;i<=k;i++){
        {
            // nearest right calculation
            vector<bool> visited(n+1);
            priority_queue<tuple<int,int,int>> qu;
            for(int x=1;x<=n;x++)if(l[x]>=i)qu.emplace(0,x,x);
            while(!qu.empty()) {
                auto [dist,source,idx] = qu.top();qu.pop();dist=-dist;
                if(visited[idx])continue;
                visited[idx]=true;
                nearest_right[i][idx]={dist,source};
                for(int&x:adj[idx])if(!visited[x])qu.emplace(-dist-1,source,x);
            }
        }
        {
            // nearest left calculation
            vector<bool> visited(n+1);
            priority_queue<tuple<int,int,int>> qu;
            for(int x=1;x<=n;x++)if(l[x]>=i)qu.emplace(0,-x,x);
            while(!qu.empty()) {
                auto [dist,source,idx] = qu.top();qu.pop();dist=-dist;source=-source;
                if(visited[idx])continue;
                visited[idx]=true;
                nearest_left[i][idx]={dist,source};
                for(int&x:adj[idx])if(!visited[x])qu.emplace(-dist-1,-source,x);
            }
        }
    }
    for(int i=1;i<=q;i++) {
        int oa,ob;
        cin >> oa >> ob;
        if(ob<oa)swap(oa,ob);
        int ans = INF;
        for(int level=1;level<=k;level++) {
            if(nearest_left[level][ob].second<nearest_right[level][oa].second)break;
            int a = nearest_right[level][oa].second;
            int b = nearest_left[level][ob].second;
            int currdist = nearest_right[level][oa].first+nearest_left[level][ob].first;
            ans = min(ans,currdist+pref[level][b]-pref[level][a]-1);
        }
        cout << ans << '\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 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 1 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 2 ms 344 KB Output is correct
2 Correct 708 ms 59736 KB Output is correct
3 Correct 1335 ms 95248 KB Output is correct
4 Correct 100 ms 10076 KB Output is correct
5 Correct 96 ms 9996 KB Output is correct
6 Correct 106 ms 10340 KB Output is correct
7 Correct 107 ms 13436 KB Output is correct
8 Correct 43 ms 12376 KB Output is correct
9 Correct 60 ms 12848 KB Output is correct
10 Correct 45 ms 10332 KB Output is correct
11 Correct 91 ms 12380 KB Output is correct
12 Correct 90 ms 10844 KB Output is correct
13 Correct 95 ms 10332 KB Output is correct
14 Correct 92 ms 12440 KB Output is correct
15 Correct 92 ms 10840 KB Output is correct
16 Correct 85 ms 10332 KB Output is correct
17 Correct 116 ms 13516 KB Output is correct
18 Correct 107 ms 13484 KB Output is correct
19 Correct 72 ms 13308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 992 ms 74016 KB Output is correct
2 Correct 703 ms 59876 KB Output is correct
3 Correct 1044 ms 77504 KB Output is correct
4 Correct 1089 ms 80968 KB Output is correct
5 Correct 1160 ms 84720 KB Output is correct
6 Correct 1296 ms 92216 KB Output is correct
7 Correct 1330 ms 95252 KB Output is correct
8 Correct 175 ms 32208 KB Output is correct
9 Correct 236 ms 77532 KB Output is correct
10 Correct 291 ms 95156 KB Output is correct
11 Correct 278 ms 95248 KB Output is correct
12 Correct 271 ms 95192 KB Output is correct
13 Correct 274 ms 95180 KB Output is correct
14 Correct 431 ms 93668 KB Output is correct
15 Correct 796 ms 93660 KB Output is correct
16 Correct 1020 ms 93916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2032 ms 13688 KB Time limit exceeded
2 Halted 0 ms 0 KB -