poj 1862 Stripies/优先队列

时间:2021-02-23 05:20:00

原题链接: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 ;
}