Submission #389976

#TimeUsernameProblemLanguageResultExecution timeMemory
389976KeshiWiring (IOI17_wiring)C++17
0 / 100
45 ms8236 KiB
//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;
			for(ll k = j; k > ls[j]; k--){
				x += a[i].F - a[k].F;
				dp[i + 1] = min(dp[i + 1], min(dp[k], dp[k + 1]) + x);
			}
			continue;
		}
		ll x = a[i].F - a[j].F;
		dp[i + 1] = x + a[i - 1].F - a[j].F + min(dp[j], dp[j + 1]);
		for(ll k = j - 1; k > ls[j]; k--){
			x += a[i - 1].F - a[k].F;
			dp[i + 1] = min(dp[i + 1], min(dp[k], dp[k + 1]) + x);
		}
	}

	return dp[n];
}

/*int main(){
    fast_io;



    return 0;
}*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...