Submission #1064189

#TimeUsernameProblemLanguageResultExecution timeMemory
1064189IgnutWiring (IOI17_wiring)C++17
0 / 100
127 ms262144 KiB
// Ignut

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll INF = 1e18 + 123;

ll min_total_length(vector<int> r, vector<int> b) {
    int n = r.size(), m = b.size();
    ll dp[n + 1][m + 1];
    for (int i = 0; i <= n; i ++)
        for (int j = 0; j <= m; j ++)
            dp[i][j] = INF;
    dp[0][0] = 0;
    for (int i = 0; i <= n; i ++) {
        int pos = lower_bound(b.begin(), b.end(), (i == 0 ? 0 : r[i - 1])) - b.begin();
        int lo = max(0, pos - 3);
        int hi = min(m, pos + 3);
        for (int j = lo; j <= hi; j ++) {
            if (i < n && j > 0)
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(r[i] - b[j - 1]));
            if (j < m && i > 0)
                dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(r[i - 1] - b[j]));
            if (i < n && j < m)
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(r[i] - b[j]));
        }
                
        pos = lower_bound(b.begin(), b.end(), i == m ? r[i - 1] : r[i]) - b.begin();
        lo = max(0, pos - 3);
        hi = min(m, pos + 3);
        for (int j = lo; j <= hi; j ++) {
            if (i < n && j > 0)
                dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(r[i] - b[j - 1]));
            if (j < m && i > 0)
                dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + abs(r[i - 1] - b[j]));
            if (i < n && j < m)
                dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(r[i] - b[j]));
        }
    }
    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...