이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10, LG=20;
int n, sl, sc, el, ec, a[N], st[LG][N];
int get(int l, int r){
if (l>r) swap(l, r);
int lg=__lg(r-l+1);
return min(st[lg][l], st[lg][r-(1<<lg)+1]);
}
int move(int &x, int &y, int z, int t){
if (x+1==z && y==a[x]+1 && t==1){
x=z, y=t;
return 1;
}
if (z+1==x && t==a[z]+1 && y==1){
x=z, y=t;
return 1;
}
int ans=abs(x-z);
y=min(get(x, z), y);
x=z;
ans+=abs(y-t); y=t;
return ans;
}
int calc(int x, int y, vector<pair<int, int>> path){
int ans=0;
for (auto &i:path){
ans+=move(x, y, i.first, i.second);
}
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> sl >> sc >> el >> ec;
for (int i=1; i<=n; ++i) cin >> a[i], st[0][i]=a[i]+1;
++n;
for (int i=1; i<LG; ++i) for (int j=1; j+(1<<i)-1<=n; ++j) st[i][j]=min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
int tl=sl, tc=sc;
int ans=move(tl, tc, el, ec);
for (int i=1; i<=n; ++i){
for (int j=2; j<=n; ++j){
ans=min(ans, calc(sl, sc, {{i, 1}, {j, 1}, {j-1, a[j-1]+1}, {el, ec}}));
if (i<n) ans=min(ans, calc(sl, sc, {{i, a[i]+1}, {i+1, 1}, {j, 1}, {j-1, a[j-1]+1}, {el, ec}}));
}
}
cout << ans << '\n';
return 0;
}
# | 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... |