Submission #294921

#TimeUsernameProblemLanguageResultExecution timeMemory
294921Aldas25전선 연결 (IOI17_wiring)C++14
20 / 100
36 ms6268 KiB
#include "wiring.h"
#include <bits/stdc++.h>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;

const ll INF = 1e16;

ll dp[210][210];

ll distToNearest (ll x, vi coords) {
    auto it = lower_bound(coords.begin(), coords.end(), x);
    ll ret = INF;
    if (it != coords.end()) {
        ll c = *it;
        ret = min(ret, abs(x - c));
    }
    if (it != coords.begin()) {
        it--;
        ll c = *it;
        ret = min(ret, abs(x - c));
    }
    return ret;
}

long long min_total_length(std::vector<int> r, std::vector<int> b) {
	int n = (int)r.size();
	int m = (int)b.size();

    ll rMost = r[n-1], lMost = b[0];

    if (rMost < lMost) {

        int fId = 0, sId = 0;
        ll ans = 0;
        while (fId < n && sId < m) {
            ans += b[sId] - r[fId];
            fId++, sId++;
        }
        while (sId < m) {
            ans += b[sId] - rMost;
            sId++;
        }
        while (fId < n) {
            ans += lMost - r[fId];
            fId++;
        }
        return ans;
    }

    FOR(i, 0, n) FOR(j, 0, m) dp[i][j] = INF;
    dp[0][0] = 0ll;

    FOR(i, 0, n) FOR(j, 0, m) {
        if (i == 0 && j == 0) continue;
        if (i > 0) dp[i][j] = min(dp[i][j], dp[i-1][j] + distToNearest(r[i-1], b));
        if (j > 0) dp[i][j] = min(dp[i][j], dp[i][j-1] + distToNearest(b[j-1], r));
        if (i > 0 && j > 0)
            dp[i][j] = min(dp[i][j], dp[i-1][j-1] + abs(r[i-1] - b[j-1]));
    }

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