当前位置: 代码迷 >> 综合 >> POJ 1862 Stripies(优先队列)
  详细解决方案

POJ 1862 Stripies(优先队列)

热度:49   发布时间:2024-01-18 18:33:21.0

http://poj.org/problem?id=1862

 

题目大意:

给定一组数,对任意两个数字进行2*sqrt(m1*m2)的操作,要求所得的所有数最小。

解题思路:

贪心,由于会一直开方,那么越大的数字开的次方数越多整体数字越小。

采用优先队列处理即可。

AC代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <stack>
#include <queue>
//#include <iomanip>
#include <map>
//#include <time.h>
using namespace std;
typedef long long ll;
inline int read() {int x = 0;int f = 1; char c = getchar();while(c<'0' || c>'9') {if(c=='-') f = -f; c = getchar();}while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}return x*f;
}
inline void write(int x) {if(!x)putchar('0');if(x<0)x=-x,putchar('-');static int sta[36];int tot = 0;while(x)sta[tot++]=x%10,x/=10;while(tot)putchar(sta[--tot]+48);
}
//mt19937 rnd(time(NULL));
const int inf = 0x3f3f3f3f;
const int maxn = 100 + 10;
int T;
int a[maxn];
priority_queue<double> q;
int main() {int n;while(~scanf("%d",&n)) {double tmp;for (int i = 0; i < n; i++) {cin >> tmp;q.push(tmp);}while (!q.empty()) {double p1 = q.top();q.pop();if (q.empty()) {printf("%.3lf\n", p1);break;}double p2 = q.top();q.pop();double p3 = 2 * sqrt(p1 * p2);q.push(p3);}}return 0;
}