Submission #330811

# Submission time Handle Problem Language Result Execution time Memory
330811 2020-11-26T16:12:40 Z two_sides Feast (NOI19_feast) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct block {
    ll val;
    block *prev, *next;

    block(ll val): val(val) {
        prev = next = nullptr;
    }
};

struct compare {
    bool operator ()
    (const block *&x, const block *&y) {
        return abs(x->val) != abs(y->val) ?
        abs(x->val) < abs(y->val) : x < y;
    }
};

const int N = 3e5 + 5;

set <block*, compare> blocks;
int a[N]; block *b[N];

bool sign(const int &x) {
    return x <= 0 ? 0 : 1;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, k, m = 0, p = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; cin >> a[i++]);
    for (int i = 1; i <= n; ) {
        int j = i; ll sum = 0;
        while (j <= n && sign(a[i])
        == sign(a[j])) sum += a[j++];
        if (sum > 0 || m) {
            b[m++] = new block(sum);
            blocks.insert(b[m - 1]);
        }
        i = j; p += sum > 0;
    }
    for (int i = 1; i < m; i++) {
        b[i]->prev = b[i - 1];
        b[i - 1]->next = b[i];
    }
    while (p > k && blocks.size()) {
        block *it = *blocks.begin();
        blocks.erase(it);
        if (it->val > 0) p--;
        if (it->prev) {
            blocks.erase(it->prev);
            if (it->prev->val > 0) p--;
            it->val += it->prev->val;
            it->prev = it->prev->prev;
        }
        if (it->next) {
            blocks.erase(it->next);
            if (it->next->val > 0) p--;
            it->val += it->next->val;
            it->next = it->next->next;
        }
        if (it->val > 0 ||
        (it->prev && it->next)) {
            if (it->val > 0) p++;
            blocks.insert(it);
        }
    }
    ll res = 0;
    for (auto it : blocks)
        res += max(0ll, it->val);
    cout << res << '\n';
}

Compilation message

In file included from /usr/include/c++/9/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
                 from feast.cpp:1:
/usr/include/c++/9/bits/stl_tree.h: In instantiation of 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = block*]':
/usr/include/c++/9/bits/stl_tree.h:2148:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = block* const&; _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>]'
/usr/include/c++/9/bits/stl_set.h:511:48:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = block*; _Compare = compare; _Alloc = std::allocator<block*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<block*>; std::set<_Key, _Compare, _Alloc>::value_type = block*]'
feast.cpp:44:35:   required from here
/usr/include/c++/9/bits/stl_tree.h:2095:11: error: no match for call to '(compare) (block* const&, block* const&)'
 2095 |    __comp = _M_impl._M_key_compare(__k, _S_key(__x));
feast.cpp:17:10: note: candidate: 'bool compare::operator()(const block*&, const block*&)' <near match>
   17 |     bool operator ()
      |          ^~~~~~~~
feast.cpp:17:10: note:   conversion of argument 2 would be ill-formed:
In file included from /usr/include/c++/9/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
                 from feast.cpp:1:
/usr/include/c++/9/bits/stl_tree.h:2095:11: error: cannot bind non-const lvalue reference of type 'const block*&' to an rvalue of type 'const block*'
 2095 |    __comp = _M_impl._M_key_compare(__k, _S_key(__x));
/usr/include/c++/9/bits/stl_tree.h:2106:7: error: no match for call to '(compare) (block* const&, block* const&)'
 2106 |       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
      |       ^~
feast.cpp:17:10: note: candidate: 'bool compare::operator()(const block*&, const block*&)' <near match>
   17 |     bool operator ()
      |          ^~~~~~~~
feast.cpp:17:10: note:   conversion of argument 2 would be ill-formed:
In file included from /usr/include/c++/9/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
                 from feast.cpp:1:
/usr/include/c++/9/bits/stl_tree.h:2106:7: error: cannot bind non-const lvalue reference of type 'const block*&' to an rvalue of type 'const block*'
 2106 |       if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
      |       ^~
/usr/include/c++/9/bits/stl_tree.h: In instantiation of 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr, _Arg&&, _NodeGen&) [with _Arg = block* const&; _NodeGen = std::_Rb_tree<block*, block*, std::_Identity<block*>, compare, std::allocator<block*> >::_Alloc_node; _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<block*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr = std::_Rb_tree_node_base*]':
/usr/include/c++/9/bits/stl_tree.h:2153:11:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = block* const&; _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>]'
/usr/include/c++/9/bits/stl_set.h:511:48:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = block*; _Compare = compare; _Alloc = std::allocator<block*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<block*>; std::set<_Key, _Compare, _Alloc>::value_type = block*]'
feast.cpp:44:35:   required from here
/usr/include/c++/9/bits/stl_tree.h:1807:10: error: no match for call to '(compare) (block* const&, block* const&)'
 1806 |  bool __insert_left = (__x != 0 || __p == _M_end()
      |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1807 |          || _M_impl._M_key_compare(_KeyOfValue()(__v),
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1808 |        _S_key(__p)));
      |        ~~~~~~~~~~~~~
