QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#612685#9440. Train Seatsucup-team3646#WA 1ms3844kbC++204.7kb2024-10-05 12:34:362024-10-05 12:34:39

Judging History

This is the latest submission verdict.

  • [2024-10-05 12:34:39]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3844kb
  • [2024-10-05 12:34:36]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <typename T>
class LeftistHeap {
public:
    LeftistHeap() = default;

    static LeftistHeap meld(LeftistHeap a, LeftistHeap b) {
        return LeftistHeap(meld(std::move(a.root), std::move(b.root)));
    }

    std::pair<int, T> top() const {
        push(root);
        return {root->id, root->val};
    }

    void pop() {
        push(root);
        root = meld(std::move(root->left), std::move(root->right));
    }

    void push(int id, T x) {
        root = meld(std::move(root), std::make_unique<Node>(id, x));
    }

    bool empty() const { return root == nullptr; }

    void add(T x) { root->lazy += x; }

private:
    struct Node;
    using node_ptr = std::unique_ptr<Node>;

    struct Node {
        node_ptr left, right;
        int s;
        int id;
        T val, lazy;
        Node(int id, T x) : id(id), val(x), lazy(0) {}
    };

    node_ptr root = nullptr;

    explicit LeftistHeap(node_ptr root) : root(std::move(root)) {}

    static node_ptr meld(node_ptr a, node_ptr b) {
        if (!a) return b;
        if (!b) return a;
        push(a);
        push(b);
        if (a->val > b->val) std::swap(a, b);
        a->right = meld(std::move(a->right), std::move(b));
        if (!a->left || a->left->s < a->right->s) std::swap(a->left, a->right);
        a->s = (a->right ? a->right->s : 0) + 1;
        return a;
    }

    static void push(const node_ptr& t) {
        if (t->left) t->left->lazy += t->lazy;
        if (t->right) t->right->lazy += t->lazy;
        t->val += t->lazy;
        t->lazy = 0;
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < (int) v.size(); ++i) {
        os << v[i];
        if (i < (int) v.size() - 1) os << " ";
    }
    return os;
}

template <typename T>
T hu_tucker(std::vector<T> vs) {
    const int n = vs.size();
    const ll INF = std::numeric_limits<T>::max() / 2;
    std::vector<LeftistHeap<T>> heaps(n - 1);  // interior nodes in each group
    std::vector<int> left(n), right(n);
    std::vector<T> cs(n - 1);
    using P = std::pair<T, int>;
    std::priority_queue<P, vector<P>, greater<P>> pq;
    for (int i = 0; i < n - 1; ++i) {
        left[i] = i - 1;
        right[i] = i + 1;
        cs[i] = vs[i] + vs[i + 1];
        pq.emplace(cs[i], i);
    }
    T ret = 0;
    for (int k = 0; k < n - 1; ++k) {
        T c;
        int i;
        // find the optimal nodes to merge
        do {
            tie(c, i) = pq.top();
            pq.pop();
        } while (right[i] == -1 || cs[i] != c);

        bool merge_l = false, merge_r = false;
        if (vs[i] + vs[right[i]] == c) {  // lr
            merge_l = merge_r = true;
        } else {
            T top = heaps[i].top().second;
            heaps[i].pop();
            if (vs[i] + top == c) {  // lm
                merge_l = true;
            } else if (top + vs[right[i]] == c) {  // mr
                merge_r = true;
            } else {  // mm
                heaps[i].pop();
            }
        }
        ret += c;
        heaps[i].push(-1, c);
        if (merge_l) {
            vs[i] = INF;
        }
        if (merge_r) {
            vs[right[i]] = INF;
        }

        // merge left
        if (merge_l && i > 0) {
            int j = left[i];
            heaps[j] = LeftistHeap<T>::meld(std::move(heaps[j]), std::move(heaps[i]));
            right[j] = right[i];
            right[i] = -1;
            left[right[j]] = j;
            i = j;
        }
        // merge right
        if (merge_r && right[i] < n - 1) {
            int j = right[i];
            heaps[i] = LeftistHeap<T>::meld(std::move(heaps[i]), std::move(heaps[j]));
            right[i] = right[j];
            right[j] = -1;
            left[right[i]] = i;
        }

        cs[i] = vs[i] + vs[right[i]];  // lr
        if (!heaps[i].empty()) {
            T top = heaps[i].top().second;
            heaps[i].pop();
            cs[i] = std::min(cs[i], std::min(vs[i], vs[right[i]]) + top);  // lm or mr
            if (!heaps[i].empty()) {
                cs[i] = std::min(cs[i], top + heaps[i].top().second);  // mm
            }
            heaps[i].push(-1, top);
        }
        pq.emplace(cs[i], i);
    }
    return ret;
}

int main() {
  ll N, M; cin >> N >> M;
  vector<ll> A(N);
  for (auto &a : A) cin >> a;
  A.emplace_back(0);
  A.emplace_back(M+1);

  sort(A.begin(), A.end());
  
  vector<ll> B;
  for (int i = 1; i < A.size(); ++i) {
    B.emplace_back(- (A[i] - A[i-1]));
  }

  ll res = -hu_tucker(B);
  cout << res << endl;

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3832kb

input:

3 10
3 7 10

output:

28

result:

ok "28"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

5 20
3 10 11 14 17

output:

73

result:

ok "73"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

10 1000000000
136909656 243332691 643531997 505919307 43553384 657638276 57213246 179732866 357373203 182482400

output:

7649951260

result:

ok "7649951260"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

1 172024959
118390965

output:

172024960

result:

ok "172024960"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

2 920065252
24963998 580538879

output:

1815166508

result:

ok "1815166508"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

3 172078788
90217996 89200357 170380183

output:

432676968

result:

ok "432676968"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

4 440425711
125318960 91140311 293637977 102491554

output:

1442752023

result:

ok "1442752023"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3500kb

input:

5 322827483
22471802 47973794 3707067 27640905 273307033

output:

1512343852

result:

ok "1512343852"

Test #9:

score: -100
Wrong Answer
time: 0ms
memory: 3844kb

input:

72 630313504
112329946 338670434 45608233 444381955 513206139 543955969 420916952 485920423 598657953 568525637 92887514 375155749 230002387 302266710 539300386 433464422 380969627 445990156 239073197 278937451 50602251 494375406 139348725 11780176 601670777 68418714 591190755 96719555 612628609 778...

output:

24892787890

result:

wrong answer 1st words differ - expected: '25015466409', found: '24892787890'