이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
int n,l[N],d[N],t[N * 4],val[N];
void build(int v = 1,int tl = 1,int tr = n) {
if(tl == tr) {
t[v] = l[tl];
} else {
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);
build(v + v + 1,tm + 1,tr);
t[v] = min(t[v + v],t[v + v + 1]);
}
}
const int inf = 1e9 + 2;
int get(int l,int r,int v = 1,int tl = 1,int tr = n) {
if(l > r || tl > r || l > tr) return inf;
if(tl >= l && tr <= r) {
return t[v];
}
int tm = (tl + tr) >> 1;
return min(get(l,r,v + v,tl,tm),get(l,r,v + v + 1,tm + 1,tr));
}
int res = inf, x, y;
void calc() {
d[x] = y - 1;
for(int i = x + 1;i <= n;i++) {
int t = min(y,get(x,i));
d[i] = i - x + t - 1;
t = min(y,get(x,i - 1));
d[i] = min(d[i],(i - 1) - x + l[i - 1] - t + 1);
}
for(int i = x - 1;i >= 1;i--) {
int t = min(y,get(i,x));
d[i] = x - i + t - 1;
}
set<pair<int,int>> st;
for(int i = 1;i <= n;i++) {
st.insert({d[i],i});
}
while(!st.empty()) {
auto [z,v] = (*st.begin());
st.erase(st.begin());
auto upd = [&](int x) {
if(x < 1 || x > n) return;
if(d[x] > d[v] + 1) {
st.erase({d[x],x});
d[x] = d[v] + 1;
st.insert({d[x],x});
}
};
upd(v - 1);
upd(v + 1);
}
// for(int i = 1;i <= n;i++) {
// cout << i << ' ' << d[i] << '\n';
// }
}
void test() {
int x1, y1;
cin >> n >> x >> y >> x1 >> y1;
for(int i = 1; i <= n; i++) {
cin >> l[i];
l[i]++;
}
build();
// dont visit col 1
auto get1 = [&](int l,int r) {
if(r < l) swap(l,r);
return get(l,r);
};
for(int i = 1;i <= min(x,x1);i++) {
int v = min({y,get1(i,x),get1(i,x1)});
res = min(res,abs(x - i) + abs(x1 - i) + abs(y1 - v));
}
for(int i = max(x,x1);i <= n;i++) {
int v = min({y,get1(i,x),get1(i,x1)});
res = min(res,abs(x - i) + abs(x1 - i) + abs(y1 - v));
}
//visit col 1
for(int i = 1;i <= n;i++) {
val[i] = inf;
}
calc();
val[x1] = l[x1] - y1;
int mn = l[x1],mn1 = abs(l[x1] - y1) - x1;
for(int i = x1 + 1;i <= n;i++) {
if(l[i] > mn) continue;
// res = min(res,d[i] + i + mn1);
mn = l[i];
mn1 = min(mn1,abs(l[i] - y1) - i);
}
for(int i = x1; i > 1; i--) {
int t = abs(y1 - get(i - 1,x1)) + (x1 - (i - 1)) + 1 + d[i];
res = min(res,t);
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# | 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... |