【HNOI 2002】 营业额统计

【题目链接】

          点击打开链接

【算法】

          观察式子 : 最小波动值 = 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 = 1; x = 0;
		char c = getchar();
		for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
		for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
		x *= f;
}

template <typename T> inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x/10);
    putchar(x%10+'0');
}

template <typename T> inline void writeln(T x) {
    write(x);
    puts("");
}

struct Splay {
		int root,total;
		struct Node {
				int fa,size,val,son[2],cnt;
		} Tree[MAXN+10];
		inline bool get(int x) {
				return Tree[Tree[x].fa].son[1] == x;
		}
 		inline void new_node(int index,int f,int x) {
				Tree[index].val = x;
				Tree[index].size = Tree[index].cnt = 1;
				Tree[index].fa = f;
				Tree[index].son[0] = Tree[index].son[1] = 0; 
		}
		inline void update(int index) {
				Tree[index].size = Tree[index].cnt;
				Tree[index].size += Tree[Tree[index].son[0]].size;
				Tree[index].size += Tree[Tree[index].son[1]].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^1];
				if (Tree[x].son[tmpx^1]) Tree[Tree[x].son[tmpx^1]].fa = f;
				Tree[x].son[tmpx^1] = 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,0,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[0]) return Tree[index].val;
						else index = Tree[index].son[0];
				}	
		}
		inline int query_max(int index) {
				while (true) {
						if (!Tree[index].son[1]) return Tree[index].val;
						else index = Tree[index].son[1];
				}
		}
		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 > 1) return x;
				else if (Tree[index].son[0]) return query_max(Tree[index].son[0]);
				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 > 1) return x;
				else if (Tree[index].son[1]) return query_min(Tree[index].son[1]);
				else return INF;
		}
} T;

int main() {
		
		read(N);
		for (i = 1; i <= N; i++) {
				minn = INF;
				read(x);
				if (i == 1) { 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 0;
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值