【题目链接】

点击打开链接

【算法】

观察式子 : 最小波动值 = min{|该天营业额 - 之前某天的营业额|}

= min{该天营业额 - 该天营业额的前驱,该天营业额的后继 - 该天营业额}

用Splay维护前驱和后继即可

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 32767
const int INF = 2e9; int i,N,minn,x,ans; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} struct Splay {
int root,total;
struct Node {
int fa,size,val,son[],cnt;
} Tree[MAXN+];
inline bool get(int x) {
return Tree[Tree[x].fa].son[] == x;
}
inline void new_node(int index,int f,int x) {
Tree[index].val = x;
Tree[index].size = Tree[index].cnt = ;
Tree[index].fa = f;
Tree[index].son[] = Tree[index].son[] = ;
}
inline void update(int index) {
Tree[index].size = Tree[index].cnt;
Tree[index].size += Tree[Tree[index].son[]].size;
Tree[index].size += Tree[Tree[index].son[]].size;
}
inline void rotate(int x) {
int f = Tree[x].fa,g = Tree[f].fa,
tmpx = get(x),tmpf = get(f);
if (!f) return;
Tree[f].son[tmpx] = Tree[x].son[tmpx^];
if (Tree[x].son[tmpx^]) Tree[Tree[x].son[tmpx^]].fa = f;
Tree[x].son[tmpx^] = f;
Tree[f].fa = x;
Tree[x].fa = g;
if (g) Tree[g].son[tmpf] = x;
update(f);
update(x);
}
inline void splay(int x) {
int f;
for (f = Tree[x].fa; (f = Tree[x].fa); rotate(x))
rotate((get(x) == get(f)) ? (f) : (x));
root = x;
}
inline void Insert(int x) {
int index = root;
bool tmp;
if (!total) {
new_node(++total,,x);
root = total;
return;
}
while (true) {
if (Tree[index].val == x) {
++Tree[index].cnt;
splay(index);
return;
}
tmp = Tree[index].val < x;
if (!Tree[index].son[tmp]) {
new_node(++total,index,x);
Tree[index].son[tmp] = total;
splay(total);
return;
} else
index = Tree[index].son[tmp];
}
}
inline int query_min(int index) {
while (true) {
if (!Tree[index].son[]) return Tree[index].val;
else index = Tree[index].son[];
}
}
inline int query_max(int index) {
while (true) {
if (!Tree[index].son[]) return Tree[index].val;
else index = Tree[index].son[];
}
}
inline int pred(int x) {
int index = root;
bool tmp;
while (true) {
if (Tree[index].val == x) break;
tmp = Tree[index].val < x;
index = Tree[index].son[tmp];
}
splay(index);
if (Tree[index].cnt > ) return x;
else if (Tree[index].son[]) return query_max(Tree[index].son[]);
else return -INF;
}
inline int succ(int x) {
int index = root;
bool tmp;
while (true) {
if (Tree[index].val == x) break;
tmp = Tree[index].val < x;
index = Tree[index].son[tmp];
}
splay(index);
if (Tree[index].cnt > ) return x;
else if (Tree[index].son[]) return query_min(Tree[index].son[]);
else return INF;
}
} T; int main() { read(N);
for (i = ; i <= N; i++) {
minn = INF;
read(x);
if (i == ) { ans += x; T.Insert(x); continue; }
T.Insert(x);
if (x - T.pred(x) < minn) minn = x - T.pred(x);
if (T.succ(x) - x < minn) minn = T.succ(x) - x;
ans += minn;
} writeln(ans); return ; }

【HNOI 2002】 营业额统计的更多相关文章

  1. [HNOI 2002]营业额统计

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

  2. [BZOJ 1588][HNOI 2002] 营业额统计

    这果然是在那个没有STL的年代出的题 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 16648  Solve ...

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

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

  4. bzoj 1588 [HNOI2002] 营业额统计 链表和Splay

    来自HNOI 2002营业额的统计一题,这题以前是用链表水过的,最近看见许多splay的题,赶紧张一下知识. 题目大意就是对于一个序列,输出每个元素与它之前元素的差的最小值的和.先说链表的方法吧. 大 ...

  5. CJOJ 1308 【HNOI 2002 】营业额统计 / CodeVS 1296 营业额统计(STL,二分)

    CJOJ 1308 [HNOI 2002 ]营业额统计 / CodeVS 1296 营业额统计(STL,二分) Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一 ...

  6. AC日记——营业额统计 1296 codevs

    1296 营业额统计 2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description Tiger ...

  7. CodeVS 1296 营业额统计

    1296 营业额统计2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description Tiger最近被公司升任为营业部经理, ...

  8. BZOJ1588: [HNOI2002]营业额统计[BST]

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

  9. 【Treap】bzoj1588-HNOI2002营业额统计

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

  10. 营业额统计(SBT)

    营业额统计(SBT) #include<cstdio> #include<cstring> #include<string> #include<cstdlib ...

随机推荐

  1. codevs——1294 全排列

    1294 全排列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给出一个n, 请输出n的所有全 ...

  2. Spring自带mock测试Controller

    原文:http://blog.csdn.net/yin_jw/article/details/24726941 准备SpringMVC环境 注意:使用mock测试需要引入spring-test包 Ba ...

  3. Android6.0权限管理以及使用权限该注意的地方

    Android 6.0 Marshmallow首次增加了执行时权限管理,这对用户来说,能够更好的了解.控 制 app 涉及到的权限.然而对开发人员来说却是一件比較蛋疼的事情.须要兼容适配,并保证程序功 ...

  4. 关于HTML中文乱码问题

    系统:ubuntu 14.04 软件:bluefish 一.乱码原因 1.不同编码内容混杂:HTML乱码是因为html编码问题照成(常见gb2312与utf-8两种编码内容同一时候存在照成) 2.未设 ...

  5. Win7 VNC远程连接Centos桌面

    一,安装Linux桌面: yum -y groupinstall Desktop yum -y groupinstall "X Window System" yum -y grou ...

  6. mysql 同样内容的字段合并为一条的方法

    从两个表中内联取出的数据,当中category_name字段有同样内容,想将具有同样内容的字段进行合并,将amount字段进行加法运算,变成下表中的内容 url=http%3A%2F%2Fdev.my ...

  7. [UnityUI]一些有趣的UI样例

    1.环形进度条 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  8. Python中ConfigParser模块应用

    Python中ConfigParser模块应用 Python的ConfigParser模块定义了3个对INI文件进行操作的类 RawConfigParser.ConfigParser和SafeConf ...

  9. C#模拟登录Facebook 实现发送消息、评论帖子

    由于目前电脑网页版FB实现模拟登录比较困难,本次选择了FB的手机版页面进行登录 MVC: private static string UserName = "用户名"; priva ...

  10. C# Backgroundworker(后台线程)的使用

    namespace BackgroundWorkderPauseSample { public partial class MainForm : Form { BackgroundWorker wor ...