原题链接: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. RC振荡器与有源滤波器1

    自我总结,有错误欢迎指出! 一.振荡器原理: 对于正反馈,有如下公式: 讨论: 1.当A*F(jw)=1时 Af→∞(物理意义:自激振荡): 2.在f0上,F(jw)相移为0或2n∏:(因此,在f0处 ...

  2. TCP/IP详解学习笔记(2)-- 数据链路层

    1.概述      数据链路层属于计算机网络的底层,使用的信道主要有点对点信道和广播信道两种类型.      在TCP/IP协议族中,数据链路层主要有三个目的:           1)为IP模块发送 ...

  3. TCP/IP详解学习笔记(15)-- TCP的流量控制和拥塞控制

    TCP的流量控制 1.概述      所谓的流量控制就是让发送方的发送速率不要太快,让接收方来得及接受.利用滑动窗口机制可以很方便的在TCP连接上实现对发送方的流量控制.TCP的窗口单位是字节,不是报 ...

  4. 关于cookie

    CookieHelper.WriteCookie("DEPID", "theway", depid);   //先写入cookie //再读取cookie Us ...

  5. MULTIBYTETOWIDECHAR的与WIDECHARTOMULTIBYTE的参数详解及相互转换

    第一个就是宽字符到多字节字符转换函数,函数原型如下: int WideCharToMultiByte( UINT CodePage, DWORD dwFlags, LPCWSTR lpWideChar ...

  6. select,poll,epoll的归纳总结区分

    Select.Poll与Epoll比较 以下资料都是来自网上搜集整理.引用源详见文章末尾. 1 Select.Poll与Epoll简介 Select select本质上是通过设置或者检查存放fd标志位 ...

  7. pig中变量

    pig中的变量都是找到$变量然后替换,有点像宏,完全就是替换,看如下例子 %default m 'you';b = load 'a' as (a:chararray);c = foreach b ge ...

  8. Android IOS WebRTC 音视频开发总结(四六)-- 从另一个角度看国内首届WebRTC大会

    文章主要从开发者角度谈国内首届WebRTC大会,支持原创,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处,更多详见www.rtc.help. -------------------- ...

  9. IIS URL重写找不到页面 (URLRewriter.dll伪静态)

    在网站上点右键 属性 进入主目录菜单 点击配置 找到.html扩展名 编辑 将 检查文件是否存在 的钩去掉! OK

  10. 彻底解决_OBJC_CLASS_$_某文件名", referenced from:问题转

    最近在使用静态库时,总是出现这个问题.下面总结一下我得解决方法: 1. .m文件没有导入   在Build Phases里的Compile Sources 中添加报错的文件 2. .framework ...