写在前面
第一次用了这个板块
毕竟这个版块的内容是很有必要的
相信大家都是通过搜索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
所以说还是有用的