#include <bits/stdc++.h>
using namespace std;
int n;
int vinit[5010];
long long dp[25000000];
priority_queue <pair <long long , 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:39:32: warning: unused variable 'val' [-Wunused-variable]
39 | int i , ers , cost , nod , val , vecin , j , elem;
| ^~~
climbers.cpp:39:38: warning: unused variable 'vecin' [-Wunused-variable]
39 | int i , ers , cost , nod , val , vecin , j , elem;
| ^~~~~
climbers.cpp:39:50: warning: variable 'elem' set but not used [-Wunused-but-set-variable]
39 | int i , ers , cost , nod , val , vecin , j , elem;
| ^~~~
climbers.cpp:41:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
41 | fscanf (fin,"%d",&n);
| ~~~~~~~^~~~~~~~~~~~~
climbers.cpp:43:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
43 | fscanf (fin,"%d",&vinit[i]);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
6 ms |
8044 KB |
Output is correct |
4 |
Incorrect |
41 ms |
70636 KB |
Output isn't correct |
5 |
Incorrect |
103 ms |
195948 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
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 |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Incorrect |
1 ms |
1516 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Correct |
2 ms |
1024 KB |
Output is correct |
3 |
Incorrect |
74 ms |
7532 KB |
Output isn't correct |
4 |
Incorrect |
130 ms |
64504 KB |
Output isn't correct |
5 |
Incorrect |
71 ms |
63980 KB |
Output isn't correct |
6 |
Incorrect |
71 ms |
121964 KB |
Output isn't correct |
7 |
Incorrect |
86 ms |
114844 KB |
Output isn't correct |
8 |
Incorrect |
98 ms |
176108 KB |
Output isn't correct |
9 |
Incorrect |
110 ms |
179564 KB |
Output isn't correct |
10 |
Incorrect |
125 ms |
175468 KB |
Output isn't correct |