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

这个是用优先队列做的,自己没事干弄了一个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);
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值