This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include "walk.h"
#define F first
#define S second
#define PB push_back
#define sz(s) (int(s.size()))
#define bit(n, k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 1e5 + 10;
const ll INF = 2e18;
vector<int> tdo[maxn][2];
ll tmp[maxn];
ll min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int src, int snk){
int n = sz(x), m = sz(y);
assert(src == 0 && snk == n-1);
set<pll> st;
ll lazy = 0;
st.insert({0, 0});
for(int i = 0; i < m; i++){
tdo[l[i]][0].PB(y[i]);
if(r[i] != n-1)
tdo[r[i]][1].PB(y[i]);
}
for(int i = 0; i < n; i++){
for(int j = 0; j < sz(tdo[i][0]); j++){
int H = tdo[i][0][j];
auto it = st.lower_bound({H, -1});
tmp[j] = INF;
if(it != st.end() && (it->F) <= h[i])
tmp[j] = min(tmp[j], (it->S) + abs(H - (it->F)));
if(it != st.begin())
it--, tmp[j] = min(tmp[j], (it->S) + abs(H - (it->F)));
}
for(int H : tdo[i][1]){
st.erase(st.lower_bound({H, -1}));
}
for(int j = 0; j < sz(tdo[i][0]); j++){
if(tmp[j] != INF)
st.insert({tdo[i][0][j], tmp[j]});
}
if(i > 0)
lazy+= x[i] - x[i-1];
}
auto it = st.begin();
if(st.empty())
return -1;
return lazy + (it->F) + (it->S);
}
# | 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... |