Submission #1199784

#TimeUsernameProblemLanguageResultExecution timeMemory
1199784lrnnzWiring (IOI17_wiring)C++20
100 / 100
20 ms6576 KiB
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include "wiring.h"
using namespace std;
 
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define ll long long
#define ld long double
#define ui uint64_t
#define cont(set, element) ((set).find(element) != (set).end())
 
/********* DEBUG *********/
 
template <typename T>
void outvec(const vector<T>& Z){
    for (const T& x : Z)
    cout << x << ' ';
    cout << "\n";
}
void printVariable(const any& var) {
    if (!var.has_value()) {
        cout << "null";
        return;
    }

    if (var.type() == typeid(int)) {
        cout << any_cast<int>(var);
    } else if (var.type() == typeid(double)) {
        cout << any_cast<double>(var);
    } else if (var.type() == typeid(float)) {
        cout << any_cast<float>(var);
    } else if (var.type() == typeid(char)) {
        cout << any_cast<char>(var);
    } else if (var.type() == typeid(bool)) {
        cout << (any_cast<bool>(var) ? "true" : "false");
    } else if (var.type() == typeid(string)) {
        cout << any_cast<string>(var);
    } else if (var.type() == typeid(const char*)) {
        cout << any_cast<const char*>(var);
    } else if (var.type() == typeid(long long)) {
        cout << any_cast<long long>(var);
    } else {
        cout << "[unknown type]";
    }
}

template<typename... Args>
void outval(Args... args) {
    vector<any> variables = {args...};
    
    for (size_t i = 0; i < variables.size(); ++i) {
        printVariable(variables[i]);
        if (i != variables.size() - 1) {
            cout << " ";
        }
    }
    cout << "\n";
}

#define nl "\n"
#define sp << " " <<

/********* DEBUG *********/

const ll MOD = 1000000007;
const ll inf = 1e18;
const ll mxN = 1000005;

ll min_total_length(vector<int> r, vector<int> b) {
    ll n = sz(r), m = sz(b);
    vector<ll> vec(n+m), col(n+m);

    ll rptr = 0, bptr = 0;
    for (int i = 0; i < n+m; i++){
        if (rptr < n && (bptr == m || r[rptr] < b[bptr])){
            vec[i] = r[rptr];
            rptr++;
        }
        else{
            vec[i] = b[bptr];
            col[i] = 1;
            bptr++;
        }
    }

    vector<ll> dp(n+m+1, inf);
    dp[0] = 0;
    ll back = 0;
    for (int i = 0; i < n+m;){
        ll j = i+1;
        while (j < n+m && col[i] == col[j])
            j++;
        
        //outval("back,i,j:",back,i,j);
        if (i){
            // connect prev cluster to nearest next block
            for (int k = back; k < i; k++){
                dp[k+1] = min(dp[k+1], dp[k] + vec[i] - vec[k]);
            }

            // 1 to 1 match
            ll val = 0;
            for (int k = i; k < j && i-(k-i)-1 >= back; k++){
                val += vec[k] - vec[i-(k-i)-1];
                dp[k+1] = min(dp[k+1], dp[i-(k-i)-1] + val);
            }

            // connect curr cluster to nearest prev block
            for (int k = i; k < j; k++){
                dp[k+1] = min(dp[k+1], dp[k] + vec[k] - vec[i-1]);
            }
        }

        back = i;
        i = j;
    }

    //outvec(dp);
    return dp[n+m];
}
#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...