「ZOJ 2334」Monkey King

Posted -wallace-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「ZOJ 2334」Monkey King相关的知识,希望对你有一定的参考价值。

Description

Once in a forest, there lived (n) aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can‘t avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of their friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

And we also assume that every monkey knows himself. That is, when he is the strongest one in all of his friends, he himself will go to duel.

Input

There are several test cases, and each case consists of two parts.

First part: The first line contains an integer (n), which indicates the number of monkeys. And then (n) lines follows. There is one number on each line, indicating the strongness value of (i- ext{th}) monkey.

Second part: The first line contains an integer (1le mle 10^5), which indicates there are (m) conflicts happened. And then (m) lines follows, each line of which contains two integers (x) and (y), indicating that there is a conflict between the (x- ext{th}) monkey and (y- ext{th}).

Output

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.

Hint

  • (1le n, mle 10^5)
  • ( ext{strongness value} le 2^{15})

Solution

题目要求维护 (n) 个集合,支持:合并两个集合,将某个集合中的最大的元素减半(向下取整)。

可以用可并堆或启发式合并来解决这个问题。这里使用 线段树合并 来做,而且可以说挺入门的了。

一开始有 (n) 个线段树,第 (i) 个线段树在位置 (i) 有值,值为第 (i) 个猴子的权值。

因为每次要求最大值 的位置 再进行单点修改,于是自然而然在线段树的每个结点维护 最大值以及最大值的位置。然后 pushup 函数这么写:

/*
 * Author : _Wallace_
 * Source : https://www.cnblogs.com/-Wallace-/
 * Problem : ZOJ 2334 Monkey King 
 */
inline void pushup(int x) {
	if (maxv[lc[x]] < maxv[rc[x]]) maxv[x] = maxv[rc[x]], maxp[x] = maxp[rc[x]];
	else maxv[x] = maxv[lc[x]], maxp[x] = maxp[lc[x]];
}

因为要判断输出 -1,而且还要搞清楚这个元素所在的集合的变换,所有上一个并查集了事。

小心多组数据,注意清空。

Code

/*
 * Author : _Wallace_
 * Source : https://www.cnblogs.com/-Wallace-/
 * Problem : ZOJ 2334 Monkey King 
 */
#include <cstring>
#include <iostream>

using namespace std;
const int N = 1e5 + 5;

const int S = N << 5;
int root[N], n;
int lc[S], rc[S], total = 0;
int maxp[S], maxv[S];

#define mid ((l + r) >> 1)
inline void pushup(int x) {
	if (maxv[lc[x]] < maxv[rc[x]]) maxv[x] = maxv[rc[x]], maxp[x] = maxp[rc[x]];
	else maxv[x] = maxv[lc[x]], maxp[x] = maxp[lc[x]];
}
void insert(int& x, int p, int v, int l = 1, int r = n) {
	if (!x) x = ++total;
	if (maxv[x] < v) maxv[x] = v, maxp[x] = p;
	if (l == r) return;
	if (p <= mid) insert(lc[x], p, v, l, mid);
	else insert(rc[x], p, v, mid + 1, r);
}
void dec_half(int x, int p, int l = 1, int r = n) {
	if (!x) return;
	if (l == r) {
		maxv[x] /= 2;
		return;
	}
	if (p <= mid) dec_half(lc[x], p, l, mid);
	else dec_half(rc[x], p, mid + 1, r);
	pushup(x);
}
int merge(int x, int y) { /* x <- y*/
	if (!x || !y) return x | y;
	lc[x] = merge(lc[x], lc[y]);
	rc[x] = merge(rc[x], rc[y]);
	return pushup(x), x;
}

int uset[N];
int find(int x) {
	return x == uset[x] ? x : uset[x] = find(uset[x]);
}

void restart() {
	memset(root, 0, sizeof root);
	memset(lc, 0, sizeof(int) * (total + 1));
	memset(rc, 0, sizeof(int) * (total + 1));
	memset(maxp, 0, sizeof(int) * (total + 1));
	memset(maxv, 0, sizeof(int) * (total + 1));
	total = 0;
}

signed main() {
	ios::sync_with_stdio(false);
	int m;
	
	while (cin >> n) {
		restart();
		
		for (register int i = 1; i <= n; i++) {
			int val;  cin >> val;
			insert(root[i], i, val);
			uset[i] = i;
		}
		
		cin >> m;
		while (m--) {
			int u, v;
			cin >> u >> v;
			
			if ((u = find(u)) == (v = find(v))) {
				cout << -1 << endl;
				continue;
			}
			uset[v] = u;
			
			dec_half(root[u], maxp[root[u]]);
			dec_half(root[v], maxp[root[v]]);
			
			root[u] = merge(root[u], root[v]);
			
			cout << maxv[root[u]] << endl;
		}	
	}
	
	return 0;
}

以上是关于「ZOJ 2334」Monkey King的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1512 Monkey King ——左偏树

[ZOJ 4025] King of Karaoke

P1456 Monkey King

Monkey King

HDU 1512 Monkey King

1512 Monkey King