Submission #1112607

#TimeUsernameProblemLanguageResultExecution timeMemory
1112607SalihSahinWiring (IOI17_wiring)C++14
0 / 100
1082 ms262144 KiB
#include <bits/stdc++.h> #define pb push_back using namespace std; #include "wiring.h" const long long inf = 1e17; long long min_total_length(vector<int> r, vector<int> b) { int n = r.size(), m = b.size(); vector<long long> mndisr(n), mndisb(m); int indb = 0, indr = 0; for(int i = 0; i < n; i++){ while(indb + 1 < m && b[indb+1] < r[i]) indb++; mndisr[i] = min((long long)abs(r[i] - b[indb]), (indb + 1 < m ? (long long)abs(r[i] - b[indb+1]) : inf)); } for(int i = 0; i < m; i++){ while(indr + 1 < n && r[indr+1] < b[i]) indr++; mndisb[i] = min((long long)abs(b[i] - r[indr]), (indr + 1 < n ? (long long)abs(b[i] - r[indr+1]) : inf)); } vector<int> adj[n]; indb = 0; for(int i = 0; i < n; i++){ while(indb + 1 < m && b[indb+1] < r[i]) indb++; for(int j = indb; j >= 0; j--){ if(mndisr[i] + mndisb[j] < r[i] - b[j]) continue; adj[i].pb(j); } for(int j = indb + 1; j < m; j++){ if(mndisr[i] + mndisb[j] < b[j] - r[i]) continue; adj[i].pb(j); } } vector<vector<long long> > dp(n+1, vector<long long>(m+1, inf)); dp[0][0] = 0; for(int i = 0; i <= n; i++){ if(i < n) dp[i+1][0] = dp[i][0] + mndisr[i]; for(int j = 0; j <= m; j++){ //if(i < n) dp[i+1][j] = min(dp[i+1][j], dp[i][j] + mndisr[i]); if(j < m) dp[i][j+1] = min(dp[i][j+1], dp[i][j] + mndisb[j]); } if(i < n){ for(auto itr: adj[i]){ dp[i + 1][itr + 1] = min(dp[i + 1][itr + 1], dp[i][itr] + abs(r[i] - b[itr])); } } } 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...