2007年3月15日星期四

Standard Template Library

STL,真是太好用了。
平时应该用STL来训练,这样可以少做很多重复的事。
Code:
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main()
{
priority_queue<int,vector<int>,greater<int> > heap;
int n,fruit,sum=0,x,y;
cin>>n;
for (int i=0;i<n;i++)
{
cin>>fruit;
heap.push(fruit);
}
for (int i=0;i<n-1;i++)
{
x = heap.top();heap.pop();
y = heap.top();heap.pop();
sum += x+y;
heap.push(x+y);
}
cout<<sum<<endl;
}

没有评论: