이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define lg length()
#define pb push_back
#define INF 2000000005
#define LINF 1000000000000000005
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(rng);}
#include "wiring.h"
long long min_total_length(vector<int> r, vector<int> b) {
int n = sz(r) + sz(b);
vector <pii> a;
for(int i : r) a.pb({i, 0});
for(int i : b) a.pb({i, 1});
sort(all(a));
vector <ll> dp(n+1);
int j = 0;
ll sum = 0;
dp[0] = 0;
for(int i=0;i<n;i++){
dp[i+1] = LINF;
if(a[i].y == 0){
auto it = lower_bound(all(b), a[i].x);
if(it != b.end()) dp[i+1] = min(dp[i+1], dp[i] + (*it) - a[i].x);
if(it != b.begin()) it--, dp[i+1] = min(dp[i+1], dp[i] + a[i].x - (*it));
}
else{
auto it = lower_bound(all(r), a[i].x);
if(it != r.end()) dp[i+1] = min(dp[i+1], dp[i] + (*it) - a[i].x);
if(it != r.begin()) it--, dp[i+1] = min(dp[i+1], dp[i] + a[i].x - (*it));
}
if(i && a[i].y != a[i-1].y) j = i, sum = 0;
if(j && a[i].y != a[j-1].y) j--, sum += a[i].x - a[j].x, dp[i+1] = min(dp[i+1], dp[j] + 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... |