题目:P2234 [HNOI2002]营业额统计
思路:
听说要用splay
所以用set.lower_bound搞一搞就好了(?)……
代码:
#include<bits/stdc++.h>
using namespace std;#define maxn 32767
#define inf ((int)1e9)
#define read(x) scanf("%d",&x)set<int> s;
int n;int main() {
read(n);s.insert(-inf),s.insert(inf);int ans=0;for(int i=1;i<=n;i++) {
int x;read(x);if(i==1) ans=x,s.insert(x);else {
set<int>::iterator k=s.lower_bound(x);int y=*k,z=*(--k);ans+=min(abs(x-y),abs(x-z));s.insert(x);}}printf("%d",ans); return 0;
}