Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&... values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using pll = pair<ll, ll>;
- using piii = pair<int, pii>;
- int n;
- vector<piii> events;
- vector<vector<pll>> dp;
- vector<int> key_contains;
- int ori = 0;
- int dest = 0;
- ll search(int beg, int end, int dir)
- {
- if (dir == 0 && dp[beg][end].first < LLONG_MAX)
- return dp[beg][end].first;
- if (dir == 1 && dp[beg][end].second < LLONG_MAX)
- return dp[beg][end].second;
- if (beg > end)
- return LLONG_MAX / 2;
- ll ans = LLONG_MAX / 2;
- if (dir) {
- int wtype = events[end].second.first;
- if ((events[end].second.second == 1 && beg <= key_contains[wtype] && end - 1 >= key_contains[wtype]) ||
- (events[end].second.second == 0 || events[end].second.second == 2 || events[end].second.second == -1)) {
- ans = min(search(beg, end - 1, 0) + events[end].first - events[beg].first,
- search(beg, end - 1, 1) + events[end].first - events[end - 1].first);
- }
- return dp[beg][end].second = ans;
- } else {
- int wtype = events[beg].second.first;
- if ((events[beg].second.second == 1 && beg + 1 <= key_contains[wtype] && end >= key_contains[wtype]) ||
- (events[beg].second.second == 0 || events[beg].second.second == 2 || events[beg].second.second == -1)) {
- ans = min(search(beg + 1, end, 0) + events[beg + 1].first - events[beg].first,
- search(beg + 1, end, 1) + events[end].first - events[beg].first);
- }
- return dp[beg][end].first = ans;
- }
- return ans;
- }
- int main(int argc, char **argv)
- {
- int x;
- scanf("%d%d", &n, &x);
- events.push_back({0, {0, -1}});
- events.push_back({x, {0, 0}});
- for (int i = 1; i <= n; ++i)
- scanf("%d", &x), events.push_back({x, {i, 1}});
- for (int i = 1; i <= n; ++i)
- scanf("%d", &x), events.push_back({x, {i, 2}});
- sort(events.begin(), events.end());
- int nn = events.size();
- key_contains = vector<int>(nn, -1);
- for (int i = 0; i < nn; ++i)
- if (events[i].second.first == 0)
- if (events[i].second.second == -1)
- ori = i;
- else
- dest = i;
- else if (events[i].second.second == 2)
- key_contains[events[i].second.first] = i;
- dp = vector<vector<pll>>(nn + 1, vector<pll>(nn + 1, {LLONG_MAX, LLONG_MAX}));
- dp[ori][ori] = {0, 0};
- search(0, nn - 1, 0);
- search(0, nn - 1, 1);
- ll min_val = LLONG_MAX;
- dbg(ori, dest);
- if (ori < dest)
- min_val = min(dp[ori][dest].first, dp[ori][dest].second), dbg(dp[ori][dest].first, dp[ori][dest].second);
- else
- min_val = min(dp[dest][ori].first, dp[dest][ori].second), dbg(dp[dest][ori].first, dp[dest][ori].second);
- printf("%lld\n", min_val >= LLONG_MAX / 2 ? -1ll : min_val);
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement