제출 #1328716

#제출 시각아이디문제언어결과실행 시간메모리
1328716edo전선 연결 (IOI17_wiring)C++20
0 / 100
1 ms352 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[i]))
            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<vector<ll>> dp(m);
    for(i = 0; i < m; i++) dp[i].resize(ssize(blocks[i]) + 1, 4e18);

    vector<ll> prev_dp(ssize(blocks[0]) + 1, 4e18);
    prev_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();

        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) d1[j + 1] = d1[j] + (b2[j] - b1[n1 - 1]);

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

                if(prev_dp[n1 - k] != 4e18)
                    dp[i + 1][j] = min(dp[i + 1][j], prev_dp[n1 - k] + cost);
            }
        }
        prev_dp = dp[i + 1];
    }
    return dp[m - 1][ssize(blocks[m - 1])];
}

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