F. Session in BSU

Codeforces
IDCF1027F
Time4000ms
Memory256MB
Difficulty
binary searchdfs and similardsugraph matchingsgraphs
English · Original
Chinese · Translation
Formal · Original
Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly $n$ exams. For the each exam $i$ there are known two days: $a_i$ — day of the first opportunity to pass the exam, $b_i$ — day of the second opportunity to pass the exam ($a_i < b_i$). Polycarp _can pass at most one exam_ during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the $n$ exams. Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the $n$ exams, or print _\-1_ if he cannot pass all the exams at all. ## Input The first line of the input contains one integer $n$ ($1 \le n \le 10^6$) — the number of exams. The next $n$ lines contain two integers each: $a_i$ and $b_i$ ($1 \le a_i < b_i \le 10^9$), where $a_i$ is the number of day of the first passing the $i$\-th exam and $b_i$ is the number of day of the second passing the $i$\-th exam. ## Output If Polycarp cannot pass all the $n$ exams, print _\-1_. Otherwise print the minimum index of day by which Polycarp can do that. [samples]
Polycarp 在 Berland 国立大学学习。很快他就要参加考试了。他必须通过恰好 $n$ 门考试。 对于每门考试 $i$,已知两个日期:$a_i$ —— 第一次参加考试的日期,$b_i$ —— 第二次参加考试的日期($a_i < b_i$)。Polycarp 在每一天最多只能参加一门考试。对于每门考试,Polycarp 自行选择在哪一天参加。他必须通过所有 $n$ 门考试。 Polycarp 希望尽可能早地通过所有考试。请输出 Polycarp 能够通过所有考试的最小日期编号,如果他根本无法通过所有考试,则输出 _-1_。 输入的第一行包含一个整数 $n$ ($1 lt.eq n lt.eq 10^6$) —— 考试的数量。 接下来的 $n$ 行每行包含两个整数:$a_i$ 和 $b_i$ ($1 lt.eq a_i < b_i lt.eq 10^9$),其中 $a_i$ 是第 $i$ 门考试第一次参加的日期,$b_i$ 是第二次参加的日期。 如果 Polycarp 无法通过所有 $n$ 门考试,请输出 _-1_。否则,请输出他能够完成所有考试的最小日期编号。 ## Input 输入的第一行包含一个整数 $n$ ($1 lt.eq n lt.eq 10^6$) —— 考试的数量。接下来的 $n$ 行每行包含两个整数:$a_i$ 和 $b_i$ ($1 lt.eq a_i < b_i lt.eq 10^9$),其中 $a_i$ 是第 $i$ 门考试第一次参加的日期,$b_i$ 是第二次参加的日期。 ## Output 如果 Polycarp 无法通过所有 $n$ 门考试,请输出 _-1_。否则,请输出他能够完成所有考试的最小日期编号。 [samples]
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of exams. For each exam $ i \in \{1, \dots, n\} $, let $ (a_i, b_i) \in \mathbb{Z}^2 $ denote the two available days to pass the exam, with $ 1 \leq a_i < b_i \leq 10^9 $. Let $ D = \{a_i, b_i \mid i = 1, \dots, n\} $ be the set of all available exam days. A valid assignment is an injection $ f: \{1, \dots, n\} \to D $ such that $ f(i) \in \{a_i, b_i\} $ for all $ i $, and $ f(i) \ne f(j) $ for all $ i \ne j $. **Constraints** 1. $ 1 \leq n \leq 10^6 $ 2. For all $ i \in \{1, \dots, n\} $: $ 1 \leq a_i < b_i \leq 10^9 $ 3. Each day can be used for at most one exam. **Objective** Find the minimum value $ d \in \mathbb{Z}^+ $ such that there exists a valid assignment $ f $ with $ \max_{i} f(i) = d $. If no such assignment exists, output $-1$. Equivalently, find the minimal $ d $ such that all exams can be scheduled on days $ \leq d $, with each exam $ i $ assigned to either $ a_i $ or $ b_i $, and no two exams assigned to the same day.
Samples
Input #1
2
1 5
1 7
Output #1
5
Input #2
3
5 13
1 5
1 7
Output #2
7
Input #3
3
10 40
40 80
10 80
Output #3
80
Input #4
3
99 100
99 100
99 100
Output #4
\-1
API Response (JSON)
{
  "problem": {
    "name": "F. Session in BSU",
    "description": {
      "content": "Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly $n$ exams. For the each exam $i$ there are known two days: $a_i$ — day of the first opportunit",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1027F"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly $n$ exams.\n\nFor the each exam $i$ there are known two days: $a_i$ — day of the first opportunit...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Polycarp 在 Berland 国立大学学习。很快他就要参加考试了。他必须通过恰好 $n$ 门考试。\n\n对于每门考试 $i$,已知两个日期:$a_i$ —— 第一次参加考试的日期,$b_i$ —— 第二次参加考试的日期($a_i < b_i$)。Polycarp 在每一天最多只能参加一门考试。对于每门考试,Polycarp 自行选择在哪一天参加。他必须通过所有 $n$ 门考试。\n\nPolyc...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of exams.  \nFor each exam $ i \\in \\{1, \\dots, n\\} $, let $ (a_i, b_i) \\in \\mathbb{Z}^2 $ denote the two available days to pass the exam, with...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments