Submission #394518

#TimeUsernameProblemLanguageResultExecution timeMemory
394518usachevd0Wiring (IOI17_wiring)C++17
0 / 100
1 ms292 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) {
  return y < x ? (x = y, true) : false; 
}
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) {
  return y > x ? (x = y, true) : false;
}
void debug_out() {
  cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
  cerr << ' ' << A;
  debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
  for (int i = 0; i < n; ++i) {
    cerr << a[i] << ' ';
  }
  cerr << endl;
}
#ifdef DEBUG
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
  #define debug(...) 1337
  #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) {
  for (auto& x : v) {
    stream << x << ' ';
  }
  return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
  return stream << p.first << ' ' << p.second;
}


const ll INF64 = 1e18;
const int INF32 = 1e9;

ll min_total_length(vector<int> A, vector<int> B) {
  if (A.back() < B.back()) swap(A, B);
  int n = A.size(), m = B.size();
  vector<int> minDist(m, INF32);
  vector<int> orb[n + 1];
  for (int j = 0; j < m; ++j) {
    int i = lower_bound(all(A), B[j]) - A.begin();
    if (i < n) {
      chkmin(minDist[j], labs(B[j] - A[i]));
    }
    if (i > 0) {
      chkmin(minDist[j], labs(B[j] - A[i - 1]));
    }
    for (int l = i - 1; l <= min(i, n - 1); ++l) {
      if (labs(B[j] - A[l]) == minDist[j]) {
        orb[l].push_back(B[j]);
        break;
      }
    }
  }

  vector<ll> dp[3];
  for (int t = 0; t < 3; ++t) {
    dp[t].assign(n + 1, INF64);
  }
  dp[0][0] = 0;

  for (int i = 0; i < n; ++i) {
    int _x1 = A[i];
    int _x2 = A[i];
    if (!orb[i].empty()) {
      _x1 = orb[i][0];
      _x2 = orb[i].back();
    }
    int j1 = lower_bound(all(B), _x1) - B.begin() - 1;
    int j2 = upper_bound(all(B), _x2) - B.begin();
    int fi = INF32;
    if (j1 >= 0) chkmin(fi, labs(A[i] - B[j1]));
    if (j2 < m) chkmin(fi, labs(A[i] - B[j2]));
    ll sumD = 0;
    for (int x : orb[i]) {
      sumD += labs(x - A[i]);
    }

    debug(i, sumD, j1, j2);

    if (sumD > 0) {
      dp[0][i + 1] = dp[0][i] + sumD;
      chkmin(dp[0][i + 1], dp[1][i] + sumD - labs(A[i] - orb[i][0]));
      if (j1 >= 0) chkmin(dp[0][i + 1], dp[2][i] + sumD + labs(A[i] - B[j1]));
    } else {
      dp[0][i + 1] = dp[0][i] + fi;
      if (j1 >= 0) chkmin(dp[0][i + 1], dp[2][i] + labs(A[i] - B[j1]));
    }

    if (j2 < m) {
      dp[1][i + 1] = min(dp[0][i] + sumD + labs(A[i] - B[j2]), dp[2][i] + sumD + labs(A[i] - B[j2]) + labs(A[i] - B[j1]));
      if (sumD > 0) {
        chkmin(dp[1][i + 1], dp[1][i] + sumD - labs(A[i] - orb[i][0]) + labs(A[i] - B[j2]));
      }
    }

    if (sumD > 0) {
      dp[2][i + 1] = min(dp[0][i] + sumD - labs(A[i] - orb[i].back()) + (orb[i].size() == 1 ? fi : 0), dp[2][i] + labs(A[i] - B[j1]) + sumD - labs(A[i] - orb[i].back()));
      if (orb[i].size() >= 2) {
        chkmin(dp[2][i + 1], dp[1][i] + sumD + (orb[i].size() == 2 ? fi : 0) - labs(A[i] - orb[i].back()) - labs(A[i] - orb[i][0]));
      }
    }

    //debug(i, dp[0][i + 1], dp[1][i + 1], dp[2][i + 1]);
  }
  return dp[0][n];
}


#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  vector<int> A(n), B(m);
  for (int& x : A) {
    cin >> x;
  }
  for (int& x : B) {
    cin >> x;
  }
  cout << min_total_length(A, B) << '\n';
    
  return 0;
}
#endif

Compilation message (stderr)

wiring.cpp: In function 'll min_total_length(std::vector<int>, std::vector<int>)':
wiring.cpp:40:22: warning: statement has no effect [-Wunused-value]
   40 |   #define debug(...) 1337
      |                      ^~~~
wiring.cpp:101:5: note: in expansion of macro 'debug'
  101 |     debug(i, sumD, j1, j2);
      |     ^~~~~
#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...