[bzoj1588][HNOI2002]营业额统计

 题意:有n个数,每个数带来的代价是它与它之前的任意一个数的差的绝对值的最小值,第一个数的代价是自己。你要算出代价总和。$n\leqslant 32767$

题解:看情况乱搞,直接set最简单。

#include<iostream>
#include<cstdio>
#include<set>
#define ll long long
#define INF 2000000000
using namespace std;
inline int read()
{
    int x = 0 , f = 1; char ch = getchar();
    while(ch < '0' || ch > '9'){ if(ch == '-') f = -1;  ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

ll ans=0;
int n;
set<int> st;
set<int>::iterator a,b; 
inline int abs(int x){return x<0?-x:x;}
int main()
{    
    st.insert(-INF);st.insert(INF);
    n=read();st.insert((int)(ans=read()));
    for(int i=1;i<n;i++)
    {
        int x=read();
        b=a=st.lower_bound(x);--b;
        ans+=min(abs(*b-x),abs(*a-x));
        st.insert(x);
    }
    cout<<ans;
    return 0;
}

 

posted @ 2017-03-27 11:06  FallDream  阅读(130)  评论(0编辑  收藏  举报