bzoj-1588 1588: [HNOI2002]营业额统计(BST)

题目链接:

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 13596  Solved: 5049
[Submit][Status][Discuss]

Description

营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。  输入输出要求

Input

第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i天公司的营业额。

Output

输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。

Sample Input

6
5
1
2
5
4
6

Sample Output

12
 
题意:
 
思路:
用set代替BST,找到与这个数相近的两个数,然后求最小的差值;
 
AC代码:
 

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('\n');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18+10;
const int N=1e6+10;
const int maxn=1e3+20;
const double eps=1e-12;


set<int>s;
set<int>::iterator it;
int main()
{
        int n;read(n);
        s.clear();
        int x,fx,fy;
        int ans=0,temp;
        read(x);ans=ans+x;
        s.insert(x);
        For(i,2,n)
        {
            read(x);
            if(s.count(x))continue;
            it=s.upper_bound(x);
            if(it==s.end())temp=abs(x-*(--it));
            else 
            {
                temp=abs(x-*it);
                if(it!=s.begin())temp=min(temp,abs(x-*(--it)));
            }
            ans+=temp;
            s.insert(x);
        }
        print(ans);
    return 0;
}

  

posted @ 2016-08-22 18:24  LittlePointer  阅读(169)  评论(0编辑  收藏  举报