poj 1862 Stripies/优先队列
原题链接: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/优先队列的更多相关文章
- POJ 1862 Stripies 贪心+优先队列
http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成 ...
- POJ 1862 Stripies【哈夫曼/贪心/优先队列】
Stripies Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18198 Accepted: 8175 Descrip ...
- POJ 1862 Stripies 【优先队列】
题意:科学家发现一种奇怪的东西,他们有重量weight,如果他们碰在一起,总重变成2*sqrt(m1*m2).要求出最终的重量的最小值. 思路:每次选取质量m最大的两个stripy进行碰撞结合,能够得 ...
- POJ 1862 Stripies (哈夫曼树)
Stripies Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10263 Accepted: 4971 Descrip ...
- STL 训练 POJ - 1862 Stripies
Description Our chemical biologists have invented a new very useful form of life called stripies (in ...
- POJ 1862 Stripies#贪心(水)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<cmath> #include<algorithm ...
- POJ 1862 Stripies
每次合并最大的两个,优先级队列维护一下. 输出的时候%.3lf G++会WA,C++能AC,改成%.3f,都能AC. #include<cstdio> #include<cstrin ...
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
随机推荐
- SDUT 2772 数据结构实验之串一:KMP简单应用
数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...
- UVa11210 中国麻将 Chinese Mahjong-搜索
https://vjudge.net/problem/UVA-11210 //被水题虐了一上午... #include<iostream> #include<cstdio> # ...
- PC上安装MAC X Lion
PC上安装MACXLion 网上关于如何在PC下安装MAC的文章已近不少了,但对于一些初学者在实践当中会遇到各种问题,以下视频资料为大家展示两种虚拟机安装MacOS. 1.VmwareWorkstat ...
- Windbg 内存命令 《第四篇》
内存是存储数据.代码的地方,通过内存查看命令可以分析很多问题.相关命令可以分为:内存查看命令和内存统计命令.内存统计命令用来分析内存的使用状况. 一.查看内存 有非常丰富的内存查看命令,它们被容易为d ...
- 【Python】django安装
官方下载:https://www.djangoproject.com/download/ 报错 [root@test Django-]# python setup.py install Traceba ...
- web.config中sessionState节点的配置方案
web.config中sessionState节点的配置方案 web.config关于sessionState节点的配置方案,sessionState有五种模式:Custom,off,inProc,S ...
- 手机NFC模拟门禁卡
楼主所在的某电子科技类大学,从宿舍楼到实验楼到图书馆办公楼,全部都有门禁,前两天突然在某安软件市场看到一个可以模拟门禁卡的软件,然而可能是我的手机系统太6了,竟然模拟不了,无奈自己动手,从根本上解决问 ...
- kafka概念
一.结构与概念解释 1.基础概念 topics: kafka通过topics维护各类信息. producer:发布消息到Kafka topic的进程. consumer:订阅kafka topic进程 ...
- C# 类型转换 Dictionary转Model类
/// <summary> /// 把Model转换为DataRow /// </summary> /// <typeparam name="T"&g ...
- CentOS 6.X版本升级PHP
#-----------------------------CentOS 6.X版本升级PHP------------------#! /bin/sh #1.关闭selinuxcp -rp /etc/ ...