Submission #608618

#TimeUsernameProblemLanguageResultExecution timeMemory
608618PiejanVDCWiring (IOI17_wiring)C++17
7 / 100
144 ms262144 KiB
#include "wiring.h" #include <bits/stdc++.h> using namespace std; long long min_total_length(vector<int>r, vector<int>b) { const int n = (int)r.size(), m = (int)b.size(); long long dp[n+1][m+1]; for(int i = 0 ; i <= n ; i++) for(int j = 0 ; j <= m ; j++) dp[i][j] = 1e15; dp[0][0] = 0; int a = 0, c = 0; vector<pair<int,int>>v; bool ok = 1; int f, cnt = 0; for(int i = 0 ; i < n+m ; i++) { if(a == n) {v.push_back({1, b[c++]}); continue;} if(c == m) {v.push_back({0, r[a++]}); continue;} if(r[a] < b[c]) v.push_back({0, r[a++]}); else v.push_back({1, b[c++]}); if(i == 0) f = v.back().first; if(ok && v.back().first == f) cnt++; else ok = 0; } int A = 0, B = 0; if(f) B = cnt; else A = cnt; while(A+B < n+m) { int len = 0; c = v[A+B].first; int ii = A+B; while(ii < n+m && v[ii].first == c) ii++, len++; ii = A+B-1; c = 1 - c; int prev = 0; while(ii >= 0 && v[ii].first == c) ii--, prev++; c = 1 - c; for(int ii = A+B ; ii < A+B+len ; ii++) { if(c == 0) { int i = A+(ii - (A+B)+1); for(int j = B-prev+1 ; j <= B ; j++) { for(int k = B-prev+1 ; k <= B ; k++) dp[i][j] = min(dp[i][j], dp[i-1][j] + abs(v[ii].second - v[k+A-1].second)); for(int k = A+(ii - (A+B)+1) ; k <= A+len ; k++) dp[i][j] = min(dp[i][j], dp[i][j-1] + abs(v[j+A-1].second - v[k+B-1].second)); dp[i][j] = min(dp[i][j], dp[i-1][j-1] + abs(v[ii].second - v[j+A-1].second)); } } else { int j = B + (ii - (A+B)+1); for(int i = A-prev+1 ; i <= A ; i++) { for(int k = A-prev+1 ; k <= A ; k++) dp[i][j] = min(dp[i][j], dp[i][j-1] + abs(v[ii].second - v[k+B-1].second)); for(int k = B+(ii - (A+B)+1) ; k <= B+len ; k++) dp[i][j] = min(dp[i][j], dp[i-1][j] + abs(v[i+B-1].second - v[k+A-1].second)); dp[i][j] = min(dp[i][j], dp[i-1][j-1] + abs(v[ii].second - v[i+B-1].second)); } } } if(c) B += len; else A += len; } return dp[n][m]; }

Compilation message (stderr)

wiring.cpp: In function 'long long int min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:32:5: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
   32 |     if(f) B = cnt;
      |     ^~
#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...