이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve() {
int n, sl, sc, el, ec;
cin >> n >> sl >> sc >> el >> ec;
int l[n+1];
bool T = 0;
for(int i=1; i<=n; ++i) {
cin >> l[i];
++l[i];
if(i != 1 && l[i] != l[i-1]) T = 1;
}
if(!T) {
int ans = abs(el - sl) + abs(ec - sc);
if(sl > el) {
ans = min(ans, sl - el - 1 + sc + l[1] - ec);
ans = min(ans, sl - el + 1 + l[1] - sc + ec);
} else if(sl < el) {
ans = min(ans, el - sl - 1 + l[1] - sc + ec);
ans = min(ans, el - sl + 1 + l[1] + ec - sc);
} else {
if(sc == ec) {
cout << 0 << endl;
return;
}
if(sc < ec && sl != 1) {
ans = min(ans, sc + l[1] - ec + 1);
} else if(ec < sc) {
ans = min(ans, l[1] - sc + ec + 1);
}
}
cout << ans << endl;
return;
}
if(n == 1) {
cout << 0 << endl;
return;
} else if(n == 2) {
int ans;
if(el == 2 && sl != 2) {
ans = sc;
ans = min(ans, l[1] - sc + 1);
} else if(el == 2 && sl == 2) {
cout << 0 << endl;
return;
} else if(sl == 2) {
ans = ec;
ans = min(ans, 1 + l[1] - ec);
} else {
ans = abs(ec - sc);
ans = min(ans, l[1] - sc + 1 + ec);
}
cout << ans << endl;
return;
}
priority_queue<pair<int,pair<int,int>>>pq;
vector<vector<int>>dp(n+1, vector<int>(5005, INT_MAX));
vector<vector<bool>>visited(n+1, vector<bool>(5005, 0));
dp[sl][sc] = 0;
pq.push({0, {sl, sc}});
while(!pq.empty()) {
int x = pq.top().second.first, y = pq.top().second.second;
if(x == el && y == ec) break;
pq.pop();
if(visited[x][y]) continue;
visited[x][y] = 1;
if(x != n) { // down
int new_y = y;
new_y = min(new_y, l[x+1]);
if(dp[x+1][new_y] > dp[x][y] + 1) {
dp[x+1][new_y] = dp[x][y] + 1;
pq.push({-dp[x+1][new_y], {x+1, new_y}});
}
}
if(x != 1) { // up
int new_y = y;
new_y = min(new_y, l[x-1]);
if(dp[x-1][new_y] > dp[x][y] + 1) {
dp[x-1][new_y] = dp[x][y] + 1;
pq.push({-dp[x-1][new_y], {x-1, new_y}});
}
}
if(y == 1 && x != 1) { // left up
int new_x = x - 1, new_y = l[new_x];
if(dp[x][y] + 1 < dp[new_x][new_y]) {
dp[new_x][new_y] = dp[x][y] + 1;
pq.push({-dp[new_x][new_y], {new_x, new_y}});
}
} else if(y != 1) { // left
if(dp[x][y] + 1 < dp[x][y-1]) {
dp[x][y-1] = dp[x][y] + 1;
pq.push({-dp[x][y-1], {x, y-1}});
}
}
if(y == l[x] && x != n) { // right down
int new_x = x+1, new_y = 1;
if(dp[x][y] + 1 < dp[new_x][new_y]) {
dp[new_x][new_y] = dp[x][y] + 1;
pq.push({-dp[new_x][new_y], {new_x, new_y}});
}
} else if(y != l[x]) { // right
if(dp[x][y] + 1 < dp[x][y+1]) {
dp[x][y+1] = dp[x][y] + 1;
pq.push({-dp[x][y+1], {x, y+1}});
}
}
}
cout << dp[el][ec] << endl;
}
signed main() {
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |