Submission #1328727

#TimeUsernameProblemLanguageResultExecution timeMemory
1328727edo전선 연결 (IOI17_wiring)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#include "wiring.h"

ll min_total_length(vector<int> r, vector<int> b) {
    vector<pair<int, int>> all;
    int i = 0, j = 0;
    while(i < r.size() || j < b.size()) {
        if(i < r.size() && (j == b.size() || r[i] < b[j]))
            all.push_back({r[i++], 0});
        else
            all.push_back({b[j++], 1});
    }

    vector<vector<int>> blocks;
    vector<int> curr = {all[0].first};
    for(int k = 1; k < all.size(); ++k) {
        if(all[k].second == all[k - 1].second) 
            curr.push_back(all[k].first);
        else {
            blocks.push_back(curr);
            curr = {all[k].first};
        }
    }
    blocks.push_back(curr);

    int m = ssize(blocks);
    vector<ll> dp(ssize(blocks[0]) + 1, 4e18);
    dp[ssize(blocks[0])] = 0;

    for(i = 0; i < m - 1; i++) {
        vector<int> &b1 = blocks[i];
        vector<int> &b2 = blocks[i + 1];

        int n1 = b1.size();
        int n2 = b2.size();
        ll dist = b2[0] - b1[n1 - 1];

        vector<ll> d1(n1 + 1), d2(n2 + 1);
        for(j = 0; j < n1; ++j) d1[j + 1] = d1[j] + (b2[0] - b1[n1 - j - 1]);
        for(j = 0; j < n2; ++j) d2[j + 1] = d2[j] + (b2[j] - b1[n1 - 1]);

        vector<ll> next_dp(n2 + 1, 4e18);

        for(j = 1; j <= n2; j++) {
            for(int k = 1; k <= n1; k++) {
                ll cost = d2[j] + d1[k] + (ll)max(0, j - k) * (b2[0] - b1[n1 - 1]);
                if(k > j) cost += (ll)(k - j) * (b2[0] - b1[n1 - 1]);
                cost -= (ll)min(j, k) * dist;

                if(dp[n1 - k] != 4e18)
                    next_dp[j] = min(next_dp[j], dp[n1 - k] + cost);
            }
            if(j > 1) next_dp[j] = min(next_dp[j], next_dp[j - 1] + (ll)(b2[j - 1] - b1[n1 - 1])); 
        }
        dp = next_dp;
    }
    return dp.back();
}

#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...