【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 本文实现了在连续视频数据流中几种不同的运动检测算法,他们都是基于当前帧图像和前一帧图像的 ...
随机推荐
- freefcw/hustoj Install Guide
First of all, this version hustoj is a skin and improved for https://code.google.com/p/hustoj/. So t ...
- ACM 第三天
A - Arpa’s hard exam and Mehrdad’s naive cheat CodeForces - 742A There exists an island called Arpa’ ...
- Scala快速入门-基础
HelloWorld 从HelloWorld开始,使用scala IDE编辑器. 新建scala project 新建scala object 编写HelloWorld run as scala ap ...
- ZOJ 2072 K-Recursive Survival
https://vjudge.net/contest/67836#problem/K n people numbered 1 to n around a circle, we eliminate ev ...
- Python使用又拍云进行第三方文件拉取
在爬虫过程中,需要将图片或其他文件进行存储到云上,但在下载图片时,看官方文档,貌似需要先下载到后再上传又拍云,感觉效率很低下.经查找资料实现Python直接对第三方资源进行文件拉取操作,即不需要下载到 ...
- 窗口中各模块的切换效果,使用jquery实现
用到了两个js库,请自行下载,用到的背景图片可任意图片都可以,主要是看效果 <!DOCTYPE html> <html> <head> <script src ...
- Django 2.0 学习(09):Django 静态文件(样式和背景图片)
应用的定制化:静态文件 首先,在polls目录中创建一个名叫static的目录.Django会在该目录里面查找静态文件,类似于Django在polls/template目录下查找模板文件. Djang ...
- 使用canvas控制gif图片的播放与暂停
if ('getContext' in document.createElement('canvas')) { HTMLImageElement.prototype.play = function() ...
- hdu 1848(Fibonacci again and again)(SG博弈)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- [CF622F]The Sum of the k-th Powers
题目大意:给你$n,k(n\leqslant10^9,k\leqslant10^6)$,求:$$\sum\limits_{i=1}^ni^k\pmod{10^9+7}$$ 题解:可以猜测是一个$k+1 ...