bzoj 1588 bst
用set存下就好了。
/************************************************************** Problem: 1588 User: BLADEVIL Language: C++ Result: Accepted Time:180 ms Memory:1468 kb ****************************************************************/ //By BLADEVIL #include <cstdio> #include <set> #define inf 1<<30 using namespace std; int n,ans; int main() { int x; set<int>bt; bt.insert(inf); bt.insert(-inf); scanf("%d",&n); for (int i=1;i<=n;i++) { //printf(" %d\n",ans); if (scanf("%d",&x)==EOF) x=0; if (i==1) ans=x,bt.insert(x); else { int a,b; a=*bt.upper_bound(x); b=*(--bt.upper_bound(x)); ans+=(a-x<x-b)?a-x:x-b; bt.insert(x); } } printf("%d\n",ans); return 0; }