제출 #395378

#제출 시각아이디문제언어결과실행 시간메모리
395378idk321전선 연결 (IOI17_wiring)C++11
20 / 100
72 ms9900 KiB
#include "wiring.h"

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 205;
ll dp[N][N][4];
const ll M = 1000000000000000000LL;

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


    if (r[n - 1] <= b[0])
    {
        int cur = n - 1;
        ll res = 0;
        for (int i = 0; i < m; i++)
        {
            if (cur >= 0) res += b[i] - r[cur];
            else res += b[i] - r[n - 1];
            cur--;
        }

        while (cur >= 0)
        {
            res += b[0] - r[cur];
            cur--;
        }

        return res;
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            for (int k = 0; k < 4; k++) dp[i][j][k] = M;
        }
    }

    vector<array<int, 2>> byType;
    for (int i = 0; i  <n; i++)
    {
        byType.push_back({r[i], i});
    }
    for (int i = 0; i < m; i++)
    {
        byType.push_back({b[i], i});
    }
    sort(byType.begin(), byType.end());


    dp[0][0][0] = 0;
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= m; j++)
        {
            for (int k = 0; k < 4; k++)
            {
                if (i < n)
                {
                    if (i == 0 || k & 1)
                    {
                         dp[i + 1][j][k & 2] = min(dp[i][j][k], dp[i + 1][j][k & 2]);

                        if (j != 0) dp[i + 1][j][3] = min(dp[i][j][k] + abs(r[i] - b[j - 1]), dp[i + 1][j][3]);
                        //cout << i << " " <<j << " " << dp[i + 1][j][3] << endl;
                    }

                }
                if (j < m)
                {
                    if (j == 0 || k & 2)
                    {
                        dp[i][j + 1][k & 1] = min(dp[i][j][k], dp[i][j + 1][k & 1]);
                        if (i != 0) dp[i][j + 1][3] = min(dp[i][j][k] + abs(r[i - 1] - b[j]), dp[i][j + 1][3]);
                    }
                }

                //cout << i << " " << j << " " << k << " "<< dp[i][j][k] << endl;
            }


        }
    }

	return dp[n][m][3];
}
#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...