CF1612F.Armor and Weapons

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Monocarp plays a computer game. There are nn different sets of armor and mm different weapons in this game. If a character equips the ii-th set of armor and wields the jj-th weapon, their power is usually equal to i+ji + j; but some combinations of armor and weapons synergize well. Formally, there is a list of qq ordered pairs, and if the pair (i,j)(i, j) belongs to this list, the power of the character equipped with the ii-th set of armor and wielding the jj-th weapon is not i+ji + j, but i+j+1i + j + 1.

Initially, Monocarp's character has got only the 11-st armor set and the 11-st weapon. Monocarp can obtain a new weapon or a new set of armor in one hour. If he wants to obtain the kk-th armor set or the kk-th weapon, he must possess a combination of an armor set and a weapon that gets his power to kk or greater. Of course, after Monocarp obtains a weapon or an armor set, he can use it to obtain new armor sets or weapons, but he can go with any of the older armor sets and/or weapons as well.

Monocarp wants to obtain the nn-th armor set and the mm-th weapon. What is the minimum number of hours he has to spend on it?

Monocarp 正在玩一款电脑游戏。游戏中共有 nn 种不同的护甲套装和 mm 种不同的武器。若角色装备第 ii 套护甲并持用第 jj 把武器,则其通常战斗力为 i+ji + j;但某些护甲与武器的组合具有良好的协同效应。形式化地,给定一个包含 qq 个有序对的列表:若有序对 (i,j)(i, j) 属于该列表,则装备第 ii 套护甲并持用第 jj 把武器的角色的战斗力并非 i+ji + j,而是 i+j+1i + j + 1。

初始时,Monocarp 的角色仅拥有第 11 套护甲和第 11 把武器。Monocarp 每小时可获得一件新武器或一套新护甲。若他想获得第 kk 套护甲或第 kk 把武器,则他必须已拥有一组护甲与武器的组合,使得该组合的战斗力至少为 kk。当然,在 Monocarp 获得某件武器或某套护甲后,他即可用其进一步获取新的护甲或武器;同时,他仍可继续使用此前已获得的任意护甲和/或武器。

Monocarp 希望获得第 nn 套护甲和第 mm 把武器。他最少需要花费多少小时?

输入格式

The first line contains two integers nn and mm (2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5) — the number of armor sets and the number of weapons, respectively.

The second line contains one integer qq (0≤q≤min⁡(2⋅105,nm)0 \le q \le \min(2 \cdot 10^5, nm)) — the number of combinations that synergize well.

Then qq lines follow, the ii-th line contains two integers aia_i and bib_i (1≤ai≤n1 \le a_i \le n; 1≤bi≤m1 \le b_i \le m) meaning that the aia_i-th armor set synergizes well with the bib_i-th weapon. All pairs (ai,bi)(a_i, b_i) are distinct.

第一行包含两个整数 nn 和 mm(2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5),分别表示护甲套装的数量和武器的数量。

第二行包含一个整数 qq(0≤q≤min⁡(2⋅105,nm)0 \le q \le \min(2 \cdot 10^5, nm)),表示协同效果良好的组合数量。

接下来是 qq 行,其中第 ii 行包含两个整数 aia_i 和 bib_i(1≤ai≤n1 \le a_i \le n;1≤bi≤m1 \le b_i \le m),表示第 aia_i 套护甲与第 bib_i 件武器协同效果良好。所有对 (ai,bi)(a_i, b_i) 均互不相同。

输出格式

Print one integer — the minimum number of hours Monocarp has to spend to obtain both the nn-th armor set and the mm-th weapon.

输出一个整数——Monocarp 为获得第 nn 套护甲和第 mm 件武器所需花费的最少小时数。

输入输出样例

  • 输入#1

    3 4
    0

    输出#1

    3
  • 输入#2

    3 4
    2
    1 1
    1 3

    输出#2

    2

说明/提示

In the first example, Monocarp can obtain the strongest armor set and the strongest weapon as follows:

  1. Obtain the 22-nd weapon using the 11-st armor set and the 11-st weapon;
  2. Obtain the 33-rd armor set using the 11-st armor set and the 22-nd weapon;
  3. Obtain the 44-th weapon using the 33-rd armor set and the 22-nd weapon.

In the second example, Monocarp can obtain the strongest armor set and the strongest weapon as follows:

  1. Obtain the 33-rd armor set using the 11-st armor set and the 11-st weapon (they synergize well, so Monocarp's power is not 22 but 33);
  2. Obtain the 44-th weapon using the 33-rd armor set and the 11-st weapon.

在第一个例子中,Monocarp 可以按如下方式获得最强的护甲套装和最强的武器:

  1. 使用第 11 套护甲和第 11 把武器获得第 22 把武器;
  2. 使用第 11 套护甲和第 22 把武器获得第 33 套护甲;
  3. 使用第 33 套护甲和第 22 把武器获得第 44 把武器。

在第二个例子中,Monocarp 可以按如下方式获得最强的护甲套装和最强的武器:

  1. 使用第 11 套护甲和第 11 把武器获得第 33 套护甲(它们协同效果极佳,因此 Monocarp 的力量值不是 22,而是 33);
  2. 使用第 33 套护甲和第 11 把武器获得第 44 把武器。

输入解题思路,AI测评打分。不知道怎么写?

首页