Stripies POJ - 1862 (贪心+优先队列,注意输出)

深碍√TFBOYSˉ_ 2022-03-07 00:56 329阅读 0赞

Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name to apply for an international patent). The stripies are transparent amorphous amebiform creatures that live in flat colonies in a jelly-like nutrient medium. Most of the time the stripies are moving. When two of them collide a new stripie appears instead of them. Long observations made by our scientists enabled them to establish that the weight of the new stripie isn’t equal to the sum of weights of two disappeared stripies that collided; nevertheless, they soon learned that when two stripies of weights m1 and m2 collide the weight of resulting stripie equals to 2*sqrt(m1*m2). Our chemical biologists are very anxious to know to what limits can decrease the total weight of a given colony of stripies.
You are to write a program that will help them to answer this question. You may assume that 3 or more stipies never collide together.

Input

The first line of the input contains one integer N (1 <= N <= 100) - the number of stripies in a colony. Each of next N lines contains one integer ranging from 1 to 10000 - the weight of the corresponding stripie.

Output

The output must contain one line with the minimal possible total weight of colony with the accuracy of three decimal digits after the point.

Sample Input

  1. 3
  2. 72
  3. 30
  4. 50

Sample Output

  1. 120.000

解题思路:

利用优先队列,每次挑选weight最大的两只strights合并,这样能量损耗就最小

注意,double读入时用%lf,输出时用%f,我因为这个点卡了好久

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <vector>
  5. #include <math.h>
  6. #include <stdio.h>
  7. using namespace std;
  8. int N;
  9. int main() {
  10. while (~scanf("%d", &N)) {
  11. priority_queue<double> inputQueue;
  12. double tweight;
  13. for (int i = 0; i < N; ++i) {
  14. scanf("%lf", &tweight);
  15. inputQueue.push(tweight);
  16. }
  17. //开始遍历操作
  18. while (inputQueue.size() > 1) {
  19. double temp1 = inputQueue.top();
  20. inputQueue.pop();
  21. double temp2 = inputQueue.top();
  22. inputQueue.pop();
  23. inputQueue.push(2*sqrt(temp1*temp2));
  24. }
  25. double res = inputQueue.top();
  26. printf("%.3f\n", res); //注意,double输出时为%f
  27. }
  28. system("PAUSE");
  29. return 0;
  30. }

发表评论

表情:
评论列表 (有 0 条评论,329人围观)

还没有评论,来说两句吧...

相关阅读

    相关 51nod 1191(贪心+优先队列)

    有N只兔子,每只有一个血量B\[i\],需要用箭杀死免子。有M种不同类型的箭可以选择,每种箭对兔子的伤害值分别为D\[i\],价格为P\[i\](1 <= i <= M)。假设