Advertisement
pb_jiang

abc273f WA

Oct 18th, 2022
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
  5. template <typename... Args> void logger(string vars, Args &&... values)
  6. {
  7.     cerr << vars << " = ";
  8.     string delim = "";
  9.     (..., (cerr << delim << values, delim = ", "));
  10.     cerr << endl;
  11. }
  12.  
  13. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  14. using ll = long long;
  15. using pii = pair<int, int>;
  16. using pll = pair<ll, ll>;
  17. using piii = pair<int, pii>;
  18.  
  19. int n;
  20. vector<piii> events;
  21. vector<vector<pll>> dp;
  22. vector<int> key_contains;
  23. int ori = 0;
  24. int dest = 0;
  25.  
  26. ll search(int beg, int end, int dir)
  27. {
  28.     if (dir == 0 && dp[beg][end].first < LLONG_MAX)
  29.         return dp[beg][end].first;
  30.     if (dir == 1 && dp[beg][end].second < LLONG_MAX)
  31.         return dp[beg][end].second;
  32.     if (beg > end)
  33.         return LLONG_MAX / 2;
  34.     ll ans = LLONG_MAX / 2;
  35.     if (dir) {
  36.         int wtype = events[end].second.first;
  37.         if ((events[end].second.second == 1 && beg <= key_contains[wtype] && end - 1 >= key_contains[wtype]) ||
  38.             (events[end].second.second == 0 || events[end].second.second == 2 || events[end].second.second == -1)) {
  39.             ans = min(search(beg, end - 1, 0) + events[end].first - events[beg].first,
  40.                       search(beg, end - 1, 1) + events[end].first - events[end - 1].first);
  41.         }
  42.         return dp[beg][end].second = ans;
  43.     } else {
  44.         int wtype = events[beg].second.first;
  45.         if ((events[beg].second.second == 1 && beg + 1 <= key_contains[wtype] && end >= key_contains[wtype]) ||
  46.             (events[beg].second.second == 0 || events[beg].second.second == 2 || events[beg].second.second == -1)) {
  47.             ans = min(search(beg + 1, end, 0) + events[beg + 1].first - events[beg].first,
  48.                       search(beg + 1, end, 1) + events[end].first - events[beg].first);
  49.         }
  50.         return dp[beg][end].first = ans;
  51.     }
  52.     return ans;
  53. }
  54.  
  55. int main(int argc, char **argv)
  56. {
  57.     int x;
  58.     scanf("%d%d", &n, &x);
  59.     events.push_back({0, {0, -1}});
  60.     events.push_back({x, {0, 0}});
  61.     for (int i = 1; i <= n; ++i)
  62.         scanf("%d", &x), events.push_back({x, {i, 1}});
  63.     for (int i = 1; i <= n; ++i)
  64.         scanf("%d", &x), events.push_back({x, {i, 2}});
  65.  
  66.     sort(events.begin(), events.end());
  67.     int nn = events.size();
  68.     key_contains = vector<int>(nn, -1);
  69.     for (int i = 0; i < nn; ++i)
  70.         if (events[i].second.first == 0)
  71.             if (events[i].second.second == -1)
  72.                 ori = i;
  73.             else
  74.                 dest = i;
  75.         else if (events[i].second.second == 2)
  76.             key_contains[events[i].second.first] = i;
  77.  
  78.     dp = vector<vector<pll>>(nn + 1, vector<pll>(nn + 1, {LLONG_MAX, LLONG_MAX}));
  79.     dp[ori][ori] = {0, 0};
  80.  
  81.     search(0, nn - 1, 0);
  82.     search(0, nn - 1, 1);
  83.  
  84.     ll min_val = LLONG_MAX;
  85.     dbg(ori, dest);
  86.     if (ori < dest)
  87.         min_val = min(dp[ori][dest].first, dp[ori][dest].second), dbg(dp[ori][dest].first, dp[ori][dest].second);
  88.     else
  89.         min_val = min(dp[dest][ori].first, dp[dest][ori].second), dbg(dp[dest][ori].first, dp[dest][ori].second);
  90.  
  91.     printf("%lld\n", min_val >= LLONG_MAX / 2 ? -1ll : min_val);
  92.  
  93.     return 0;
  94. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement