原题链接:http://poj.org/problem?id=1862

简单题,贪心+优先队列主要练习一下stl大根堆

写了几种实现方式写成类的形式还是要慢一些。。。

手打的heap:

1:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
class Solution{
public:
static const int Max_N = ;
int sz;
double heap[Max_N];
inline void init(){
sz = ;
}
inline void push(double x){
int i = sz++;
while (i > ){
int p = (i - ) >> ;
if (heap[p] >= x) break;
heap[i] = heap[p];
i = p;
}
heap[i] = x;
}
inline double pop(){
double ret = heap[], x = heap[--sz];
int i = ;
while ((i << ) + < sz){
int a = (i << ) + , b = (i << ) + ;
if (b < sz && heap[a] <= heap[b]) a = b;
if (heap[a] <= x) break;
heap[i] = heap[a];
i = a;
}
heap[i] = x;
return ret;
}
};
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
double a, b;
Solution ans;
ans.init();
scanf("%d", &n);
while (n--){
scanf("%lf", &a);
ans.push(a);
}
while (ans.sz > ){
a = ans.pop(), b = ans.pop();
ans.push( * sqrt(a * b));
}
printf("%.3lf", ans.pop());
return ;
}

2:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
struct Node{
static const int Max_N = ;
int sz;
double heap[Max_N];
Node() :sz(){}
inline void push(double x){
int i = sz++;
while (i > ){
int p = (i - ) >> ;
if (heap[p] >= x) break;
heap[i] = heap[p];
i = p;
}
heap[i] = x;
}
inline double pop(){
double ret = heap[], x = heap[--sz];
int i = ;
while ((i << ) + < sz){
int a = (i << ) + , b = (i << ) + ;
if (b < sz && heap[a] <= heap[b]) a = b;
if (heap[a] <= x) break;
heap[i] = heap[a];
i = a;
}
heap[i] = x;
return ret;
}
}ans;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
double a, b;
scanf("%d", &n);
while (n--){
scanf("%lf", &a);
ans.push(a);
}
while (ans.sz > ){
a = ans.pop(), b = ans.pop();
ans.push( * sqrt(a * b));
}
printf("%.3lf", ans.pop());
return ;
}

3:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
const int Max_N = ;
double heap[Max_N];
int sz = ;
void push(double x){
int i = sz++;
while (i > ){
int p = (i - ) >> ;
if (heap[p] >= x) break;
heap[i] = heap[p];
i = p;
}
heap[i] = x;
}
double pop(){
double ret = heap[], x = heap[--sz];
int i = ;
while ((i << ) + < sz){
int a = (i << ) + , b = (i << ) + ;
if (b < sz && heap[a] <= heap[b]) a = b;
if (heap[a] <= x) break;
heap[i] = heap[a];
i = a;
}
heap[i] = x;
return ret;
}
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
double a, b; scanf("%d", &n);
while (n--){
scanf("%lf", &a);
push(a);
}
while (sz > ){
a = pop(), b = pop();
push( * sqrt(a * b));
}
printf("%.3lf", pop());
return ;
}

4:

std::priority_queue<double> ans
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<iostream>
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
double a, b;
scanf("%d", &n);
std::priority_queue<double> ans;
while (n--){
scanf("%lf", &a);
ans.push(a);
}
while (ans.size() > ){
a = ans.top(), ans.pop();
b = ans.top(), ans.pop();
ans.push( * sqrt(a * b));
}
printf("%.3lf", ans.top());
return ;
}

poj 1862 Stripies/优先队列的更多相关文章

  1. POJ 1862 Stripies 贪心+优先队列

    http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成 ...

  2. POJ 1862 Stripies【哈夫曼/贪心/优先队列】

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18198   Accepted: 8175 Descrip ...

  3. POJ 1862 Stripies 【优先队列】

    题意:科学家发现一种奇怪的东西,他们有重量weight,如果他们碰在一起,总重变成2*sqrt(m1*m2).要求出最终的重量的最小值. 思路:每次选取质量m最大的两个stripy进行碰撞结合,能够得 ...

  4. POJ 1862 Stripies (哈夫曼树)

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10263   Accepted: 4971 Descrip ...

  5. STL 训练 POJ - 1862 Stripies

    Description Our chemical biologists have invented a new very useful form of life called stripies (in ...

  6. POJ 1862 Stripies#贪心(水)

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cmath> #include<algorithm ...

  7. POJ 1862 Stripies

    每次合并最大的两个,优先级队列维护一下. 输出的时候%.3lf G++会WA,C++能AC,改成%.3f,都能AC. #include<cstdio> #include<cstrin ...

  8. poj 3431 Expedition 优先队列

    poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...

  9. POJ 1862 &amp; ZOJ 1543 Stripies(贪心 | 优先队列)

    题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

随机推荐

  1. 慕课网-安卓工程师初养成-2-2 认识Java标识符

    来源:http://www.imooc.com/code/1177 问:标识符是神马? 答:标识符就是用于给 Java 程序中变量.类.方法等命名的符号. 使用标识符时,需要遵守几条规则: 1.  标 ...

  2. 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 超时怎么办? ...

  3. ViewPager撤消左右滑动切换功能

    ViewPager取消左右滑动切换功能 最近做项目要求某种情况下ViewPager不能滑动,那么我们只需要重写这个方法就可以禁止ViewPager滑动 IndexViewPager.java: imp ...

  4. ionic ngcordova map 地圖

    幾乎每個APP都會有地圖 所以在這裏記錄一下 1.在index.html 中 <script src="https://maps.googleapis.com/maps/api/js? ...

  5. The difference between macro and function I/Ofunction comparision(from c and pointer )

    macro is typeless and execute faster than funtion ,becaus of the overhead of calling and returnning ...

  6. ajax success 和complete 的区别

    Function) success - 当请求成功时调用的函数.这个函数会得到一个参数:从服务器返回的数据(根据“dataType”进行了格式化). Function) complete - 当请求完 ...

  7. Windows phone 8 学习笔记(4) 应用的启动(转)

    Windows phone 8 的应用除了可以直接从开始菜单以及应用列表中打开外,还可以通过其他的方式打开.照片中心.音乐+视频中心提供扩展支持应用从此启动.另外,我们还可以通过文件关联.URI关联的 ...

  8. linux 文本处理

    tr,awk,sed 一:tr 1.大小写转换 cat file | tr [a-z] [A-Z] > new_file(大写 --> 小写) cat file | tr [A-Z] [a ...

  9. 会"说话"的勒索病毒Cerber

    最近有个案子与勒索病毒有关,证物是个台式机,运行Windows 7 64bit操作系统,委托方是某高科技公司,希望能调查出事发的关键时间点.感染来源及途径.恶意程序文件名等相关信息. 在对证物计算机进 ...

  10. CentOS学习笔记—软件管理程序RPM、YUM

    软件管理程序 Linux的软件安装分为源代码编译安装和打包安装.RPM是一种打包安装方式,是由 Red Hat 这家公司开发出来的,后来实在很好用,因此很多 distributions 就使用这个机制 ...