Submission #227064

# Submission time Handle Problem Language Result Execution time Memory
227064 2020-04-26T00:25:44 Z qkxwsm Climbers (RMI18_climbers) C++14
0 / 100
190 ms 196600 KB
#include <bits/stdc++.h>

using namespace std;

template<class T, class U>
void ckmin(T &a, U b)
{
    if (a > b) a = b;
}

template<class T, class U>
void ckmax(T &a, U b)
{
    if (a < b) a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef pair<ll, pii> plp;

const int MAXN = 5013;
const ll LLINF = 2e18;

int N;
vl arr;
ll dist[MAXN][MAXN];
priority_queue<plp, vector<plp>, greater<plp> > pq;
ll ans = LLINF;

bool between(int a, int b, int c)
{
    return ((b <= a && a <= c) || (c <= a && a <= b));
}

int32_t main()
{
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(4);
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> N;
    FOR(i, 0, N)
    {
        ll x; cin >> x;
        if (SZ(arr) >= 2)
        {
            if (arr[SZ(arr) - 2] <= arr.back() && arr.back() <= x)
            {
                arr.pop_back();
            }
            else if (arr[SZ(arr) - 2] >= arr.back() && arr.back() >= x)
            {
                arr.pop_back();
            }
        }
        arr.PB(x);
    }
    N = SZ(arr);
    FOR(i, 0, N)
    {
        fill(dist[i], dist[i] + N, LLINF);
    }
    dist[0][N - 2] = 0;
    pq.push({0, {0, N - 2}});
    while(!pq.empty())
    {
        ll d = pq.top().fi;
        pii u = pq.top().se;
        pq.pop();
        //one of you is at u.fi, the other is at arr[u.fi] between u.se and u.se + 1
        ll nd; pii v;
        if (u.se + 1 != N - 1 && arr[u.se + 1] == arr[u.fi])
        {
            nd = d; v = {u.fi, u.se + 1};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        if (u.se != 0 && arr[u.se] == arr[u.fi])
        {
            nd = d; v = {u.fi, u.se - 1};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        //right guy moves to .se + 1
        if (u.fi != N - 1 && between(arr[u.se + 1], arr[u.fi], arr[u.fi + 1]))
        {
            nd = d + abs(arr[u.se + 1] - arr[u.fi]); v = {u.se + 1, u.fi};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        if (u.fi != 0 && between(arr[u.se + 1], arr[u.fi], arr[u.fi - 1]))
        {
            nd = d + abs(arr[u.se + 1] - arr[u.fi]); v = {u.se + 1, u.fi - 1};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        //right guy moves to .se
        if (u.fi != N - 1 && between(arr[u.se], arr[u.fi], arr[u.fi + 1]))
        {
            nd = d + abs(arr[u.se] - arr[u.fi]); v = {u.se, u.fi};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        if (u.fi != 0 && between(arr[u.se + 1], arr[u.fi], arr[u.fi - 1]))
        {
            nd = d + abs(arr[u.se] - arr[u.fi]); v = {u.se, u.fi - 1};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        //left guy moves right.
        if (u.fi != N - 1 && between(arr[u.fi + 1], arr[u.se], arr[u.se + 1]))
        {
            nd = d + abs(arr[u.fi + 1] - arr[u.fi]); v = {u.fi + 1, u.se};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
        if (u.fi != 0 && between(arr[u.fi - 1], arr[u.se], arr[u.se + 1]))
        {
            nd = d + abs(arr[u.fi - 1] - arr[u.fi]); v = {u.fi - 1, u.se};
            if (dist[v.fi][v.se] > nd)
            {
                dist[v.fi][v.se] = nd;
                pq.push({nd, v});
            }
        }
    }
    FOR(i, 0, N)
    {
        ckmin(ans, dist[i][i]);
    }
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 768 KB Output isn't correct
2 Incorrect 7 ms 2304 KB Output isn't correct
3 Incorrect 11 ms 12160 KB Output isn't correct
4 Incorrect 39 ms 82808 KB Output isn't correct
5 Incorrect 95 ms 196600 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 512 KB Output isn't correct
2 Incorrect 5 ms 512 KB Output isn't correct
3 Incorrect 5 ms 1152 KB Output isn't correct
4 Incorrect 5 ms 768 KB Output isn't correct
5 Incorrect 6 ms 3072 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 768 KB Output isn't correct
2 Incorrect 6 ms 2048 KB Output isn't correct
3 Incorrect 158 ms 11520 KB Output isn't correct
4 Incorrect 40 ms 76152 KB Output isn't correct
5 Incorrect 38 ms 75512 KB Output isn't correct
6 Incorrect 70 ms 137848 KB Output isn't correct
7 Incorrect 70 ms 130296 KB Output isn't correct
8 Incorrect 93 ms 186488 KB Output isn't correct
9 Incorrect 92 ms 188280 KB Output isn't correct
10 Incorrect 190 ms 186132 KB Output isn't correct