Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这么多人贴代码。。那我也贴一个吧。。。STL 16ms。。#include <iostream> #include <queue> using namespace std; priority_queue<int,vector<int>,greater<vector<int>::value_type>> for_huff_tree; int main() { int length,N; __int64 ans=0,sum_length=0; scanf("%d",&N); for(int i=0;i<N;i++) { scanf("%d",&length); for_huff_tree.push(length); sum_length+=length; } int temp1,temp2; for(int i=1;i<N;i++) { temp1=for_huff_tree.top(); for_huff_tree.pop(); temp2=for_huff_tree.top(); for_huff_tree.pop(); ans+=temp1+temp2; for_huff_tree.push(temp1+temp2); } ans+=for_huff_tree.top(); printf("%I64d\n",ans-sum_length); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator