poj 3253 Fence Repair (优先队列)

2015-01-27 22:35:48 ? 作者: ? 浏览: 48
///优先队列,每次取两个最小的木板的长度,再把这个长度放进队列,反复取
# include 
  
   
# include 
   
     # include 
    
      # include 
     
       # include 
      
        # include 
       
         using namespace std; ///长度小的放在前面 class cmp { public: bool operator ()(const __int64 a,const __int64 b)const { return a>b; } }; int main() { int n; __int64 temp,sum,p1,p2; while(~scanf("%d",&n)) { priority_queue<__int64,vector<__int64>,cmp>q;///定义优先队列 while(n--) { scanf("%I64d",&temp); q.push(temp); } sum=0; while(q.size()>1) { p1=q.top(); q.pop(); p2=q.top(); q.pop(); temp=p1+p2; sum+=temp; q.push(temp); } printf("%I64d\n",sum); } return 0; }