【51nod 数据流中的算法】+ vector

该博客介绍了51nod的一个新功能,即使用高级人工智能算法来评估用户对网站的满意度。文章重点讨论如何在数据流中处理窗口统计,包括计算用户满意度的均值、方差和中位数。提出了使用vector来实现,通过二分插入和删除维护有序序列,并用数组记录次数。同时,作者提到了stdio.h和cstdio在C++程序中的性能差异。

数据流中的算法
Wizmann (命题人)
基准时间限制:1.5 秒 空间限制:131072 KB 分值: 20
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
对于“查询均值”、“查询方差”、“查询中位数”操作的结果,输出保留两位小数。

思路 : 1) vector 可以二分插入,删除,维护窗口内有序的序列
2) 用一个数组记录下窗口内的数的出现次数
ps : stdio.h 可以过掉,cstdio 却会超时 ……. c++ 库要比 c 慢一点 ?

两种AC代码:

vector :

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
const int MAX = 1e6 + 10;
int a[MAX];
vector <int> v;
int main()
{
    int n,k,x,y,l = 1,nl = 0,ans = 0;
    scanf("%d %d",&n,&k);
    while(n--){
        scanf("%d",&x);
        if(x == 1){
            scanf("%d",&y);
            a[++nl] = y;
            ans += y;
            if(v.size() == 0) v.insert(v.begin(),y);
            else{
                int p = upper_bound(v.begin(),v.end(),y) - v.begin();
                v.insert(v.begin() + p,y);
            }
            if(nl > k){
                int o = upper_bound(v.begin(),v.end(),a[l]) - v.begin() - 1;
                if(o < 0) o = 0;
                v.erase(v.begin() + o);
                ans -= a[l];
                l++;
            }
        }
        else if(x == 2) printf("%.2lf\n",floor((double) ans / (nl - l + 1)));
        else if(x == 3){
            double sum = 0,o = (double) ans / (nl - l + 1);
            for(int i = l; i <= nl; i++)
                sum += ((double)(o - a[i])) * ((double)(o - a[i]));
            printf("%.2lf\n",sum / (nl - l + 1));
        }
        else{
            int w = (nl - l + 1);
            if(w & 1)
                printf("%.2lf\n",v[w / 2] * 1.0);
            else
                printf("%.2lf\n",(v[w / 2] * 1.0 + v[w / 2 - 1] * 1.0) / 2);
        }
    }
    return 0;
}

数组标记 :

#include<stdio.h>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e6 + 10;
int a[MAX],b[110];
int main()
{
    int n,k,x,y,l = 1,nl = 0,ans = 0;
    scanf("%d %d",&n,&k);
    while(n--){
        scanf("%d",&x);
        if(x == 1){
            scanf("%d",&y);
            a[++nl] = y;
            b[y]++;
            ans += y;
            if(nl > k){
                b[a[l]]--;
                ans -= a[l];
                l++;
            }
        }
        else if(x == 2) printf("%.2lf\n",floor((double) ans / (nl - l + 1)));
        else if(x == 3){
            double sum = 0,o = (double) ans / (nl - l + 1);
            for(int i = l; i <= nl; i++)
                sum += ((double)(o - a[i])) * ((double)(o - a[i]));
            printf("%.2lf\n",sum / (nl - l + 1));
        }
        else{
            int w = (nl - l + 1),h = 0,cut = 0,p = w / 2;
            if(w & 1){
                for(int i = 0;i <= 100; i++){
                    h += b[i];
                    if(h > p){
                        printf("%.2lf\n",(double)i);
                        break;
                    }
                }
            }
            else{
                int t = -1;
                for(int i = 0;i <= 100; i++){
                    h += b[i];
                    if(h >=  p && t == -1) t = i;
                    if(h > p){
                        printf("%.2lf\n",(double)(t + i) / 2);
                        break;
                    }
                }
            }
        }
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值