답안 #241034

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
241034 2020-06-22T09:21:02 Z osaaateiasavtnl Railway Trip (JOI17_railway_trip) C++14
5 / 100
29 ms 8320 KB
#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 2007;
int n, k, q;
int a[N];

vector <int> g[N];

void bfs(int S, int dist[N]) {
    queue <int> q;
    for (int i = 0; i < N; ++i)
        dist[i] = N;
    q.push(S);
    dist[S] = 0;
    while (q.size()) {
        int u = q.front(); q.pop();
        for (int v : g[u]) {
            if (dist[u] + 1 < dist[v]) {
                dist[v] = dist[u] + 1;
                q.push(v);
            }   
        }   
    }   
}

int dist[N][N];
bool used[N];

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> k >> q;

    if (n >= N)
        exit(1);

    for (int i = 1; i <= n; ++i)
        cin >> a[i];

    vector <ii> ord;
    for (int i = 1; i <= n; ++i) {
        ord.app(mp(a[i], i));
    }
    sort(all(ord));
    reverse(all(ord));
    for (auto e : ord) {
        int i = e.s;

        used[i] = 1;

        int cur = -1;
        for (int j = i - 1; j; --j) {
            if (used[j])
                break;
            if (a[j] > cur) {
                g[i].app(j);
                g[j].app(i);

                //cout << "edge " << i << ' ' << j << endl;

                cur = a[j];
            }       
        }   

        cur = -1;
        for (int j = i + 1; j <= n; ++j) {
            if (used[j])
                break;
            if (a[j] > cur) {
                g[i].app(j);
                g[j].app(i);

                //cout << "edge " << i << ' ' << j << endl;

                cur = a[j];
            }       
        }   
    }   

    for (int i = 1; i <= n; ++i)
        bfs(i, dist[i]);

    while (q--) {
        int i, j;
        cin >> i >> j;
        cout << dist[i][j] - 1 << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1152 KB Output is correct
2 Correct 5 ms 1152 KB Output is correct
3 Correct 5 ms 1152 KB Output is correct
4 Correct 5 ms 1152 KB Output is correct
5 Correct 5 ms 1152 KB Output is correct
6 Correct 5 ms 1152 KB Output is correct
7 Correct 6 ms 1152 KB Output is correct
8 Correct 6 ms 1200 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 8320 KB Output is correct
2 Runtime error 5 ms 384 KB Execution failed because the return code was nonzero
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 384 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 384 KB Execution failed because the return code was nonzero
2 Halted 0 ms 0 KB -