原题链接: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. 学习练习 java面向对象存取款查询余额

    package com.hanqi; public class Account { String ZhangHao; double CunKuanYuE; Account(String ZhangHa ...

  2. hadoop2.5.1搭建(一)

    1.1配置 1.1.1修改hosts vi /etc/hosts 192.168.220.64 cluster4 192.168.220.63 cluster3 1.2安装jdk rpm安装 rpm ...

  3. sql实现分页

    IF EXISTS(SELECT * FROM sysobjects WHERE name='usp_getPage') DROP PROC usp_getPage GO CREATE PROC us ...

  4. java基础回顾(一)—— sleep和wait的区别

    sleep是Thread类的一个方法,wait是Object类的一个方法 sleep是线程被调用时,占着cpu去睡觉,其他线程不能占用cpu,os认为该线程正在工作,不会让出系统资源 wait是进入等 ...

  5. STM32F05 学习中............

    今天,拿到stm32f05的板子已经三个月了吧,但是没有真的研究过,真的对板子过意不去了...所以决定今天好好的对待我的板子.

  6. 开源项目:X265

    1 Windows下编译X265 具体的编译环境: Windows 7(64bit) + Visual Stdio 2010 + Cmake 3.4.3 + vsyasm-1.3.0-win64 a. ...

  7. JavaScript 性能优化1

    一直在学习javascript,也有看过<犀利开发Jquery内核详解与实践>,对这本书的评价只有两个字犀利,可能是对javascript理解的还不够透彻异或是自己太笨,更多的是自己不擅于 ...

  8. 4种kill某个用户所有进程的方法

    在linux系统管理中,我们有时候需要kill掉某个用户的所有进程,初学者一般先查询出用户的所有pid,然后一条条kill掉,或者写好一个脚本,实际上方法都有现成的,这边有4种方法,我们以kill用户 ...

  9. thinkphp验证码点击更换js实现

    <img src="__CONTROLLER__/verify" alt="" onclick=this.src="__CONTROLLER__ ...

  10. 022 ARM寄存器详解

    R13:堆栈指针寄存器 SP R14:链接寄存器 LR R15:程序计数器 PC指针 CPSR:当前程序状态寄存器 SPSR:备份程序状态寄存器