이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//In the name of God
#include <bits/stdc++.h>
#include "wiring.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define lc (id << 1)
#define rc (lc | 1)
ll ls[maxn], dp[maxn];
vector<pll> a;
long long min_total_length(vector<int> r, vector<int> b){
for(ll i : r){
a.pb(Mp(i, 0));
}
for(ll j : b){
a.pb(Mp(j, 1));
}
sort(all(a));
ll las[2] = {-1, -1};
ll n = Sz(a);
for(ll i = 0; i < n; i++){
ls[i] = las[a[i].S ^ 1];
las[a[i].S] = i;
ll j = ls[i];
dp[i + 1] = inf;
if(j == -1) continue;
if(i - j > 2){
dp[i + 1] = dp[i] + a[i].F - a[j].F;
continue;
}
if(i - j == 1){
ll x = 0;
dp[i + 1] = dp[i] + a[i].F - a[i - 1].F;
for(ll k = j; k > ls[j]; k--){
x += a[i].F - a[k].F;
dp[i + 1] = min(dp[i + 1], dp[k] + x);
}
continue;
}
ll x = a[i].F - a[j].F;
dp[i + 1] = x + a[i - 1].F - a[j].F + dp[j];
dp[i + 1] = min(dp[i + 1], dp[j] + x + a[i - 1].F + a[j].F);
for(ll k = j - 1; k > ls[j]; k--){
x += a[i - 1].F - a[k].F;
dp[i + 1] = min(dp[i + 1], dp[k] + x);
}
}
return dp[n];
}
/*int main(){
fast_io;
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... |