您的位置:首页 > 其它

poj 1862 还是哈夫曼思想贪心

2015-02-19 22:32 465 查看
题意:科学家发现一种奇怪的玩意,他们有重量Wi,

如果他们碰在一起,总重变成2*sqrt(t1*t2)。

要求出最终的重量的最小值。

思路:果然A题多点经验就用上了,跟农夫锯木头那个一个思路,哈夫曼思想,把所有重量放堆里,每次取俩最小的处理再放堆里,最后堆剩下的最后元素就是答案了

代码:

#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
//题目大意:科学家发现一种奇怪的玩意,他们有重量weight,
//如果他们碰在一起,总重变成2*sqrt(m1*m2)。
//要求出最终的重量的最小值。
int n;
double a[2000];
void input()
{
for(int i = 0 ; i < n ; i++)
cin >> a[i];
}
void solve()
{
double ans = 0.000;
priority_queue <double , vector<double> , less<double> > q;
for(int i = 0 ; i < n ; i++)
q.push(a[i]);

while( q.size() != 1)
{
double t = q.top();
q.pop();
double t1 = q.top();
q.pop();
q.push( 2*sqrt(t*t1) );
}
cout.setf( ios::fixed );
cout.precision(3);<span style="white-space:pre">	</span>
cout << q.top() << endl;<span style="white-space:pre">	</span>//cout输出用了47MS
//        ans = q.top();
//        printf("%.3lf\n",ans);<span style="white-space:pre">	</span>//printf 0MS无压力
}<span style="white-space:pre">					</span>//所以说能用C的还是用C的吧,这次当练cout格式化输出了
int main()
{
#ifdef H_R
freopen("in.txt","r",stdin);
#endif // H_R
cin.tie(false);
ios::sync_with_stdio(false);
while(cin >> n)
{
input();
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: