Submission #70043

# Submission time Handle Problem Language Result Execution time Memory
70043 2018-08-22T09:41:46 Z antimirage Railway Trip (JOI17_railway_trip) C++17
20 / 100
2000 ms 17204 KB
#include <algorithm>
#include <iostream>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <iomanip>
#include <utility>
#include <math.h>
#include <time.h>
#include <vector>
#include <set>
#include <map>
#include <queue>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back
#define sz(s) (int)s.size()
#define all(s) s.begin(), s.end()

using namespace std;

const int N = 1e5 + 5;

int n, k, q, ar[N], u[N], a, b, used[N], v;

vector <int> g[N];

queue <int> qu;

main()
{
    cin >> n >> k >> q;

    for (int i = 1; i <= n; i++)
        scanf("%d", &ar[i]);

    for (int i = 1; i <= n; i++)
    {
        for (int j = i - 1; j >= 1; j--)
        {
            if (ar[j] >= ar[i])
            {
                g[i].pb(j);
                break;
            }
        }
        for (int j = i + 1; j <= n; j++)
        {
            if (ar[j] >= ar[i])
            {
                g[i].pb(j);
                break;
            }
        }
    }
    while (q--)
    {
        memset(u, -1, sizeof(u));
        memset(used, -1, sizeof(used));
        scanf("%d%d", &a, &b);

        v = a;

        qu.push(v);
        u[v] = 0;

        while (!qu.empty())
        {
            v = qu.front();
            qu.pop();
            for(auto to : g[v])
            {
                if ( u[to] == -1 )
                {
                    qu.push(to);
                    u[to] = u[v] + 1;
                }
            }
        }
        int ans = (u[b] == -1 ? 1e9 + 7 : u[b] - 1);

        v = b;

        qu.push(v);
        used[v] = 0;

        while (!qu.empty())
        {
            v = qu.front();
            qu.pop();
            for(auto to : g[v])
            {
                if ( used[to] == -1 )
                {
                    qu.push(to);
                    used[to] = used[v] + 1;
                }
            }
        }
        ans = (used[a] == -1 ? ans : used[a] - 1);

        for (int i = 1; i <= n; i++)
        {
            if (used[i] != -1 && u[i] != -1)
                ans = min(ans, used[i] + u[i] - 1);
        }
        printf("%d\n", ans);
    }
}

Compilation message

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &ar[i]);
         ~~~~~^~~~~~~~~~~~~~
railway_trip.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3448 KB Output is correct
2 Correct 9 ms 3448 KB Output is correct
3 Correct 8 ms 3592 KB Output is correct
4 Correct 9 ms 3592 KB Output is correct
5 Correct 10 ms 3592 KB Output is correct
6 Correct 8 ms 3640 KB Output is correct
7 Correct 7 ms 3640 KB Output is correct
8 Correct 8 ms 3640 KB Output is correct
9 Correct 8 ms 3640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3660 KB Output is correct
2 Correct 115 ms 7484 KB Output is correct
3 Correct 68 ms 7748 KB Output is correct
4 Correct 62 ms 8008 KB Output is correct
5 Correct 45 ms 8288 KB Output is correct
6 Correct 42 ms 8744 KB Output is correct
7 Correct 64 ms 9264 KB Output is correct
8 Correct 145 ms 9552 KB Output is correct
9 Correct 1560 ms 10216 KB Output is correct
10 Correct 1374 ms 10696 KB Output is correct
11 Correct 769 ms 11388 KB Output is correct
12 Correct 760 ms 11680 KB Output is correct
13 Correct 832 ms 12308 KB Output is correct
14 Correct 779 ms 12932 KB Output is correct
15 Correct 755 ms 13580 KB Output is correct
16 Correct 754 ms 14016 KB Output is correct
17 Correct 49 ms 14588 KB Output is correct
18 Correct 39 ms 15232 KB Output is correct
19 Correct 52 ms 15896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2071 ms 16428 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2043 ms 17204 KB Time limit exceeded
2 Halted 0 ms 0 KB -