【51NOD】数据流中的算法
【算法】数学
【题解】
1.平均数:累加前缀和。//听说要向下取整?
2.中位数:双堆法,大于中位数存入小顶堆,小于中位数存入大顶堆,保证小顶堆内数字数量≥大顶堆,奇数则取小堆顶,偶数则取两堆顶/2。
3.方差=(平方的均值)-(均值的平方),即对于a,b,c,s2=(a2+b2+c2)/3-((a+b+c)/3)2。
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<set>
#include<cctype>
using namespace std;
const int maxn=;
multiset<int>q2;//小顶堆
struct cmp
{
bool operator() (const int a,const int b)const
{return a>b;}
};
multiset<int,cmp>q1;//大顶堆
int n,k,a[maxn],sum[maxn],tot1,tot2;
long long sums[maxn];
double ans2;
int read()
{
char c;int s=,t=;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
void compair()
{
if(tot1>tot2)
{
int now=*q1.begin();tot1--;//?????????
q1.erase(q1.begin());//一定要删除指定位置,删除multiset中的键值会把全部键值等于的都删掉。
q2.insert(now);tot2++;
}
if(tot2->tot1)
{
int now=*q2.begin();tot2--;
q2.erase(q2.begin());
q1.insert(now);tot1++;
}
if((tot1+tot2)%)
ans2=*q2.begin();
else ans2=1.0*(*q1.begin()+*q2.begin())/;
}
int main()
{
n=read(),k=read();
sum[]=;sums[]=;
int tot=,task=;
for(int i=;i<=n;i++)
{
task=read();
if(task==)
{
tot++;
a[tot]=read();
if(tot>k)
{if(a[tot-k]<ans2)q1.erase(q1.find(a[tot-k])),tot1--;else q2.erase(q2.find(a[tot-k])),tot2--;}
sum[tot]=sum[tot-]+a[tot];
if(a[tot]>=ans2)q2.insert(a[tot]),tot2++;
else q1.insert(a[tot]),tot1++;
compair();
sums[tot]=sums[tot-]+a[tot]*a[tot];
}
else if(task==)
{
if(tot<k)printf("%d.00\n",(sum[tot])/tot);else
printf("%d.00\n",(sum[tot]-sum[tot-k])/k);
}
else if(task==)
{
printf("%.2lf\n",ans2);
}
else if(task==)
{
if(tot<k)printf("%.2lf\n",1.0*(sums[tot])/tot-1.0*(1.0*sum[tot]/tot)*(1.0*sum[tot]/tot));else//1.0进入
printf("%.2lf\n",1.0*(sums[tot]-sums[tot-k])/k-1.0*(1.0*(sum[tot]-sum[tot-k])/k)*(1.0*(sum[tot]-sum[tot-k])/k));
}
}
return ;
}
【51NOD】数据流中的算法的更多相关文章
- 51nod 1785 数据流中的算法 | STL的应用
51nod 1785 数据流中的算法 题面 动态求平均数.方差.中位数. 题解 这道题的坑: 平均数在答案中是向下取整输出并在后面添加".00" 方差:平方的平均数减去平均数的平方 ...
- 51nod 1785 数据流中的算法 (方差计算公式)
1785 数据流中的算法 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠 ...
- AC日记——数据流中的算法 51nod 1785
数据流中的算法 思路: 线段树模拟: 时间刚刚卡在边界上,有时超时一个点,有时能过: 来,上代码: #include <cstdio> #include <cstring> # ...
- 【51nod 1785】数据流中的算法
Description 51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间.鼠标轨迹等特征计算用户对于网站的满意程度. 现有的统计工具只能统计某一个窗口中,用户的满意程 ...
- [算法]最小的K个数和数据流中的中位数
1. 最小的K个数 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4. 思路 Java 中的PriorityQueue是 ...
- 数据流中的中位数 Find Median from Data Stream
2019-04-17 16:34:50 问题描述: 问题求解: class MedianFinder { PriorityQueue<Integer> smaller; PriorityQ ...
- [LeetCode解题报告] 703. 数据流中的第K大元素
题目描述 设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包 ...
- 剑指offer63:数据流中的中位数
题目描述: 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值. ...
- Motion Detection Algorithms视频中运动检测算法源代码及演示代码
原文地址:http://www.codesoso.com/code/Motion_Detection.aspx 本文实现了在连续视频数据流中几种不同的运动检测算法,他们都是基于当前帧图像和前一帧图像的 ...
随机推荐
- alpha-4
前言 失心疯病源4 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 19:00~21:50 利用背景相减法完成背景构建与更新模块,查找关于blob更多的论文资 ...
- 解决连接mysql报错1130
最近在服务器上部署好的应用突然间连接不上mysql数据库,报错“ERROR 1130: Host xxx.xxx.xxx.xxx is not allowed to connect to this M ...
- google go语言开发
C:cd C:\Program Files\go\gopath\src\opmsset GOOS=linuxset GOARCH=amd64set CGO_ENABLED=0make.batgo in ...
- mysql(三) 慢查询分析(二)
在一般的查询中,都要求尽量围绕创建的索引进行.针对索引,常用的有主键索引,单列索引,组合索引,索引合并等. 在评价索引时,关键看区分度.索引区分度=索引列唯一值/表记录数. 如果在区分度很低的列上建索 ...
- BZOJ 1179 Atm(强连通分量缩点+DP)
题目说可以通过一条边多次,且点权是非负的,所以如果走到图中的一个强连通分量,那么一定可以拿完这个强连通分量上的money. 所以缩点已经很明显了.缩完点之后图就是一个DAG,对于DAG可以用DP来求出 ...
- NOI 97 (Vijos 1464)积木游戏(DP)
很普通的DP,设dp[i][j][k]为第i块积木放在第j堆且摆放状态为k的最高高度.方程很容易推出. # include <cstdio> # include <cstring&g ...
- 【bzoj5064】B-number 数位dp
题目描述 B数的定义:能被13整除且本身包含字符串"13"的数. 例如:130和2613是B数,但是143和2639不是B数. 你的任务是计算1到n之间有多少个数是B数. 输入 输 ...
- BZOJ3040:最短路——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3040 题意rt,使用pb_ds的堆解决本问题. 所以其实就是mark一下的. 不过有人确认过官方不 ...
- BZOJ1101 & 洛谷3455:[POI2007]ZAP——题解
https://www.luogu.org/problemnew/show/3455#sub http://www.lydsy.com/JudgeOnline/problem.php?id=1101 ...
- BZOJ3670 [Noi2014]动物园 【KMP计数】
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 3143 Solved: 1690 [Submit][Stat ...