#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
22876 KB |
Output isn't correct |
4 |
Incorrect |
10 ms |
62044 KB |
Output isn't correct |
5 |
Incorrect |
20 ms |
100236 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
4488 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
8792 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
22876 KB |
Output isn't correct |
4 |
Incorrect |
10 ms |
62044 KB |
Output isn't correct |
5 |
Incorrect |
10 ms |
61956 KB |
Output isn't correct |
6 |
Incorrect |
14 ms |
82544 KB |
Output isn't correct |
7 |
Incorrect |
14 ms |
82520 KB |
Output isn't correct |
8 |
Incorrect |
22 ms |
100188 KB |
Output isn't correct |
9 |
Incorrect |
19 ms |
100104 KB |
Output isn't correct |
10 |
Incorrect |
18 ms |
100188 KB |
Output isn't correct |