# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
667832 |
2022-12-02T06:19:17 Z |
Nursik |
Climbers (RMI18_climbers) |
C++14 |
|
321 ms |
196044 KB |
#include <stdio.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
const ll maxn = 5e3 + 1, maxm = 1e6 + 1;
const ll mod = 1e9 + 7, inf = 1e9, block = 550, hb = 31, base = 1000050017, biginf = 5e18;
const ld eps = 1e-9;
int n;
int a[maxn];
ll dp[maxn][maxn];
set<pair<ll, pair<int, int>>> setik;
vector<int> vec;
bool is(int i, int j){
if (a[j] <= a[j + 1]){
return (a[j] <= a[i] && a[i] <= a[j + 1]);
}
else{
return (a[j] >= a[i] && a[i] >= a[j + 1]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
vec.pb(a[1]);
for (int i = 2; i < n; ++i){
if (a[i] < a[i - 1] && a[i] < a[i + 1] || a[i - 1] < a[i] && a[i] > a[i + 1]){
vec.pb(a[i]);
}
}
for (int i = 1; i <= n; ++i){
a[i] = 0;
}
vec.pb(a[n]);
n = (int)vec.size();
for (int i = 0; i < n; ++i){
a[i + 1] = vec[i];
}
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
dp[i][j] = inf * inf;
}
}
dp[1][n - 1] = 0;
setik.insert(mp(0, mp(1, n - 1)));
while (setik.size() > 0){
pair<ll, pair<int, int>> cur = *setik.begin(); setik.erase(cur);
ll cost = cur.f, x = cur.s.f, y = cur.s.s;
if (x == y || x == y + 1){
cout << cost;
exit(0);
}
if (cost > dp[x][y])
continue;
if (x > 1 && is(x - 1, y) && dp[x - 1][y] > dp[x][y] + abs(a[x] - a[x - 1])){
dp[x - 1][y] = dp[x][y] + abs(a[x] - a[x - 1]);
setik.insert(mp(dp[x - 1][y], mp(x - 1, y)));
}
if (x < n && is(x + 1, y) && dp[x + 1][y] > dp[x][y] + abs(a[x] - a[x + 1])){
dp[x + 1][y] = dp[x][y] + abs(a[x] - a[x + 1]);
setik.insert(mp(dp[x + 1][y], mp(x + 1, y)));
}
if (is(y, x) && dp[y][x] > dp[x][y] + abs(a[x] - a[y])){
dp[y][x] = dp[x][y] + abs(a[x] - a[y]);
setik.insert(mp(dp[y][x], mp(y, x)));
}
if (x > 1 && is(y, x - 1) && dp[y][x - 1] > dp[x][y] + abs(a[x] - a[y])){
dp[y][x - 1] = dp[x][y] + abs(a[x] - a[y]);
setik.insert(mp(dp[y][x - 1], mp(y, x - 1)));
}
if (is(y + 1, x) && dp[y + 1][x] > dp[x][y] + abs(a[x] - a[y + 1])){
dp[y + 1][x] = dp[x][y] + abs(a[x] - a[y + 1]);
setik.insert(mp(dp[y + 1][x], mp(y + 1, x)));
}
if (is(y + 1, x - 1) && dp[y + 1][x - 1] > dp[x][y] + abs(a[x] - a[y + 1])){
dp[y + 1][x - 1] = dp[x][y] + abs(a[x] - a[y + 1]);
setik.insert(mp(dp[y + 1][x - 1], mp(y + 1, x - 1)));
}
}
cout << "NO\n";
}
Compilation message
climbers.cpp: In function 'int main()':
climbers.cpp:65:29: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
65 | if (a[i] < a[i - 1] && a[i] < a[i + 1] || a[i - 1] < a[i] && a[i] > a[i + 1]){
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
2132 KB |
Output is correct |
3 |
Correct |
7 ms |
12168 KB |
Output is correct |
4 |
Correct |
42 ms |
82724 KB |
Output is correct |
5 |
Correct |
115 ms |
196044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
2 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
2 ms |
2004 KB |
Output is correct |
3 |
Correct |
90 ms |
11436 KB |
Output is correct |
4 |
Correct |
194 ms |
76048 KB |
Output is correct |
5 |
Correct |
250 ms |
75472 KB |
Output is correct |
6 |
Correct |
260 ms |
137740 KB |
Output is correct |
7 |
Correct |
273 ms |
130364 KB |
Output is correct |
8 |
Correct |
257 ms |
185900 KB |
Output is correct |
9 |
Correct |
321 ms |
187700 KB |
Output is correct |
10 |
Correct |
314 ms |
185512 KB |
Output is correct |