其他分享
首页 > 其他分享> > 【ybt金牌导航4-3-1】波动值之和

【ybt金牌导航4-3-1】波动值之和

作者:互联网

波动值之和

题目链接:ybt金牌导航4-3-1

题目大意

给出 a i , n a_i,n ai​,n,求 a 1 + ∑ i = 2 n min ⁡ j = 1 i − 1 { ∣ a i − a j ∣ } a_1+\sum\limits_{i=2}^{n}\min\limits_{j=1}^{i-1}\{|a_i-a_j|\} a1​+i=2∑n​j=1mini−1​{∣ai​−aj​∣}。

思路

其实会发现难点就是求 min ⁡ \min min 里面的值。
然后你会发现就是求值跟它相差最小的跟它相差多少。

自然想到前驱加后继,然后这里用 splay 实现。

别的打了不要抄,都是没用的,而且还不确定对不对。
(不过用来提供思路也可以)

splay 主要调整的方法就是在根据题目进行操作的时候把操作要的点尽可能放到上面,就是认为它会更容易出现在询问中。

代码

#include<cstdio>
#include<iostream>

using namespace std;

struct Tree {
	int l, r, fa, val, size;
}tree[40001];
int n, X, root;
int tot, lef_root, rig_root;
long long ans;

bool son__p(int now) {//判断是否是左儿子
	return tree[tree[now].fa].l == now;
}

void rotate(int now) {//旋转
	int father = tree[now].fa;
	int grand = tree[father].fa;
	int son = son__p(now) ? tree[now].r : tree[now].l;
	if (grand) son__p(father) ? tree[grand].l = now : tree[grand].r = now;
	if (son__p(now)) tree[now].r = father, tree[father].l = son;
		else tree[now].l = father, tree[father].r = son;
	tree[now].fa = grand;
	tree[father].fa = now;
	if (son) tree[son].fa = father;
}

void splay(int x, int target) {//splay的上移操作
	while (tree[x].fa != target) {
		if (tree[tree[x].fa].fa != target) {
			son__p(x) == son__p(tree[x].fa) ? rotate(tree[x].fa) : rotate(x);
		}
		rotate(x);
	}
	
	if (!target) root = x;
}

int find(int x) {
	int now = root;
	while (now) {
		if (x == tree[now].val) break;
		if (x >= tree[now].val) now = tree[now].r;
			else now = tree[now].l;
	}
	if (now != root) splay(now, 0);
	return now;
}

void insert(int x) {//插入点
	int now = root, last = 0;
	while (now) {
		last = now;
		tree[now].size++;
		if (x < tree[now].val) now = tree[now].l;
			else now = tree[now].r;
	}
	
	tot++;
	tree[tot].fa = last;
	tree[tot].val = x;
	tree[tot].size = 1;
	if (x < tree[last].val) tree[last].l = tot;
		else tree[last].r = tot;
	
	splay(tot, 0);
}

void join(int small, int big) {
	tree[small].fa = tree[big].fa = 0;
	int new_root = small;
	while (tree[new_root].r)
		new_root = tree[new_root].r;
	splay(new_root, 0);
	tree[new_root].r = big;
	tree[big].fa = new_root;
}

void delete_(int x) {
	splay(x, 0);
	if (!tree[x].l && tree[x].r) tree[tree[x].r].fa = 0;
		else if (tree[x].l && !tree[x].r) tree[tree[x].l].fa = 0;
			else join(tree[x].l, tree[x].r);
	
	tree[x].l = tree[x].r = 0;
}

int get_rank(int x) {
	int now = find(x);
	return tree[tree[now].l].size + 1;
}

void spilt(int x) {
	int no_root = find(x);
	lef_root = tree[no_root].l;
	rig_root = tree[no_root].r;
	tree[no_root].l = tree[no_root].r = 0;
}

int pre(int x) {//求前驱
	int now = root;
	now = tree[now].l;
	if (!now) return -1;
	while (tree[now].r) {
		now = tree[now].r;
	}
	return tree[now].val;
}

int nxt(int x) {//求后继
	int now = root;
	now = tree[now].r;
	if (!now) return -1;
	while (tree[now].l) {
		now = tree[now].l;
	}
	return tree[now].val;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &X);
		
		insert(X);
		
		if (i == 1) ans += 1ll * X;
			else {
				int XX = pre(X), YY = nxt(X);
				if (XX == -1) ans += YY - tree[root].val;
					else if (YY == -1) ans += tree[root].val - XX;
						else ans += min(tree[root].val - XX, YY - tree[root].val);
			}
	}
	
	printf("%lld", ans);
	
	return 0;
}

标签:val,int,tree,ybt,fa,root,now,导航,金牌
来源: https://blog.csdn.net/weixin_43346722/article/details/113855065