POJ 2833 求平均数。 模板:STL实现大根堆和小根堆
2012-06-30 16:07
411 查看
这题直接存储会超内存。因为n1,n2都很小,用大根堆和小根堆分别存储 最小的n2个数 和最大的n1个数。
//remove the greatest n1 ones and the least n2 ones //priority_queue <BIGR> 是大根堆 //priority_queue <SMALLR> 是小根堆 //priority_queue <int,vector<int>,less<int> > 大根堆 //priority_queue <int,vector<int>,greater<int> > 小根堆 #include <stdio.h> #include <algorithm> #include <queue> using namespace std; const int S=5000100; struct BIGR{ int n; BIGR(int n=0):n(n){} friend bool operator < (const BIGR &a , const BIGR &b){ return (a.n<b.n?1:0); } };//restore the least n2 ones struct SMALLR{ int n; SMALLR(int n=0):n(n){} friend bool operator < (const SMALLR &a , const SMALLR &b){ return (a.n<b.n?0:1); } }; int main(){ int n1,n2,n,i,j,k,tot,x; double avg; while (scanf("%d%d%d",&n1,&n2,&n),!(n1==0&&n2==0&&n==0)){ priority_queue <BIGR> small; priority_queue <SMALLR> big; tot=n-n1-n2; avg=0; for (i=0;i<n;i++) { scanf("%d",&x); avg+=x*1.0/tot; if (small.size()<n2){ small.push(x); } else{ if (x<small.top().n){ small.pop(); small.push(x); } } if (big.size()<n1){ big.push(x); } else{ if (x>big.top().n){ big.pop(); big.push(x); } } } while (!small.empty()){ avg-=small.top().n*1.0/tot; // printf("small: %d\n",small.top().n); small.pop(); } while (!big.empty()){ avg-=big.top().n*1.0/tot; // printf("big: %d\n",big.top().n); big.pop(); } printf("%.6lf\n",avg); } }
相关文章推荐
- 数据离散化模板(用STL实现)
- stl实现全排列模板
- hash表 拉链法 仿sgi stl 非模板简单实现
- C模板实现STL容器中的vector
- POJ 3277 City Horizon【map模板】【stl练习】
- C++ 模板应用举例_模板实现STL类(堆栈)
- [poj 1611]The Suspects[并查集模板][递归与非递归实现]
- 使用模板和STL实现对象池
- POJ-1273-Drainage Ditches(最大流)dinic实现 后续模板待补充
- LIS 模板 (最长上升/下降子序列) STL实现
- poj 1258 MST模板题(用PRIM+堆实现)
- STL 队列模板实现
- poj 2001 字典树入门题(数组实现模板)
- STL-lesson001_1_模板实现Array类
- 进阶篇_STL详解(函数模板特化,类模板特化,用模板实现自己的通用算法)
- 自己实现STL模板upper_bound() 和lower_bound()
- 类模板模拟实现STL中Vector
- 没有模板代码膨胀的STL:一、设计目标与实现思路
- 类模板模拟实现STL中List
- poj 2442(堆的stl实现)