이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
#include "walk.h"
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<tuple>
#include<algorithm>
using namespace std;
#define pb push_back
#define snd second
#define fst first
typedef long long int ll;
const ll INF = 9e18;
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
int n, m;
map<ll, ll> dst; // altura, custo vertical, barra horizontal
map<ll, vector<ll>> com, fim;
n = x.size();
m = l.size();
for (int i = 0; i < m; i++){ // altura tem que bater pelo enunciado
com[x[l[i]]].push_back(y[i]);
fim[x[r[i]]].push_back(y[i]); // sei onde começa e onde acaba
}
// tomar cuidado com inicio e fim na mesma altura na mesma barra
ll ans = INF; //INF
dst[0] = 0; // altura zero no inicio
fim[x[0]].pb(0); // depois não é mais válido
for (int i = 0; i < n; i++){
set<int> stay;
for (auto e : com[x[i]]){
if (dst.count(e))
stay.insert(e); // não apaga essa altura, pode manter o mesmo valor
else{
auto it = dst.lower_bound(e);
auto dit = it;
if (dit != dst.begin()) // se existe
dit--;
ll val = INF; //INF
if ((it != dst.end())){ // caminho válido
val = min(val, (*it).snd + abs((*it).fst - e));
}
if ((dit != dst.end())){ // caminho válido
val = min(val, (*dit).snd + abs((*dit).fst - e));
}
dst[e] = val;
}
}
if (i == n - 1){
for (auto e : dst)
ans = min(ans, e.fst + e.snd);
}
for (auto e : fim[x[i]])
if (!stay.count(e))
dst.erase(e);
// cout<<i<<" "<<x[i]<<endl;
// for (auto e : dst)
// cout<<e.fst<<" -- "<<e.snd<<endl;
// cout<<"--------"<<endl<<endl;
}
if (ans >= INF)
return -1;
return ans + (ll)((ll)x[n - 1] - (ll)x[0]);
}
// signed main(){
// int n, m;
// assert(2 == scanf("%d%d", &n, &m));
// vector<int> x(n), h(n);
// for (int i = 0; i < n; i++)
// assert(2 == scanf("%d%d", &x[i], &h[i]));
// vector<int> l(m), r(m), y(m);
// for (int i = 0; i < m; i++)
// assert(3 == scanf("%d%d%d", &l[i], &r[i], &y[i]));
// int s, g;
// assert(2 == scanf("%d%d", &s, &g));
// fclose(stdin);
// long long result = min_distance(x, h, l, r, y, s, g);
// printf("%lld\n", result);
// fclose(stdout);
// 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... |