#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
using namespace std;
const long long INF = 1e18;
vector<vector<pair<int,int>>> con;
vector<long long> dist;
int nrm[5005][5005],cate;
int n,p[5005];
void add_edge(int v1, int m1, int v2, int m2)
{
con[nrm[v1][m1]].push_back({nrm[v2][m2],abs(p[v1]-p[v2])});
con[nrm[v2][m2]].push_back({nrm[v1][m1],abs(p[v1]-p[v2])});
}
priority_queue<pair<long long,int>> pq;
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>p[i];
assert(p[1]==0);
assert(p[n]==0);
for(int v=1;v<=n;v++)
{
for(int m=1;m<n;m++)
{
if(p[v] < min(p[m],p[m+1]) || p[v] > max(p[m],p[m+1]))
continue;
nrm[v][m] = ++cate;
}
}
con.resize(cate+2);
dist.resize(cate+2);
for(int v=1;v<=n;v++)
{
for(int m=1;m<n;m++)
{
if(p[v] < min(p[m],p[m+1]) || p[v] > max(p[m],p[m+1]))
continue;
for(int newv:{v-1,v+1})
{
if(newv>=1 && newv<=n && min(p[m],p[m+1]) <= p[newv] && p[newv] <= max(p[m],p[m+1]))
{
add_edge(v,m,newv,m);
}
}
if(p[v] == p[m] && m-1>0)
{
add_edge(v,m,v,m-1);
}
if(p[v] == p[m+1] && m+1<n)
{
add_edge(v,m,v,m+1);
}
if(v+1<n && min(p[v],p[v+1]) <= p[m] && p[m] <= max(p[v],p[v+1]))
{
add_edge(v,m,m,v);
}
if(v-1>0 && min(p[v],p[v-1]) <= p[m] && p[m] <= max(p[v],p[v-1]))
{
add_edge(v,m,m,v-1);
}
/*if(v+1<n && min(p[v],p[v+1]) <= p[m+1] && p[m+1] <= max(p[v],p[v+1]))
{
add_edge(v,m,m+1,v);
}
if(v-1>0 && min(p[v],p[v-1]) <= p[m+1] && p[m+1] <= max(p[v],p[v-1]))
{
add_edge(v,m,m+1,v-1);
}*/
}
}
for(int i=1;i<=cate;i++)
dist[i]=INF;
dist[nrm[n][1]]=0;
//dist[nrm[1][n-1]]=0;
pq.push({0,nrm[n][1]});
//pq.push({0,nrm[1][n-1]});
while(!pq.empty())
{
long long cdist = -pq.top().first;
int nod = pq.top().second;
pq.pop();
if(dist[nod]!=cdist)
continue;
//cerr<<nod<<" new dist "<<dist[nod]<<"\n";
for(auto [adj,c]:con[nod])
{
if(cdist + c < dist[adj])
{
dist[adj] = cdist + c;
pq.push({-dist[adj],adj});
}
}
}
long long rez=INF;
for(int m=1;m<n;m++)
{
rez = min(rez, dist[nrm[m][m]]);
rez = min(rez, dist[nrm[m+1][m]]);
}
assert(rez<INF);
cout<<rez;
return 0;
}
/*
7
0 10 1 20 5 10 0
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
2136 KB |
Execution killed with signal 6 |
2 |
Runtime error |
12 ms |
9308 KB |
Execution killed with signal 6 |
3 |
Runtime error |
105 ms |
80672 KB |
Execution killed with signal 6 |
4 |
Execution timed out |
3130 ms |
524288 KB |
Time limit exceeded |
5 |
Runtime error |
465 ms |
524288 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
860 KB |
Execution killed with signal 6 |
2 |
Runtime error |
3 ms |
1192 KB |
Execution killed with signal 6 |
3 |
Runtime error |
5 ms |
3980 KB |
Execution killed with signal 6 |
4 |
Runtime error |
4 ms |
2396 KB |
Execution killed with signal 6 |
5 |
Runtime error |
17 ms |
14572 KB |
Execution killed with signal 6 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
3676 KB |
Execution killed with signal 6 |
2 |
Runtime error |
23 ms |
19464 KB |
Execution killed with signal 6 |
3 |
Runtime error |
481 ms |
314228 KB |
Execution killed with signal 6 |
4 |
Execution timed out |
957 ms |
524288 KB |
Time limit exceeded |
5 |
Runtime error |
777 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Runtime error |
388 ms |
524288 KB |
Execution killed with signal 9 |
7 |
Runtime error |
217 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Runtime error |
212 ms |
524288 KB |
Execution killed with signal 9 |
9 |
Runtime error |
207 ms |
524288 KB |
Execution killed with signal 9 |
10 |
Runtime error |
218 ms |
524288 KB |
Execution killed with signal 9 |