제출 #249280

#제출 시각아이디문제언어결과실행 시간메모리
249280ernestvw전선 연결 (IOI17_wiring)C++11
20 / 100
135 ms5368 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define sz(x) ((int)x.size()) #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); int n,m; vector<ll>R,B; vector<vector<ll>> dp; ll f(int i, int j) { if(i == n && j == m) return 0; if(dp[i][j] != -1) return dp[i][j]; dp[i][j] = 1e18; if(j < m) { for(int k = 0; k < n; ++k) dp[i][j] = min(dp[i][j], f(i, j + 1) + abs(B[j] - R[k])); } if(i < n) { for(int k = 0; k < m; ++k) dp[i][j] = min(dp[i][j], f(i + 1, j) + abs(B[k] - R[i])); } if(i < n && j < m) dp[i][j] = min(dp[i][j], f(i + 1, j + 1) + abs(B[j] - R[i])); return dp[i][j]; } ll solveSub1() { dp.assign(n+1, vector<ll>(m+1, -1)); return f(0, 0); } ll solveSub2() { ll ans = 0; if(n >= m) { for(int i = 0; i < n - m; ++i) ans += B[0] - R[i]; for(int i = 0; i < m; ++i) ans += B[i] - R[n - m + i]; } else { for(int i = 0; i < n; ++i) ans += B[i] - R[i]; for(int i = 0; i < m - n; ++i) ans += B[n + i] - R[n-1]; } return ans; } ll min_total_length(vector<int> r, vector<int> b) { n = sz(r); m = sz(b); R.resize(n),B.resize(m); for(int i=0;i<n;++i)R[i]=r[i]; for(int i=0;i<m;++i)B[i]=b[i]; if(max(n, m) <= 200) return solveSub1(); return solveSub2(); }
#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...