51nod 1785 数据流中的算法 【D(x)与E(x)+队列+线段树】

59 篇文章 0 订阅
15 篇文章 0 订阅

基准时间限制: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

首先说一下题意----k-应该理解为只能看到k个人的满意度(最后k个人了)

所以涉及到增加数据与删除数据----中位数可以用线段树来搞定-.-       引用  stdio.h确实比 引用 cstdio  要快几倍


代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
#define LL long long
double he[2];
int shu;
struct node{int l,r,shu;}PP[300];
void creat(int l,int r,int x)
{
    PP[x].l=l;PP[x].r=r;
    PP[x].shu=0;
    if (l==r) return ;
    int m=(l+r)>>1;
    creat(l,m,x*2);
    creat(m+1,r,x*2+1);
}
void add(int wei,int x)
{
    PP[x].shu++;
    if (PP[x].l==PP[x].r) return ;
    int m=(PP[x].l+PP[x].r)>>1;
    if (wei>m) add(wei,x*2+1);
    else add(wei,x*2);
}
void jian(int wei,int x)
{
    PP[x].shu--;
    if (PP[x].l==PP[x].r) return ;
    int m=(PP[x].l+PP[x].r)>>1;
    if (wei>m) jian(wei,x*2+1);
    else jian(wei,x*2);
}
int query(int ll,int x)
{
    if (PP[x].l==PP[x].r)
        return PP[x].l;
    else if (PP[x*2].shu<ll)
        query(ll-PP[x*2].shu,x*2+1);
    else
        query(ll,x*2);
}
int main()
{
/*    freopen("in1.txt","r",stdin);
    freopen("out.txt","w",stdout);*/
    queue<int> que;
    int n,k;
    scanf("%d%d",&n,&k);
    int s=0,a,b;
    shu=0;
    creat(0,100,1);
    while (n--)
    {
        scanf("%d",&a);
        switch(a)
        {
            case 1:
                if (shu==k)
                {
                    b=que.front();
                    que.pop();
                    he[0]-=b;
                    he[1]-=b*b;
                    jian(b,1);
                }
                else shu++;
                scanf("%d",&b);
                que.push(b);
                he[0]+=b;he[1]+=b*b;
                add(b,1);
                break;
            case 2:
                printf("%d.00\n",((int)he[0])/shu);
                break;
            case 3:
                printf("%.2lf\n",he[1]/shu-(he[0]/shu)*(he[0]/shu));
                break;
            case 4:
                if (shu%2==0)
                {
                    a=query(shu/2,1);
                    b=query(shu/2+1,1);
                    printf("%.2lf\n",(a+b)/2.0);
                }
                else
                {
                    b=query(shu/2+1,1);
                    printf("%.2lf\n",(double)b);
                }
                break;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值