您的位置:首页 > 其它

1785 数据流中的算法 模拟,精度

2018-02-09 16:20 260 查看
1785 数据流中的算法



基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20
难度:3级算法题


收藏


关注

51nod近日上线了用户满意度检测工具,使用高级人工智能算法,通过用户访问时间、鼠标轨迹等特征计算用户对于网站的满意程度。

现有的统计工具只能统计某一个窗口中,用户的满意程度的均值。夹克老爷想让你为统计工具添加一个新feature,即在统计均值的同时,计算窗口中满意程度的标准差和中位数(均值需要向下取整)。

Input
第一行是整数n与k,代表有n次操作,时间窗口大小为k。 
(1 <= n <= 10^6, 1 <= k <= 100)

接下来的n行,每行代表一次操作。操作有“用户访问”、“查询均值”、“查询方差”、“查询中位数”四种。每行的第一个数代表操作类型。

操作数1:用户访问
输入格式:<1, v>
用户的满意度v为闭区间[0, 100]中的任意整数。用户每访问一次,数据更新,移动统计窗口。

操作数2:查询均值
输入格式:<2>
统计窗口内的用户满意度的均值。

操作数3:查询方差
输入格式:<3>
统计窗口内用户满意度的方差

操作数4:查询中位数
输入格式:<4>
统计窗口内用户满意度的中位数

p.s. 在有查询请求时,窗口保证不为空
p.s.s. 有查询请求时,窗口可能不满

Output
对于“查询均值”、“查询方差”、“查询中位数”操作的结果,输出保留两位小数。

Input示例
12 3
1 1
1 2
1 3
2
3
4
1 4
1 5
1 6
2
3
4

Output示例
2.00
0.67
2.00
5.00
0.67
5.00

思路:开始用队列最后一组T到死,最后看有人数组过,就改用数组过掉了

Code:

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

const int AX = 1e6+66;
int a[AX];
int b[AX];
int main(){
int n , k ;
scanf("%d%d",&n,&k);
int op ,v;
int tot = 0;
int flag = 0;
int sum = 0;
memset( a , -1 , sizeof(a) );
for( int i = 0 ; i < n ; i++ ){
scanf("%d",&op);
if( op == 1 ){
scanf("%d",&v);
sum += v;
if( tot >= k ) { flag = 1; tot = 0;}
if( flag ){
sum -= a[tot];
}
a[tot++] = v;
}else if( op == 2 ){
int total ;
if( flag )  total = k;
else total = tot;
int ave = (double)sum / total;
printf("%d.00\n",ave);
}else if( op == 3 ){
int total ;
if( flag )  total = k;
else total = tot;
double ave = (double)sum / total;
double sum = 0.0;
for( int j = 0 ; j < total ; j++ ){
sum += ( a[j] - ave ) * ( a[j] - ave );
}
printf("%.2lf\n",(double)sum/total);
}else{
int t = 0;
for( int j = 0 ; j < k ; j++ ){
if( a[j] != -1 ){
b[++t] = a[j];
}
}
sort( b + 1 , b + t + 1 );
printf("%.2lf\n",( b[(t+1)/2] + b[t/2+1] ) * 1.0 / 2.0 );
}
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: