AtCoder题解——Beginner Contest 170——E - Smart Infants

题目相关

题目链接

AtCoder Beginner Contest 170 E 题,https://atcoder.jp/contests/abc170/tasks/abc170_e

Problem Statement

There are N infants registered in AtCoder, numbered 1 to N, and 2×10^5 kindergartens, numbered 1 to 2×10^5. Infant i has a rating of Ai and initially belongs to Kindergarten Bi.

From now on, Q transfers will happen. After the j-th transfer, Infant Cj will belong to Kindergarten Dj.

Here, we define the evenness as follows. For each kindergarten with one or more infants registered in AtCoder, let us find the highest rating of an infant in the kindergarten. The evenness is then defined as the lowest among those ratings.

For each of the Q transfers, find the evenness just after the transfer.

Input

Input is given from Standard Input in the following format:

N Q
A1 B1
A2 B2
.
.
AN BN
C1 D1
C2 D2
.
.
CQ DQ

Output

Print Q lines. The j-th line should contain the evenness just after the j-th transfer.

Samples1

Sample Input 1

6 3
8 1
6 2
9 3
1 1
2 2
1 3
4 3
2 1
1 2

Sample Output 1

6
2
6

Explaination

Initially, Infant 1,4 belongs to Kindergarten 1, Infant 2,5 belongs to Kindergarten 2, and Infant 3,6 belongs to Kindergarten 3.

After the 1-st transfer that makes Infant 4 belong to Kindergarten 3, Infant 1 belongs to Kindergarten 1, Infant 2,5 belong to Kindergarten 2, and Infant 3,4,6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1,2,3 are 8,6,9, respectively. The lowest among them is 6, so the 1-st line in the output should contain 6.

After the 2-nd transfer that makes Infant 2 belong to Kindergarten 1, Infant 1,2 belong to Kindergarten 1, Infant 5 belongs to Kindergarten 2, and Infant 3,4,6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1,2,3 are 8,2,9, respectively. The lowest among them is 2, so the 2-nd line in the output should contain 2.

After the 3-rd transfer that makes Infant 1 belong to Kindergarten 2, Infant 2 belongs to Kindergarten 1, Infant 1,5 belong to Kindergarten 2, and Infant 3,4,6 belong to Kindergarten 3. The highest ratings of an infant in Kindergarten 1,2,3 are 6,8,9, respectively. The lowest among them is 6, so the 3-rd line in the output should contain 6.

Samples2

Sample Input 2

2 2
4208 1234
3056 5678
1 2020
2 2020

Sample Output 2

3056
4208

Constraints

  • 1 ≤ N, Q ≤ 2×10^5
  • 1 ≤ Ai ≤ 10^9
  • 1 ≤ Cj ≤ N
  • 1 ≤ Bi, Dj ≤ 2×10^5
  • All values in input are integers.
  • In the j-th transfer, Infant Cj changes the kindergarten it belongs to.

题解报告

题目翻译

有 N 个婴儿在 AtCoder 注册,对应的序号从 1 到 N,同时有序号从 1 到 2*10^5 个幼儿园。第 i 号婴儿拥有数值 Ai,开始的时候分配到 Bi 的幼儿园。

从现在开始,有 Q 次转园操作发生,第 j 次转园后,婴儿 Cj 将转到 Dj 幼儿园。

我们定义的均匀性(evenness)如下:在每个幼儿园中找到所有婴儿的最高数值,那么均匀性(evenness)就是所有幼儿园中婴儿最高数值的最小值。

样例数据分析

样例数据 1

根据样例数据可知。N=6,表示有 6 个婴儿,Q=3,表示后面有 3 次操作。接下来的 6 行是婴儿的 rating。

8 1
6 2
9 3
1 1
2 2
1 3

这样,我们可以绘制出初始的婴儿与幼儿园的关系,如下图所示。

1、第一次操作。4 3,表示 4 号婴儿转到 3 号幼儿园,那么对应的婴儿与幼儿园的关系,如下图所示。

这样我们可以看到: 1 号幼儿园有婴儿 1,对应最大的数据为 8;2 号幼儿园有婴儿 2,5,对应最大的数据为 6;3 号幼儿园有婴儿 3,4,6,对应最大的数据为 9。这样 8,6,9 中对应的最小值为 6。

2、第一次操作。2 1,表示 2 号婴儿转到 1 号幼儿园,那么对应的婴儿与幼儿园的关系,如下图所示。

这样我们可以看到: 1 号幼儿园有婴儿 1,2,对应最大的数据为 8;2 号幼儿园有婴儿 5,对应最大的数据为 2;3 号幼儿园有婴儿 3,4,6,对应最大的数据为 9。这样 8,2,9 中对应的最小值为 2。

3、第一次操作。1 2,表示 1 号婴儿转到 2 号幼儿园,那么对应的婴儿与幼儿园的关系,如下图所示。

这样我们可以看到: 1 号幼儿园有婴儿 2,对应最大的数据为 6;2 号幼儿园有婴儿 1,5,对应最大的数据为 8;3 号幼儿园有婴儿 3,4,6,对应最大的数据为 9。这样 6,8,9 中对应的最小值为 6。

样例数据 2

略。我偷懒了,道歉。

题目分析

从上面的样例数据分析中,我们可以看出本题是以到模拟题,考试内容属于数据结构范畴。就是如何使用一个最优的数据结构来解决本题。

数据规模分析

1、Ai 数据范围是 [1, 10^9],那么可以用 int 来表示。

2、N 数据范围是 [1, 2*10^5],因此算法的复杂度不能超过 O(nlogn)。

数据结构设计

 从样例数据分析中,我们发现两个细节:

1、每个幼儿园中找出婴儿最大值。可以考虑使用优先队列的升序队列。

2、所有幼儿园中找出最小值。可以考虑使用优先队列的降序队列。

AC 参考代码

待完成。今天事情比较多。晚些时候再完成。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值