C. C. Blog

Security Research, Algorithm and Data Structure

51Nod 1785 数据流中的算法

Problem

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

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

Solution

方差转化为均值相减,均值维护,中位数multiset维护每次找。

还有双堆维护中位数,QAQ。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<set>
#define mem(ss) memset(ss,0,sizeof(ss))
#define rep(d, s, t) for(int d=s;d<=t;d++)
#define rev(d, s, t) for(int d=s;d>=t;d--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod = 998244353;
const int N = 1e4 + 10;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
//using namespace std;
int n,k;
struct E{
int a;
int p;
}e[1000020];
int head=1,tail=0;
int x,val;
db ans;
std::multiset<int>q;
int sum=0,sum2=0;
bool fg=false;
int cnt;
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%d",&x);
if(x==1){
scanf("%d",&val);
e[++tail].a=val;
e[tail].p=i;
sum+=val;
sum2+=val*val;
q.insert(val);
//cout<<*q.begin()<<endl;
if(tail-head+1==k+1){
q.erase(q.lower_bound(e[head].a));
sum-=e[head].a;
sum2-=e[head].a*e[head].a;
head++;
}
cnt=tail-head+1;
}
else if(x==2){
ans=sum/cnt;
printf("%.2f\n",ans);
}
else if(x==3){
ans=sum2/(ld)cnt-(sum/(ld)cnt)*(sum/(ld)cnt);
printf("%.2f\n",ans);
}
else{
auto tmp=q.begin();
//cout<<"!!"<<*tmp<<endl;
if(cnt%2){
for(int i=1;i<=cnt/2;i++){
tmp++;
}
printf("%.2f\n",(db)*tmp);
}
else{
for(int i=1;i<=cnt/2-1;i++){
tmp++;
}
val=*tmp;
tmp++;
val+=*tmp;
printf("%.2f\n",(db)val/2.0);
}
}
}
return 0;
}
  • 本文作者: CCWUCMCTS
  • 本文链接: https://ccwucmcts.github.io/posts/56103/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!