社区讨论
20分求调
P1801黑匣子参与者 3已保存回复 5
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 5 条
- 当前快照
- 1 份
- 快照标识符
- @mhjtgowd
- 此快照首次捕获于
- 2025/11/04 08:14 4 个月前
- 此快照最后确认于
- 2025/11/04 08:14 4 个月前
写的有点长
CPP#include<bits/stdc++.h>
#define FOR(a1,b1,c1) for(int a1=b1;a1<=c1;a1++)
#define For(a1,b1) for(;a1<=b1;a1++)
using namespace std;
template<typename _Tp, typename _Sequence = vector<_Tp>, typename _Compare = less<typename _Sequence::value_type> >
class _priority_queue {
#ifdef _GLIBCXX_CONCEPT_CHECKS
typedef typename _Sequence::value_type _Sequence_value_type;
# if __cplusplus < 201103L
__glibcxx_class_requires(_Tp, _SGIAssignableConcept)
# endif
__glibcxx_class_requires(_Sequence, _SequenceConcept) __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept) __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp, _BinaryFunctionConcept)
#endif
#if __cplusplus >= 201103L
template<typename _Alloc>using _Uses = typename enable_if<uses_allocator<_Sequence, _Alloc>::value>::type;
#if __cplusplus >= 201703L
static_assert(is_same<_Tp, typename _Sequence::value_type>::value, "value_type must be the same as the underlying container");
#endif
#endif
public:
typedef typename _Sequence::value_type value_type;
typedef typename _Sequence::reference reference;
typedef typename _Sequence::const_reference const_reference;
typedef typename _Sequence::size_type size_type;
typedef _Sequence container_type;
typedef _Compare value_compare;
protected:
_Sequence c;
_Compare comp;
public:
#if __cplusplus < 201103L
explicit _priority_queue(const _Compare& __x = _Compare(), const _Sequence& __s = _Sequence()): c(__s), comp(__x) {
std::make_heap(c.begin(), c.end(), comp);
}
#else
template<typename _Seq = _Sequence, typename _Requires = typename enable_if<__and_<is_default_constructible<_Compare>, is_default_constructible<_Seq>>::value>::type>_priority_queue(): c(), comp() { } explicit _priority_queue(const _Compare& __x, const _Sequence& __s): c(__s), comp(__x) {
std::make_heap(c.begin(), c.end(), comp);
} explicit _priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence()): c(std::move(__s)), comp(__x) {
std::make_heap(c.begin(), c.end(), comp);
} template<typename _Alloc, typename _Requires = _Uses<_Alloc>>explicit _priority_queue(const _Alloc& __a): c(__a), comp() { } template<typename _Alloc, typename _Requires = _Uses<_Alloc>>_priority_queue(const _Compare& __x, const _Alloc& __a): c(__a), comp(__x) { } template<typename _Alloc, typename _Requires = _Uses<_Alloc>>_priority_queue(const _Compare& __x, const _Sequence& __c, const _Alloc& __a): c(__c, __a), comp(__x) {
std::make_heap(c.begin(), c.end(), comp);
} template<typename _Alloc, typename _Requires = _Uses<_Alloc>>_priority_queue(const _Compare& __x, _Sequence && __c, const _Alloc& __a): c(std::move(__c), __a), comp(__x) {
std::make_heap(c.begin(), c.end(), comp);
} template<typename _Alloc, typename _Requires = _Uses<_Alloc>>_priority_queue(const _priority_queue& __q, const _Alloc& __a): c(__q.c, __a), comp(__q.comp) { } template<typename _Alloc, typename _Requires = _Uses<_Alloc>>_priority_queue(_priority_queue && __q, const _Alloc& __a): c(std::move(__q.c), __a), comp(std::move(__q.comp)) { }
#endif
#if __cplusplus < 201103L
template<typename _InputIterator>_priority_queue(_InputIterator __first, _InputIterator __last, const _Compare& __x = _Compare(), const _Sequence& __s = _Sequence()): c(__s), comp(__x) {
__glibcxx_requires_valid_range(__first, __last);
c.insert(c.end(), __first, __last);
std::make_heap(c.begin(), c.end(), comp);
}
#else
template<typename _InputIterator>_priority_queue(_InputIterator __first, _InputIterator __last, const _Compare& __x, const _Sequence& __s): c(__s), comp(__x) {
__glibcxx_requires_valid_range(__first, __last);
c.insert(c.end(), __first, __last);
std::make_heap(c.begin(), c.end(), comp);
} template<typename _InputIterator>_priority_queue(_InputIterator __first, _InputIterator __last, const _Compare& __x = _Compare(), _Sequence && __s = _Sequence()): c(std::move(__s)), comp(__x) {
__glibcxx_requires_valid_range(__first, __last);
c.insert(c.end(), __first, __last);
std::make_heap(c.begin(), c.end(), comp);
}
#endif
_GLIBCXX_NODISCARD bool empty() const {
return c.empty();
} size_type size() const {
return c.size();
} const_reference top() const {
__glibcxx_requires_nonempty();
return c.front();
} const_reference get(int n) const {
__glibcxx_requires_nonempty();
return c[n];
} void push(const value_type& __x) {
c.push_back(__x);
std::push_heap(c.begin(), c.end(), comp);
}
#if __cplusplus >= 201103L
void push(value_type&& __x) {
c.push_back(std::move(__x));
std::push_heap(c.begin(), c.end(), comp);
} template<typename... _Args>void emplace(_Args&&... __args) {
c.emplace_back(std::forward<_Args>(__args)...);
std::push_heap(c.begin(), c.end(), comp);
}
#endif
void pop() {
__glibcxx_requires_nonempty();
std::pop_heap(c.begin(), c.end(), comp);
c.pop_back();
}
#if __cplusplus >= 201103L
void swap(_priority_queue& __pq)noexcept(__and_ <
#if __cplusplus > 201402L || !defined(__STRICT_ANSI__)
__is_nothrow_swappable<_Sequence>,
#else
__is_nothrow_swappable<_Tp>,
#endif
__is_nothrow_swappable<_Compare >>::value) {
using std::swap;
swap(c, __pq.c);
swap(comp, __pq.comp);
}
#endif
};
_priority_queue<int, vector<int>, greater<int> >a;
int m, n, i = 0, x[200001], k, p =1;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> m >> n;
for (int j = 1; j <= m; j++)
cin >> x[j];
FOR(j, 1, n)
{
cin >> k;
for(;p<=k;p++)
a.push(x[p]);
cout << a.get(i) << endl;
i++;
}
return 0;
}
回复
共 5 条回复,欢迎继续交流。
正在加载回复...