bzoj 1588: [HNOI2002]营业额统计 解题报告

写在前面

第一次用了这个板块
毕竟这个版块的内容是很有必要的
相信大家都是通过搜索bzoj 1588来的
但是那里并没有告诉我们最小波动值是什么
所以给大家更完美的题目描述(然后我就不粘题目过来了)

题目描述

传送门

想说的话

最近迷上了stl2333
虽说

题解

这个题我们直接用set维护一下
用两个迭代器一个set用来找当前的前驱和后继
分四种情况讨论
当前没有前驱的时候 我们只能用后继
没有后继的时候 我们只能用前驱
都有的时候我们讨论abs大小就行

代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;
#define inf 1<<30
set<int>st;
set<int>::iterator sub,pre;
int n,ans,x;
inline int read()
{
    int s=0,w=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0')
    {
        s=s*10+ch-'0';
        ch=getchar();
    }
    return s*w;
}
int main()
{
    n=read();
    n--;
    st.clear();
    st.insert(inf);
    st.insert(-inf);
    x=read();
    st.insert(x);
    ans+=x;
    while(n--)
    {
        x=read();
        sub=st.lower_bound(x);
        pre=st.lower_bound(x);
        pre--;
        //cout<<*pre<<" "<<*sub<<endl;
        if(*sub==inf)ans+=(x-*pre);
        else if(*pre==-inf)ans+=(*sub-x);
        else ans+=(*sub-x>=x-*pre)?(x-*pre):*sub-x;
        st.insert(x);
    }
    cout<<ans<<endl;
}

写在最后

有的时候stl跑起来很慢 可能是正常splay或者treap的二倍
但是写起来的时间可能是那两个的十分之一2333
所以说还是有用的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值