feast.cpp:17:10: note: candidate: 'bool compare::operator()(const block*&, const block*&)' <near match>
   17 |     bool operator ()
      |          ^~~~~~~~
feast.cpp:17:10: note:   conversion of argument 2 would be ill-formed:
In file included from /usr/include/c++/9/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
                 from feast.cpp:1:
/usr/include/c++/9/bits/stl_tree.h:1807:10: error: cannot bind non-const lvalue reference of type 'const block*&' to an rvalue of type 'const block*'
 1806 |  bool __insert_left = (__x != 0 || __p == _M_end()
      |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1807 |          || _M_impl._M_key_compare(_KeyOfValue()(__v),
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1808 |        _S_key(__p)));
      |        ~~~~~~~~~~~~~
/usr/include/c++/9/bits/stl_tree.h: In instantiation of 'std::pair<std::_Rb_tree_iterator<_Val>, std::_Rb_tree_iterator<_Val> > std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::equal_range(const _Key&) [with _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>]':
/usr/include/c++/9/bits/stl_tree.h:2534:32:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::erase(const _Key&) [with _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type = long unsigned int]'
/usr/include/c++/9/bits/stl_set.h:685:30:   required from 'std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = block*; _Compare = compare; _Alloc = std::allocator<block*>; std::set<_Key, _Compare, _Alloc>::size_type = long unsigned int; std::set<_Key, _Compare, _Alloc>::key_type = block*]'
feast.cpp:54:24:   required from here
/usr/include/c++/9/bits/stl_tree.h:1997:4: error: no match for call to '(compare) (block* const&, block* const&)'
 1997 |    if (_M_impl._M_key_compare(_S_key(__x), __k))
      |    ^~
feast.cpp:17:10: note: candidate: 'bool compare::operator()(const block*&, const block*&)' <near match>
   17 |     bool operator ()
      |          ^~~~~~~~
feast.cpp:17:10: note:   conversion of argument 2 would be ill-formed:
In file included from /usr/include/c++/9/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
                 from feast.cpp:1:
/usr/include/c++/9/bits/stl_tree.h:1997:4: error: cannot bind non-const lvalue reference of type 'const block*&' to an rvalue of type 'const block*'
 1997 |    if (_M_impl._M_key_compare(_S_key(__x), __k))
      |    ^~
/usr/include/c++/9/bits/stl_tree.h:1999:9: error: no match for call to '(compare) (block* const&, block* const&)'
 1999 |    else if (_M_impl._M_key_compare(__k, _S_key(__x)))
      |         ^~
feast.cpp:17:10: note: candidate: 'bool compare::operator()(const block*&, const block*&)' <near match>
   17 |     bool operator ()
      |          ^~~~~~~~
feast.cpp:17:10: note:   conversion of argument 2 would be ill-formed:
In file included from /usr/include/c++/9/map:60,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
                 from feast.cpp:1:
/usr/include/c++/9/bits/stl_tree.h:1999:9: error: cannot bind non-const lvalue reference of type 'const block*&' to an rvalue of type 'const block*'
 1999 |    else if (_M_impl._M_key_compare(__k, _S_key(__x)))
      |         ^~
/usr/include/c++/9/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type) [with _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Const_Link_type = const std::_Rb_tree_node<block*>*]':
/usr/include/c++/9/bits/stl_tree.h:2095:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = block*]'
/usr/include/c++/9/bits/stl_tree.h:2148:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = block* const&; _Key = block*; _Val = block*; _KeyOfValue = std::_Identity<block*>; _Compare = compare; _Alloc = std::allocator<block*>]'
/usr/include/c++/9/bits/stl_set.h:511:48:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = block*; _Compare = compare; _Alloc = std::allocator<block*>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<block*>; std::set<_Key, _Compare, _Alloc>::value_type = block*]'
feast.cpp:44:35:   required from here
/usr/include/c++/9/bits/stl_tree.h:772:16: error: static assertion failed: comparison object must be invocable with two arguments of key type
  772 |  static_assert(__is_invocable<_Compare&, const _Key&, const _Key&>{},
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~