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>
#define DIM 5010
#define INF 2000000000
using namespace std;
priority_queue <pair<int,pair<int,int> >, vector<pair<int,pair<int,int> > >, greater<pair<int,pair<int,int> > > > h;
int dp[DIM][DIM],v[DIM];
int n,i,j;
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<=n;i++)
cin>>v[i];
int k = 1;
for (i=2;i<n;i++){
if ( (v[i] > v[i-1] && v[i] > v[i+1]) || (v[i] < v[i-1] && v[i] < v[i+1]))
v[++k] = v[i];
}
v[++k] = 0;
n = k;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
dp[i][j] = INF;
/// dp[capat][segment] - A/B se afla intr un capat de segm si celalalt e pe un segm
dp[1][n-1] = 0;
h.push(make_pair(0,make_pair(1,n-1)));
while (!h.empty()){
int cost = h.top().first;
int capat = h.top().second.first;
int segm = h.top().second.second;
h.pop();
if (capat == segm || capat == segm+1){ /// au ajuns in acelasi punct
cout<<cost;
return 0;
}
if (capat < segm){ /// A e in capat
if (v[capat] < v[capat+1]){ /// trebuie sa urc
if (v[segm] > v[segm+1]){ /// poate si B sa urce
if (v[capat+1] <= v[segm]){
int val = v[capat+1] - v[capat];
if (dp[capat][segm] + val < dp[capat+1][segm]){
dp[capat+1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat+1][segm],make_pair(capat+1,segm)));
}
} else {
int val = v[segm] - v[capat];
if (dp[capat][segm] + val < dp[segm][capat]){
dp[segm][capat] = dp[capat][segm] + val;
h.push(make_pair(dp[segm][capat],make_pair(segm,capat)));
}
}
} else { /// B trebuie sa se intoarca inapoi
if (v[capat+1] <= v[segm+1]){
int val = v[capat+1] - v[capat];
if (dp[capat][segm] + val < dp[capat+1][segm]){
dp[capat+1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat+1][segm],make_pair(capat+1,segm)));
}
} else {
int val = v[segm+1] - v[capat];
if (dp[capat][segm] + val < dp[segm+1][capat]){
dp[segm+1][capat] = dp[capat][segm] + val;
h.push(make_pair(dp[segm+1][capat],make_pair(segm+1,capat)));
}
}
}
} else { /// A coboara
if (v[segm] > v[segm+1]){ /// trb sa coboare si B, dar se duce inapoi
if (v[capat+1] >= v[segm+1]){
int val = v[capat] - v[capat+1];
if (dp[capat][segm] + val < dp[capat+1][segm]){
dp[capat+1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat+1][segm],make_pair(capat+1,segm)));
}
} else {
int val = v[capat] - v[segm+1];
if (dp[capat][segm] + val < dp[segm+1][capat]){
dp[segm+1][capat] = dp[capat][segm] + val;
h.push(make_pair(dp[segm+1][capat],make_pair(segm+1,capat)));
}
}
} else { /// coboara B, merge inainte
if (v[capat+1] >= v[segm]){
int val = v[capat] - v[capat+1];
if (dp[capat][segm] + val < dp[capat+1][segm]){
dp[capat+1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat+1][segm],make_pair(capat+1,segm)));
}
} else {
int val = v[capat] - v[segm];
if (dp[capat][segm] + val < dp[segm][capat]){
dp[segm][capat] = dp[capat][segm] + val;
h.push(make_pair(dp[segm][capat],make_pair(segm,capat)));
}
}
}
}
} else { /// B e in capat
if (v[capat] < v[capat-1]){ /// B urca
if (v[segm+1] > v[segm]){ /// poate si A sa urce
if (v[segm+1] >= v[capat-1]){
int val = v[capat-1] - v[capat];
if (dp[capat][segm] + val < dp[capat-1][segm]){
dp[capat-1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat-1][segm],make_pair(capat-1,segm)));
}
} else {
int val = v[segm+1] - v[capat];
if (dp[capat][segm] + val < dp[segm+1][capat-1]){
dp[segm+1][capat-1] = dp[capat][segm] + val;
h.push(make_pair(dp[segm+1][capat-1],make_pair(segm+1,capat-1)));
}
}
} else { /// A urca, dar se intoarce inapoi
if (v[segm-1] >= v[capat-1]){
int val = v[capat-1] - v[capat];
if (dp[capat][segm] + val < dp[capat-1][segm]){
dp[capat-1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat-1][segm],make_pair(capat-1,segm)));
}
} else {
int val = v[segm] - v[capat];
if (dp[capat][segm] + val < dp[segm][capat-1]){
dp[segm][capat-1] = dp[capat][segm] + val;
h.push(make_pair(dp[segm][capat-1],make_pair(segm,capat-1)));
}
}
}
} else { /// B coboara
if (v[segm] < v[segm+1]){ /// A cobora, dar se intoarce inapoi
if (v[segm] <= v[capat-1]){
int val = v[capat] - v[capat-1];
if (dp[capat][segm] + val < dp[capat-1][segm]){
dp[capat-1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat-1][segm],make_pair(capat-1,segm)));
}
} else {
int val = v[capat] - v[segm];
if (dp[capat][segm] + val < dp[segm][capat-1]){
dp[segm][capat-1] = dp[capat][segm] + val;
h.push(make_pair(dp[segm][capat-1],make_pair(segm,capat-1)));
}
}
} else { /// A coboara, merge inainte
if (v[segm+1] <= v[capat-1]){
int val = v[capat] - v[capat-1];
if (dp[capat][segm] + val < dp[capat-1][segm]){
dp[capat-1][segm] = dp[capat][segm] + val;
h.push(make_pair(dp[capat-1][segm],make_pair(capat-1,segm)));
}
} else {
int val = v[capat] - v[segm+1];
if (dp[capat][segm] + val < dp[segm+1][capat-1]){
dp[segm+1][capat-1] = dp[capat][segm] + val;
h.push(make_pair(dp[segm+1][capat-1],make_pair(segm+1,capat-1)));
}
}
}
}
}
}
cout<<"NO";
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... |