码迷,mamicode.com
首页 > 其他好文 > 详细

PID25 / 合并果子 ☆

时间:2017-02-28 20:59:35      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:while   学习   out   nod   ng2   namespace   精通   html   names   

这里用到了STL里面的priority_queue,我也不是很精通基本上属于现学现卖阶段,http://www.cnblogs.com/flyoung2008/articles/2136485.html,这里挂一个我学习的地址

相当于一个堆,这里自定义了最小堆,这样之后每次取堆顶的两个元素就好,相加之后再插入堆

#include<iostream>
#include <queue>
#include<algorithm>
#include<cstring>
using namespace std;
int fruit[10000+10];
struct node{

  int  m;
  bool operator<(const node &a) const
  {
      return a.m<m;
  }

};
int main()
{
    int n;

    while(cin>>n)
    {
        priority_queue<node>Q;
        node p;
        for(int i=0;i<n;i++)
        {
            cin>>p.m;
            Q.push(p);
        }
        int sum=0;
        node fir,sec;
        node temp;
        for(int i=0;i<n-1;i++)
        {
            fir=Q.top();
            Q.pop();
            sec=Q.top();
            Q.pop();
            sum+=fir.m+sec.m;
            temp.m=fir.m+sec.m;
            Q.push(temp);
        }
        while(!Q.empty())
        {
            Q.pop();
        }
        cout<<sum<<endl;
    }
    return 0;
}

 

PID25 / 合并果子 ☆

标签:while   学习   out   nod   ng2   namespace   精通   html   names   

原文地址:http://www.cnblogs.com/lulichuan/p/6480765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!