2924: 营业额统计

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 389            Accepted:122

Description

Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况:

该天的最小波动值 = min{ |该天以前某天的营业额 - 该天的营业额 | }

当最小波动值越大时,就说明营业情况越不稳定。而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。第一天的最小波动值为第一天的营业额。

Input

测试数据多组,每组的第一行为正整数n(1 <= n <= 32767), 表示该公司从成立一直到现在的天数. 接下来的n行每行有一个整数Ai(Ai <= 1000000) , 表示第i天的营业额。处理到EOF为止。

Output

每组数据占一行,每行输出一个整数,每天最小波动值的和。结果小于2^31

Sample Input

6
5
1
2
5
4
6

Sample Output

12

Hint

5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
 
思路:在出现过的数字中,找出与当前数字x差值最小的值的差值。求和。
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N = 1e5+;
const int INF = 0x3f3f3f3f;
int pre[N] , key[N],ch[N][],root,tot1;
int n; void NewNode(int &r ,int father,int k)
{
r = ++tot1;/**保存的位置**/
pre[r] = father;
key[r] = k;
ch[r][] = ch[r][] = ;
} void Rotate(int x,int kind)
{
int y = pre[x]; // x 的父亲节点
ch[y][!kind] = ch[x][kind]; /**建立x的kind节点和父亲节点的关系**/
pre[ch[x][kind]] = y; if(pre[y]) /**建立x节点和x父亲父亲节点的关系**/
{
int k = ch[ pre[y] ] [ ] == y;
ch[pre[y]][k] = x;
}
pre[x] = pre[y]; ch[x][kind] = y; /**建立x和y的关系**/
pre[y] = x;
}
//Splay调整,将结点r调整到goal下面
void Splay(int r , int goal )
{
while(pre[r]!=goal)
{
if( pre[pre[r]] == goal ) Rotate(r,ch[pre[r]][]==r);
else
{
int y = pre[r];
int kind = ch[pre[y]][] == y; /**如果是左节点返回1。否则0。取反的kind**/ if(ch[y][kind] == r)/**r和父亲节点y的位置,和y和其y父亲节点位置不同。**/
{ /**采用 Zig-Zag或Zag-Zig操作**/
Rotate(r,!kind);
Rotate(r,kind);
}
else/**Zig-Zig或Zag-Zag操作**/
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
if(goal == ) root = r;
} int Insert(int k)
{
int r = root;
while( ch[r][key[r]<k]!=)
{
if( key[r] == k)
{
Splay(r,);
return ;
}
r = ch[r][key[r]<k];
}
NewNode(ch[r][key[r]<k],r,k);
Splay(ch[r][key[r]<k],);
return ;
} int get_pre(int x)
{
int temp = ch[x][];
if(temp == ) return INF;
while(ch[temp][])
temp = ch[temp][];
return key[x] - key[temp];
} int get_next(int x)
{
int temp = ch[x][];
if(temp == ) return INF;
while(ch[temp][])
temp = ch[temp][];
return key[temp] - key[x];
}
int main()
{
int n , x;
while(scanf("%d",&n)>)
{
root = tot1 = ;
int sum = ;
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(i==)
{
sum = sum + x;
NewNode(root,,x);
continue;
}
if(Insert(x) == ) continue;
int a = get_pre(root);
int b = get_next(root);
sum=sum+min(a,b);
}
printf("%d\n",sum);
}
return ;
}
 

NOIP 营业额统计 splay tree 纯模板的更多相关文章

  1. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

  2. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

  3. [HNOI2002]营业额统计 Splay tree

    Splay tree入门题,学好代码风格,学习HH大牛的,传送门.. #include <functional> #include <algorithm> #include & ...

  4. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  5. NOI 2002 营业额统计 (splay or fhq treap)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  6. 【BZOJ-1588】营业额统计 Splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12485  Solved: 4508[Submit][Sta ...

  7. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

  8. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  9. 洛谷P2234 [HNOI2002] 营业额统计 [splay]

    题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

随机推荐

  1. poj 算法 分类

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 最近AC题:2528   更新时间:2011.09.22  ...

  2. [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. 在C#中使用LOG4NET(winform程序

    http://www.csharpwin.com/csharpspace/678.shtml 1.下载log4net (Google log4net) 2.unzip log4net 3.运行VS,新 ...

  4. 夺命雷公狗---微信开发55----微信js-sdk接口开发(2)接口功能介绍之签名算法

    我们JS-SDK里面其实有不少的接口 startRecord---录音 stopRecord---停止录音 playVoice---播放 pauseVoice---暂停播放 uploadImage-- ...

  5. scan cell

    scan cell有两种不同的input: 1)data input:由电路的combinational logic驱动: 2)scan input:由另一个scan cell驱动,从而形成scan  ...

  6. yii2多语言设置

    yii2的多语言切换功能 1.页面添加语言切换按钮,如下图: 代码如下: <ul>       <li>           <a href="javascri ...

  7. 似是而非的k=sqrt(n)

    //题目:输入一个大于3的整数n,判定它是否为素数(prime,又称质数)#include <stdio.h>#include <math.h>int main(){int n ...

  8. android 学习随笔十九(对话框、样式、主题、国际化 )

    1.对话框 package com.itheima.dialog; import android.os.Bundle; import android.app.Activity; import andr ...

  9. Delphi 的各版本定义,用于预编译参数中,避免忘记备忘之

    DELPHI的版本宏: VER80 - Delphi 1 VER90 - Delphi 2 VER100 - Delphi 3 VER120 - Delphi 4 VER130 - Delphi 5 ...

  10. 【secureCRT】设置自动连接会话+设置自动连接上次使用的会话: