제출 #416493

#제출 시각아이디문제언어결과실행 시간메모리
416493arvindr9전선 연결 (IOI17_wiring)C++14
20 / 100
44 ms3120 KiB
#include <bits/stdc++.h> using namespace std; #define pi pair<int, int> #define pb push_back #define mp make_pair #define eb emplace_back #define f first #define s second typedef long long ll; const ll inf = 1e18; ll min_total_length(vector<int> r, vector<int> b) { int n = r.size(); int m = b.size(); sort(r.begin(), r.end()); sort(b.begin(), b.end()); if (n <= 200 and m <= 200) { vector<vector<ll>> dp(n, vector<ll>(m, inf)); ll curcost = 0; for (int i = 0; i < m; i++) { curcost += abs(r[0] - b[i]); dp[0][i] = curcost; } for (int i = 1; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = min(dp[i][j], abs(r[i] - b[j]) + dp[i-1][j]); curcost = abs(r[i] - b[j]); for (int k = j - 1; k >= 0; k--) { dp[i][j] = min(dp[i][j], curcost + dp[i-1][k]); curcost += abs(r[i] - b[k]); } } } return dp[n-1][m-1]; } int mn = min(n, m); ll ans = 0; for (int i = 0; i < mn; i++) { ans += (b[i] - r[n - 1 - i]); } for (int i = mn; i < max(n, m); i++) { if (i < m) ans += (b[i] - r[n - 1]); else ans += (b[0] - r[n - 1 - i]); } return ans; } // int main() { // ios::sync_with_stdio(0); // cin.tie(0); // return 0; // }
#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...