校内赛 不正常序列 堆 解题报告

这里写图片描述

思路

如果成功的话应该卡掉了所有手写平衡树的人。
这道题询问中位数完全不需要平衡树。
我们直接维护两个堆,一个是所有大于中位数的数的集合,一个是小于中位数的数的集合

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int MOD=1e9+7;
const int N=1000000+5;
int n,one[N],two[N],top1,top2,x,y;
int main()
{
    freopen("unnormal.in","r",stdin);
//  freopen("unnormal.out","w",stdout);
    long long a,b,c,sum=1;
    scanf("%I64d%I64d%I64d%d",&a,&b,&c,&n);
    one[0]=1;top1=1;top2=0;
    for (int i=2;i<=n;i++)
    {
        x=one[0];//作为中位数 
        y=(int)((a*x+b*i+c)%MOD);
        if (y<=one[0])
        {       
            one[top1]=y;
            push_heap(one,one+(++top1));
        }
        else
        {
            two[top2]=y;
            push_heap(two,two+(++top2),greater<int>());
        }
        if (top1>top2)
        {
            two[top2]=one[0];
            push_heap(two,two+(++top2),greater<int>());
            pop_heap(one,one+(top1--));
        }
        if (top2>top1)
        {
            one[top1]=two[0];
            push_heap(one,one+(++top1));
            pop_heap(two,two+(top2--),greater<int>());          
        }
        sum+=y;
    }
    printf("%I64d\n",sum);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值