This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int oo = INT_MAX;
const int nmax = 5e3;
int n;
int v[nmax + 5];
int d[nmax + 5][nmax + 5];
bool sel[nmax + 5][nmax + 5];
typedef pair<int,pair<int,int>> pii;
priority_queue<pii,vector<pii>,greater<pii>> h;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>v[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
d[i][j] = oo;
}
}
d[1][n] = 0;
h.push({0,{1,n}});
while(!h.empty())
{
while(!h.empty() && sel[h.top().second.first][h.top().second.second])
{
h.pop();
}
if(h.empty())
{
break;
}
int st = h.top().second.first;
int dr = h.top().second.second;
h.pop();
sel[st][dr] = true;
if(st==dr)
{
cout<<d[st][dr]<<'\n';
return 0;
}
for(int pst=-1;pst<=1;pst++)
{
for(int pdr=-1;pdr<=1;pdr++)
{
int new_st = st + pst;
int new_dr = dr + pdr;
if(new_st > n || new_st <= 0 || new_dr > n || new_dr <= 0)
{
continue;
}
if(v[new_st] != v[new_dr])
{
continue;
}
if(d[new_st][new_dr] > d[st][dr] + abs(v[st] - v[new_st]))
{
d[new_st][new_dr] = d[st][dr] + abs(v[st] - v[new_st]);
h.push({d[new_st][new_dr],{new_st,new_dr}});
}
}
}
}
cout<<"NO"<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |