您的位置:首页 > 其它

洛谷P1090 合并果子 (优先队列伪STL)

2016-07-08 16:49 381 查看
这个是用优先队列做的,自己没事干弄了一个STL(其实就是从头文件里面拷贝的),可以通过编译。

#include <iostream>
#include <cstdio>
#include <bits/c++config.h>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/detail/priority_queue_base_dispatch.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#ifndef PB_DS_PRIORITY_QUEUE_HPP
#define PB_DS_PRIORITY_QUEUE_HPP
namespace __gnu_pbds{
template<typename _Tv,
typename Cmp_Fn = std::less<_Tv>,
typename Tag = pairing_heap_tag,
typename _Alloc = std::allocator<char> >
class priority_queue
: public detail::container_base_dispatch<_Tv, Cmp_Fn, _Alloc, Tag>::type{
public:
typedef _Tv 					value_type;
typedef Cmp_Fn 					cmp_fn;
typedef Tag 					container_category;
typedef _Alloc 					allocator_type;
typedef typename allocator_type::size_type 		size_type;
typedef typename allocator_type::difference_type 	difference_type;
private:
typedef typename detail::container_base_dispatch<_Tv, Cmp_Fn, _Alloc,
Tag>::type
base_type;
typedef typename _Alloc::template rebind<_Tv>   	__rebind_v;
typedef typename __rebind_v::other			__rebind_va;
public:
typedef typename __rebind_va::reference 		reference;
typedef typename __rebind_va::const_reference 	const_reference;
typedef typename __rebind_va::pointer 	   	pointer;
typedef typename __rebind_va::const_pointer 	const_pointer;
typedef typename base_type::point_iterator 		point_iterator;
typedef typename base_type::point_const_iterator 	point_const_iterator;
typedef typename base_type::iterator 		iterator;
typedef typename base_type::const_iterator 		const_iterator;
priority_queue() { }
priority_queue(const cmp_fn& r_cmp_fn) : base_type(r_cmp_fn) { }
template<typename It>
priority_queue(It first_it, It last_it)
{ base_type::copy_from_range(first_it, last_it); }
template<typename It>
priority_queue(It first_it, It last_it, const cmp_fn& r_cmp_fn)
: base_type(r_cmp_fn)
{ base_type::copy_from_range(first_it, last_it); }
priority_queue(const priority_queue& other)
: base_type((const base_type& )other) { }
virtual
~priority_queue() { }
priority_queue&
operator=(const priority_queue& other){
if(this != &other){
priority_queue tmp(other);
swap(tmp);
}
return *this;
}
void
swap(priority_queue& other)
{ base_type::swap(other); }
};
}
#endif
using namespace std;
priority_queue<int,vector<int>,greater<int> > PriQueue;
int Input,sum,tot,num;
void Push_In();
int main(){
Push_In();
while(PriQueue.empty()==0){
tot+=PriQueue.top();
PriQueue.pop();num++;
if(num%2==0){
sum+=tot;
PriQueue.push(tot);
tot=0;
}
}
cout<<sum;
return 0;
}
void Push_In(){
int Nums;
cin>>Input;
for(int i=1;i<=Input;i++){
cin>>Nums;
PriQueue.push(Nums);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  洛谷