#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);
}
while (SZ(arr) >= 2 && arr[SZ(arr) - 2] >= arr.back())
{
arr.pop_back();
}
arr.PB(0);
N = SZ(arr);
// for (ll x : arr)
// {
// cerr << x << ' ';
// }
// cerr << endl;
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 != N - 2 && 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 - 1], arr[u.fi]))
{
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], arr[u.fi - 1], arr[u.fi]))
{
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, 1, N)
{
ckmin(ans, dist[i - 1][i - 1]);
ckmin(ans, dist[i][i - 1]);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
8 ms |
2304 KB |
Output is correct |
3 |
Correct |
14 ms |
12160 KB |
Output is correct |
4 |
Correct |
69 ms |
82808 KB |
Output is correct |
5 |
Correct |
176 ms |
196600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
4 |
Correct |
5 ms |
768 KB |
Output is correct |
5 |
Correct |
7 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
768 KB |
Output is correct |
2 |
Correct |
8 ms |
2176 KB |
Output is correct |
3 |
Correct |
146 ms |
11392 KB |
Output is correct |
4 |
Correct |
371 ms |
76152 KB |
Output is correct |
5 |
Correct |
519 ms |
75640 KB |
Output is correct |
6 |
Correct |
430 ms |
137848 KB |
Output is correct |
7 |
Correct |
575 ms |
130296 KB |
Output is correct |
8 |
Correct |
435 ms |
186452 KB |
Output is correct |
9 |
Correct |
664 ms |
188280 KB |
Output is correct |
10 |
Correct |
700 ms |
186060 KB |
Output is correct |