51nod 1785 数据流中的算法 | STL的应用
51nod 1785 数据流中的算法
题面
动态求平均数、方差、中位数。
题解
这道题的坑:
平均数在答案中是向下取整输出并在后面添加".00"
方差:平方的平均数减去平均数的平方
中位数:维护两个multiset,一个存较小的一半元素,另一个存较大的一半。当两个multiset的大小相差超过二时,把较大的multiset中多出来的那个放到另一个multiset中。这样就知道中位数或中间两个数了。
注意multiset中,st.erase(1)会删除所有大小为1的元素!
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#define INF 0x3f3f3f3f
#define space putchar(' ')
#define enter putchar('\n')
using namespace std;
typedef long long ll;
template <class T>
bool read(T &x){
char c;
bool op = 0;
while(c = getchar(), c < '0' || c > '9')
if(c == '-') op = 1;
else if(c == EOF) return 0;
x = c - '0';
while(c = getchar(), c >= '0' && c <= '9')
x = x * 10 + c - '0';
if(op) x = -x;
return 1;
}
template <class T>
void write(T x){
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar('0' + x % 10);
}
int n, m;
queue <int> q;
multiset <int> smaller, bigger;
int sum1, cnt, sum2;
double mid;
void getmid(){
if((int)smaller.size() - (int)bigger.size() > 1){
bigger.insert(*smaller.rbegin());
smaller.erase(smaller.find(*smaller.rbegin()));
}
else if((int)bigger.size() - (int)smaller.size() > 1){
smaller.insert(*bigger.begin());
bigger.erase(bigger.begin());
}
if(bigger.size() == smaller.size())
mid = ((double) *bigger.begin() + *smaller.rbegin()) / 2;
else if(bigger.size() > smaller.size())
mid = *bigger.begin();
else
mid = *smaller.rbegin();
}
void pop(){
int u = q.front();
q.pop();
if(smaller.find(u) != smaller.end()) smaller.erase(smaller.find(u));
else bigger.erase(bigger.find(u));
getmid();
sum1 -= u;
sum2 -= u * u;
cnt--;
}
void push(int u){
q.push(u);
if(u <= mid) smaller.insert(u);
else bigger.insert(u);
getmid();
sum1 += u;
sum2 += u * u;
cnt++;
}
int main(){
read(n), read(m);
while(n--){
int op, tmp;
read(op);
if(op == 1){
if(cnt >= m) pop();
read(tmp);
push(tmp);
}
else if(op == 2)
printf("%d.00\n", sum1 / cnt);
else if(op == 3)
printf("%.2lf\n", (double) sum2 / cnt - ((double)sum1 / cnt) * ((double)sum1 / cnt));
else
printf("%.2lf\n", mid);
}
return 0;
}
51nod 1785 数据流中的算法 | STL的应用的更多相关文章
- 51nod 1785 数据流中的算法 (方差计算公式)
1785 数据流中的算法 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠 ...
- 【51NOD】数据流中的算法
[算法]数学 [题解] 1.平均数:累加前缀和.//听说要向下取整? 2.中位数:双堆法,大于中位数存入小顶堆,小于中位数存入大顶堆,保证小顶堆内数字数量≥大顶堆,奇数则取小堆顶,偶数则取两堆顶/2. ...
- AC日记——数据流中的算法 51nod 1785
数据流中的算法 思路: 线段树模拟: 时间刚刚卡在边界上,有时超时一个点,有时能过: 来,上代码: #include <cstdio> #include <cstring> # ...
- 【51nod 1785】数据流中的算法
Description 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠标轨迹等特征计算用户对于网站的满意程度. 现有的统计工具只能统计某一个窗口中,用户的满意程 ...
- STL中的算法小结
()要运用STL的算法,首先必须包含头文件<algorithm>,某些STL算法用于数值处理,因此被定义于头文件<numeric> ()所有STL算法都被设计用来处理一个或多个 ...
- 有一个问题关于stl函数中的算法问题
是不是stl中的算法函数中参数只要是和函数相关的就是函数对象和谓词?
- STL中的算法
STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baidu.com/ding ...
- [算法]最小的K个数和数据流中的中位数
1. 最小的K个数 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 思路 Java 中的PriorityQueue是 ...
- [LeetCode解题报告] 703. 数据流中的第K大元素
题目描述 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包 ...
随机推荐
- Unity 实现一个简单的 TPS 相机
效果如下: 代码如下: public class TPSCamera : MonoBehaviour { /// <summary> /// 目标对象 /// </summary&g ...
- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 78,050,512 milliseconds ago.
今天访问已经架上服务器的网站,报错: Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet s ...
- Git知识点整合
Git安装 Windows上安装Git 64 位安装包下载地址 : https://github.com/git-for-windows/git/releases/download/v2.16.2.w ...
- .NetCore mvc Ajax Post数据到后端
在前端页面中,如果没有表单,想把复杂对象提交到后端,可使用以下方法 后端Controller中定义以下方法: [HttpPost] public int AddSolution([FromBody]S ...
- [zabbix] zabbix数据采集频率、数据连续多次异常触发、告警次数、告警频率
数据采集频率:1分钟采集一次 数据连续多次异常触发:连续三次异常才触发告警 告警次数:告警三次 告警频率:每隔10分钟告警一次 默认模板“Template App Zabbix Agent”监控项“A ...
- ORM PHP 学习记录
ORM:object relation mapping,即对象关系映射,简单的说就是对象模型和关系模型的一种映射.为什么要有这么一个映射?很简单,因为现在的开发语言基本都是oop的,但是传统的数据库却 ...
- 互评Beta版本——杨老师粉丝群——Pinball
互评beta版本 杨老师粉丝群——<PinBall> 一.基于NABCD评论作品,及改进建议 1.根据(不限于)NABCD评论作品的选题 (1)N(Need,需求) 随着年龄的增长, ...
- a标签的href为空的问题
在表格里写一个a标签链接刷新表格的时候,没注意,把a标签的href设置为""空字符串,导致每次刷新表格之后会再刷新一次整体页面,找了很久都没发现问题出在哪里,最后无意之间,鼠标在一 ...
- BloomFilter——大规模数据处理利器(爬虫判重)
http://www.cnblogs.com/heaad/archive/2011/01/02/1924195.html Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快 ...
- HDU 5925 Coconuts 离散化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5925 Coconuts Time Limit: 9000/4500 MS (Java/Others) ...