#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;
void update (int i, int j, int val){
if (val < dp[i][j]){
dp[i][j] = val;
h.push(make_pair(val,make_pair(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])
if (v[capat+1] < v[segm])
update (capat+1,segm,dp[capat][segm] + v[capat+1] - v[capat]);
else update (capat+1,segm-1,dp[capat][segm] + v[capat+1] - v[capat]);
else update (segm,capat,dp[capat][segm] + v[segm] - v[capat]);
} else { /// B trebuie sa se intoarca inapoi
if (v[capat+1] <= v[segm+1] ){
update (capat+1,segm,dp[capat][segm] + v[capat+1] - v[capat]);
} else update (segm+1,capat,dp[capat][segm] + v[segm+1] - v[capat] );
}
} else { /// A coboara
if (v[segm] > v[segm+1]){ /// trb sa coboare si B, dar se duce inapoi
if (v[capat] - v[capat+1] <= v[capat] - v[segm+1])
update (capat+1,segm,dp[capat][segm] + v[capat] - v[capat+1]);
else update (segm+1,capat,dp[capat][segm] + v[capat] - v[segm+1]);
} else { /// coboara B, merge inainte
if ( v[capat+1] >= v[segm]) {
if (v[capat+1] > v[segm])
update (capat+1,segm,dp[capat][segm] + v[capat] - v[capat+1]);
else update (capat+1,segm-1,dp[capat][segm] + v[capat] - v[capat+1]);
} else update (segm,capat, dp[capat][segm] + v[capat] - v[capat+1]);
}
}
} 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])
if (v[segm+1] > v[capat-1])
update (capat-1,segm,dp[capat][segm] + v[capat-1] - v[capat]);
else update (capat-1,segm+1,dp[capat][segm] + v[capat-1] - v[capat]);
else update (segm+1,capat-1,dp[capat][segm] + v[segm+1] - v[capat]);
} else { /// A urca, dar se intoarce inapoi
if (v[segm] >= v[capat-1])
update (capat-1,segm,dp[capat][segm] + v[capat-1] - v[capat]);
else update (segm,capat-1,dp[capat][segm] + v[segm] - v[capat]);
}
} else { /// B coboara
if (v[segm] < v[segm+1]){ /// A cobora, dar se intoarce inapoi
if (v[segm] <= v[capat-1])
update (capat-1,segm,dp[capat][segm] + v[capat] - v[capat-1]);
else
update (segm,capat-1,dp[capat][segm] + v[capat] - v[segm]);
} else { /// A coboara, merge inainte
if (v[segm+1] <= v[capat-1]){
if (v[segm+1] < v[capat-1])
update (capat-1,segm,dp[capat][segm] + v[capat] - v[capat-1]);
else update (capat-1,segm+1,dp[capat][segm] + v[capat] - v[capat-1]);
} else update (segm+1,capat-1,dp[capat][segm] + v[capat] - v[segm+1]);
}
}
}
}
cout<<"NO";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
1740 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
8188 KB |
Output isn't correct |
4 |
Incorrect |
24 ms |
47416 KB |
Output isn't correct |
5 |
Incorrect |
51 ms |
98236 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
2380 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
1740 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
7628 KB |
Output isn't correct |
4 |
Incorrect |
23 ms |
43880 KB |
Output isn't correct |
5 |
Incorrect |
22 ms |
43552 KB |
Output isn't correct |
6 |
Incorrect |
42 ms |
76740 KB |
Output isn't correct |
7 |
Incorrect |
40 ms |
72832 KB |
Output isn't correct |
8 |
Incorrect |
53 ms |
93192 KB |
Output isn't correct |
9 |
Incorrect |
55 ms |
94112 KB |
Output isn't correct |
10 |
Incorrect |
46 ms |
92948 KB |
Output isn't correct |