poj 1862 还是哈夫曼思想贪心
2015-02-19 22:32
465 查看
题意:科学家发现一种奇怪的玩意,他们有重量Wi,
如果他们碰在一起,总重变成2*sqrt(t1*t2)。
要求出最终的重量的最小值。
思路:果然A题多点经验就用上了,跟农夫锯木头那个一个思路,哈夫曼思想,把所有重量放堆里,每次取俩最小的处理再放堆里,最后堆剩下的最后元素就是答案了
代码:
如果他们碰在一起,总重变成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; }
相关文章推荐
- 贪心+优先队列(哈夫曼思想)POJ 3253 Fence Repair
- Stripies(POJ 1862 贪心)
- (贪心5.2.5)POJ 1862 Stripies(利用数据有序化来进行贪心选择)
- poj 3253 类似霍夫曼贪心思想,优先队列的运用
- POJ 1018 贪心思想
- poj1862~Stripies~贪心+优先队列
- 9 POJ 1862 Stripies 简单贪心
- poj 3040 深入理解贪心思想
- Stripies(POJ 1862, 贪心)
- 贪心 POJ 1862
- POJ 2057 贪心思想在动态规划中的应用
- POJ 1862 Stripies 贪心
- POJ 1862 Stripies#贪心(水)
- poj 3253 Fence Repair (贪心+哈弗曼思想+优先队列)
- poj 1862 Stripies 优先队列 贪心
- poj 2057 The Lost House 贪心思想在动态规划上的应用
- POJ 1862 Stripies(哈夫曼)
- poj 3253 类似霍夫曼贪心思想,优先队列的运用
- poj1426 Find The Multiple(哈夫曼思想)
- poj 1862 Stripies简单贪心