洛谷P1456Monkey King

洛谷P1456 Monkey King

题目描述

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 there 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.

n个猴子打了m次架,每打一次架,双方都会派出自己一方战斗力最强的猴子参战,打完架后,所派出的猴子的战斗力减半,同时两方猴子变为朋友,如果友方内部发生冲突,输出-1,每打一次架后,输出两方猴子合并后的最强战斗力。

输入输出格式

输入格式:

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

First part: The first line contains an integer N(N<=100,000), which indicates the number of monkeys. And then N lines follows. There is one number on each line, indicating the strongness value of ith monkey(<=32768).

Second part: The first line contains an integer M(M<=100,000), 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 Xth monkey and Yth.

输出格式:

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel.

输入输出样例

输入样例#1:

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5

输出样例#1:

8
5
5
-1
10

这是一道左偏树的模板题,我发现单靠并查集是做不了的,于是去了解了一下左偏树这个东西。打架前,先判断两只猴子是不是属于同一方,若是,输出-1;若不是,直接用左偏树的merge函数合并两方猴子,同时维护左偏树,输出根节点即可。
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 using namespace std;
 6 int n,m,x1,y;
 7 int f1,f2,u,v;
 8 struct node{
 9     int r;
10     int l;
11     int v;
12     int dis;
13     int f;
14 }tr[1000001];
15 int find(int x)
16 {
17     if(tr[x].f!=x) return find(tr[x].f);
18     return x;
19 }
20 int merge(int a,int b)//合并两方猴子
21 {
22     if(a==0) return b;
23     if(b==0) return a;
24     if(tr[a].v<tr[b].v) swap(a,b);//性质1:节点的键值小于或等于它的左右子节点的键值。
25     tr[a].r=merge(tr[a].r,b);//性质2:节点的左子节点右子节点也是一颗左偏树。
26     tr[tr[a].r].f=a;
27     if(tr[tr[a].l].dis<tr[tr[a].r].dis) swap(tr[a].l,tr[a].r);//性质:3:节点的左子节点的距离不小于右子节点的距离。
28     if(tr[a].r==0) tr[a].dis=0;
29     else tr[a].dis=tr[tr[a].r].dis+1;
30     return a;
31 }
32 int pop(int a)
33 {
34     int l=tr[a].l;
35     int r=tr[a].r;
36     tr[l].f=l;
37     tr[r].f=r;
38     tr[a].l=tr[a].r=tr[a].dis=0;
39     return merge(l,r);
40 }
41 int main()
42 {
43     while(scanf("%d",&n)!=EOF)//多组数据
44     {
45        for(int i=1;i<=n;i++)
46        {
47            scanf("%d",&tr[i].v);
48            tr[i].l=tr[i].r=tr[i].dis=0;
49            tr[i].f=i;
50        }
51        scanf("%d",&m);
52        for(int i=1;i<=m;i++)
53        {
54           scanf("%d%d",&x1,&y);
55           f1=find(x1);
56           f2=find(y);
57           if(f1==f2)
58           printf("-1\n");
59           else
60           {
61              tr[f1].v/=2;
62              u=pop(f1);
63              u=merge(u,f1);
64              tr[f2].v/=2;
65              v=pop(f2);
66              v=merge(v,f2);
67              printf("%d\n",tr[merge(u,v)].v);
68           }
69        }
70     }
71     return 0;
72 }
时间: 2024-07-31 04:28:56

洛谷P1456Monkey King的相关文章

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3

洛谷1231 教辅的组成

洛谷1231 教辅的组成 https://www.luogu.org/problem/show?pid=1231 题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书多得数不胜数,其中有书,有答案,有练习册.已知一个完整的书册均应该包含且仅包含一本书.一本练习册和一份答案,然而现在全都乱做了一团.许多书上面的字迹都已经模糊了,然而HansBug还是可

洛谷教主花园dp

洛谷-教主的花园-动态规划 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都不一样,一些树可能会因为不适合这个位置的土壤而损失观赏价值. 教主最喜欢3种树,这3种树的高度分别为10,20,30.教主希望这一圈树种得有层次感,所以任何一个位置的树要比它相邻的两棵树的高度都高或者都低,并且在此条件下,教主想要你设计出一套方案,使得观赏价值之和最高. 输入输出格式 输入格式: 输入文件garden.in的第1行为一个正整数n,表示需要种的

洛谷 P2801 教主的魔法 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2801 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是不超过1000的正整数.教主的魔法每次可以把闭区间[L, R](1≤L≤R≤N)内的英雄的身高全部加上一个整数W.(虽然L=R时并不

洛谷P1466 集合 Subset Sums

洛谷P1466 集合 Subset Sums这题可以看成是背包问题 用空间为 1--n 的物品恰好填充总空间一半的空间 有几种方案 01 背包问题 1.注意因为两个交换一下算同一种方案,所以最终 要 f [ v ] / 2 2.要开 long long 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string&g

洛谷P1160 队列安排 链表

洛谷P1160 队列安排   链表 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <string> 6 #include <algorithm> 7 #include <iomanip> 8 #include <iostream> 9 using namespace std

洛谷 P3367 并查集模板

#include<cstdio> using namespace std; int n,m,p; int father[2000001]; int find(int x) { if(father[x]!=x) father[x]=find(father[x]); return father[x]; } void unionn(int i,int j) { father[j]=i; } int main() { scanf("%d%d",&n,&m); for

[题解]洛谷比赛『期末考后的休闲比赛2』

[前言] 这场比赛已经结束了有几天,但我各种忙,虽然AK但还是没来得及写题解.(我才不会告诉你我跑去学数据结构了) T1 区间方差 (就不贴题好了) 首先可以推公式(我们可以知道,线段树然而并不能通过初中学过的方差公式在log(L)内求出方差): (s2表示方差,L表示区间长度,xi表示区间的每一项,最后一个x上画了一根线表示这些数据的平均数) 用二项式定理完全平方公式可得: 再次展开: 另外,再代入以下这个 得到了: 然后继续吧.. 然后duang地一声合并同类项,于是我们得到了: 然后可以高

洛谷P1471 方差

蒟蒻HansBug在一本数学书里面发现了一个神奇的数列,包含N个实数.他想算算这个数列的平均数和方差. ——by 洛谷; http://www.luogu.org/problem/show?pid=1471 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<