#include <bits/stdc++.h>
using namespace std;
int n;
int dp[25000000] , vinit[5010];
priority_queue <pair <int , int> > h;
int encode (int x , int y){
x--;
y--;
return x * n + y;
}
pair <int , int> decode (int cod){
return make_pair(cod / n + 1 , cod % n + 1);
}
void muchie (int x1 , int y1 , int x2 , int y2 , int cost){
int nod , vecin;
nod = encode(x1 , y1);
vecin = encode(x2 , y2);
if (dp[vecin] > dp[nod] + cost){
dp[vecin] = dp[nod] + cost;
h.push(make_pair(-dp[vecin] , vecin));
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int i , ers , cost , nod , val , vecin , j , elem;
pair <int , int> actual;
fscanf (fin,"%d",&n);
for (i = 1 ; i <= n ; i++)
fscanf (fin,"%d",&vinit[i]);
ers = 0;
for (i = 2 ; i < n ; i++){
if ((vinit[i - 1] <= vinit[i] && vinit[i] <= vinit[i + 1]) || (vinit[i - 1] >= vinit[i] && vinit[i] >= vinit[i + 1]))
ers++; /// il stergi pe vinit[i]
else vinit[i - ers] = vinit[i];
}
vinit[n - ers] = vinit[n];
n -= ers;
elem = 0;
/// acum vinit ul e "condensat"
for (i = 0 ; i < n * n ; i++)
dp[i] = 2000000000;
dp[encode(1 , n - 1)] = 0;
h.push(make_pair(0 , encode(1 , n - 1)));
while (!h.empty()){
cost = -h.top().first;
nod = h.top().second;
h.pop();
actual = decode(nod);
if (dp[nod] != cost)
continue;
i = actual.first;
j = actual.second;
if (i == j || i == j + 1){
fprintf (fout,"%d",cost);
return 0;
}
/// ------------------------------------------------------------------------
if (vinit[j] < vinit[j + 1]){ /// segm e crescator
if (i != n){ /// ma duc in nodul i + 1, deci nu parasesc segm
if (vinit[i + 1] > vinit[i] && vinit[i + 1] <= vinit[j + 1]){
muchie (i , j , i + 1 , j , vinit[i + 1] - vinit[i]); /// se pastreaza segmentul
}
else if (vinit[i + 1] < vinit[i] && vinit[i + 1] >= vinit[j]){
muchie (i , j , i + 1 , j , vinit[i] - vinit[i + 1]);
}
}
if (i != 1){ /// ma duc in nodul i - 1, raman pe acelasi segm
if (vinit[i - 1] > vinit[i] && vinit[i - 1] <= vinit[j + 1]){
muchie (i , j , i - 1 , j , vinit[i - 1] - vinit[i]); /// se pastreaza segmentul
}
else if (vinit[i - 1] < vinit[i] && vinit[i - 1] >= vinit[j]){
muchie (i , j , i - 1 , j , vinit[i] - vinit[i - 1]);
}
}
/// --------------------------------------------------
/// sunt intre j si j + 1
/// vreau sa ma mut in j, adc cobor
if (i != n && vinit[i] > vinit[i + 1] && vinit[i] - vinit[i + 1] >= vinit[i] - vinit[j]){
muchie (i , j , j , i , vinit[i] - vinit[j]);
}
if (i != 1 && vinit[i] > vinit[i - 1] && vinit[i] - vinit[i - 1] >= vinit[i] - vinit[j]){
muchie (i , j , j , i - 1 , vinit[i] - vinit[j]);
}
/// in j + 1 , adc urc
if (i != n && vinit[i] < vinit[i + 1] && vinit[i + 1] - vinit[i] >= vinit[j + 1] - vinit[i]){
muchie (i , j , j + 1 , i , vinit[j + 1] - vinit[i]);
}
if (i != 1 && vinit[i] < vinit[i - 1] && vinit[i - 1] - vinit[i] >= vinit[j + 1] - vinit[i]){
muchie (i , j , j + 1 , i - 1 , vinit[j + 1] - vinit[i]);
}
}
else { /// segm e descrescator
if (i != n){ /// ma duc in nodul i + 1, deci nu parasesc segm
if (vinit[i + 1] > vinit[i] && vinit[i + 1] <= vinit[j]){
muchie (i , j , i + 1 , j , vinit[i + 1] - vinit[i]); /// se pastreaza segmentul
}
else if (vinit[i + 1] < vinit[i] && vinit[i + 1] >= vinit[j + 1]){
muchie (i , j , i + 1 , j , vinit[i] - vinit[i + 1]);
}
}
if (i != 1){ /// ma duc in nodul i - 1, raman pe acelasi segm
if (vinit[i - 1] > vinit[i] && vinit[i - 1] <= vinit[j]){
muchie (i , j , i - 1 , j , vinit[i - 1] - vinit[i]); /// se pastreaza segmentul
}
else if (vinit[i - 1] < vinit[i] && vinit[i - 1] >= vinit[j + 1]){
muchie (i , j , i - 1 , j , vinit[i] - vinit[i - 1]);
}
}
/// --------------------------------------------------
/// sunt intre j si j + 1
/// vinitreau sa ma mut in j, adc URC
if (i != n && vinit[i] < vinit[i + 1] && vinit[i + 1] - vinit[i] >= vinit[j] - vinit[i]){
muchie (i , j , j , i , vinit[j] - vinit[i]);
}
if (i != 1 && vinit[i] < vinit[i - 1] && vinit[i - 1] - vinit[i] >= vinit[j] - vinit[i]){
muchie (i , j , j , i - 1 , vinit[j] - vinit[i]);
}
/// vinitreau sa ma duc in j + 1 adica cobor
if (i != n && vinit[i] > vinit[i + 1] && vinit[i] - vinit[i + 1] >= vinit[i] - vinit[j + 1]){
muchie (i , j , j + 1 , i , vinit[i] - vinit[j + 1]);
}
if (i != 1 && vinit[i] > vinit[i - 1] && vinit[i] - vinit[i - 1] >= vinit[i] - vinit[j + 1]){
muchie (i , j , j + 1 , i - 1 ,vinit[i] - vinit[j + 1]);
}
}
}
fprintf (fout,"NO");
return 0;
}
Compilation message
climbers.cpp: In function 'int main()':
climbers.cpp:38:32: warning: unused variable 'val' [-Wunused-variable]
38 | int i , ers , cost , nod , val , vecin , j , elem;
| ^~~
climbers.cpp:38:38: warning: unused variable 'vecin' [-Wunused-variable]
38 | int i , ers , cost , nod , val , vecin , j , elem;
| ^~~~~
climbers.cpp:38:50: warning: variable 'elem' set but not used [-Wunused-but-set-variable]
38 | int i , ers , cost , nod , val , vecin , j , elem;
| ^~~~
climbers.cpp:40:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
40 | fscanf (fin,"%d",&n);
| ~~~~~~~^~~~~~~~~~~~~
climbers.cpp:42:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
42 | fscanf (fin,"%d",&vinit[i]);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
4 ms |
4204 KB |
Output is correct |
4 |
Incorrect |
25 ms |
35436 KB |
Output isn't correct |
5 |
Incorrect |
60 ms |
98156 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Incorrect |
1 ms |
876 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Incorrect |
64 ms |
3948 KB |
Output isn't correct |
4 |
Incorrect |
92 ms |
32492 KB |
Output isn't correct |
5 |
Incorrect |
49 ms |
32236 KB |
Output isn't correct |
6 |
Incorrect |
42 ms |
61184 KB |
Output isn't correct |
7 |
Incorrect |
54 ms |
57580 KB |
Output isn't correct |
8 |
Incorrect |
57 ms |
88300 KB |
Output isn't correct |
9 |
Incorrect |
59 ms |
89964 KB |
Output isn't correct |
10 |
Incorrect |
79 ms |
87916 KB |
Output isn't correct |