이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define GR(a,n,m) vector<vector<int>> a(n, vector<int>(m, 0));
ll min_total_length(vector<int> a, vector<int> b)
{
vector<pair<int,int>> v;
set<int> s[2];
for(auto x : a){
v.pb({x, 0});
s[0].insert(x);
}
for(auto x: b){
v.pb({x, 1});
s[1].insert(x);
}
sort(all(v));
int n = sz(v);
vector<ll> dp(n + 5, 1e16);
dp[0] = 0;
ll k = -1, sum = 0;
for(int i = 0;i < n;i++){
ll mn = 1e16;
if(v[i].second == 1){
int pp = upper_bound(all(a), v[i].first) - a.begin();
if(pp < sz(a))mn = min(mn, 1LL * abs(v[i].first - a[pp]));
if(pp)mn = min(mn, 1LL * abs(v[i].first - a[pp-1]));
}else{
int pp = upper_bound(all(b), v[i].first) - b.begin();
if(pp < sz(b))mn = min(mn, 1LL * abs(v[i].first - b[pp]));
if(pp)mn = min(mn, 1LL * abs(v[i].first - b[pp-1]));
}
dp[i + 1] = dp[i] + mn;
if(v[i].second != v[i - 1].second){
sum = v[i].first - v[i - 1].first;
k = i - 1;
}else if(k > 0 && v[k].second == v[k - 1].second){
--k;
sum += v[i].first - v[k].first;
}else k = -1;
if(k >= 0)dp[i + 1] = min(dp[i + 1], dp[k] + sum);
}
return dp[n];
}
# | 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... |