B. Vlad and Cafes

Codeforces
IDCF890B
Time2000ms
Memory256MB
Difficulty
English · Original
Chinese · Translation
Formal · Original
Vlad likes to eat in cafes very much. During his life, he has visited cafes _n_ times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research. First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe. ## Input In first line there is one integer _n_ (1 ≤ _n_ ≤ 2·105) — number of cafes indices written by Vlad. In second line, _n_ numbers _a_1, _a_2, ..., _a__n_ (0 ≤ _a__i_ ≤ 2·105) are written — indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted. ## Output Print one integer — index of the cafe that Vlad hasn't visited for as long as possible. [samples] ## Note In first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes.
Vlad 非常喜欢去咖啡馆吃饭。在他的一生中,他一共访问了 #cf_span[n] 次咖啡馆。不幸的是,Vlad 开始觉得最近的几次访问彼此之间没有任何区别。为了解决这个问题,Vlad 做了一项小研究。 首先,Vlad 给所有咖啡馆分配了独立的编号。然后,他按访问顺序记录了他访问过的咖啡馆编号。现在,Vlad 希望找到这样一个咖啡馆:他最后一次访问该咖啡馆的时间,早于他最后一次访问其他所有咖啡馆的时间。换句话说,他想找到一个他最久没有去过的咖啡馆。请帮助 Vlad 找到这个咖啡馆。 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 2·105]) —— Vlad 记录的咖啡馆编号数量。 第二行包含 #cf_span[n] 个数字 #cf_span[a1, a2, ..., an] (#cf_span[0 ≤ ai ≤ 2·105]) —— Vlad 访问咖啡馆的顺序编号。Vlad 可能多次访问同一个咖啡馆。注意,在编号过程中,某些编号可能被跳过。 请输出一个整数 —— Vlad 最久没有去过的咖啡馆的编号。 在第一个测试中,共有三个咖啡馆,编号为 #cf_span[1] 和 #cf_span[2] 的咖啡馆的最后一次访问都发生在编号为 #cf_span[3] 的咖啡馆之后;因此,编号为 #cf_span[3] 的咖啡馆是答案。 在第二个测试用例中,同样有三个咖啡馆,但编号为 #cf_span[1]、#cf_span[2] 和 #cf_span[4]。编号为 #cf_span[1] 和 #cf_span[4] 的咖啡馆的最后一次访问都发生在编号为 #cf_span[2] 的咖啡馆之后,因此答案是 #cf_span[2]。请注意,Vlad 在编号咖啡馆时可能会跳过某些数字。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 2·105]) —— Vlad 记录的咖啡馆编号数量。第二行包含 #cf_span[n] 个数字 #cf_span[a1, a2, ..., an] (#cf_span[0 ≤ ai ≤ 2·105]) —— Vlad 访问咖啡馆的顺序编号。Vlad 可能多次访问同一个咖啡馆。注意,在编号过程中,某些编号可能被跳过。 ## Output 请输出一个整数 —— Vlad 最久没有去过的咖啡馆的编号。 [samples] ## Note 在第一个测试中,共有三个咖啡馆,编号为 #cf_span[1] 和 #cf_span[2] 的咖啡馆的最后一次访问都发生在编号为 #cf_span[3] 的咖啡馆之后;因此,编号为 #cf_span[3] 的咖啡馆是答案。在第二个测试用例中,同样有三个咖啡馆,但编号为 #cf_span[1]、#cf_span[2] 和 #cf_span[4]。编号为 #cf_span[1] 和 #cf_span[4] 的咖啡馆的最后一次访问都发生在编号为 #cf_span[2] 的咖啡馆之后,因此答案是 #cf_span[2]。请注意,Vlad 在编号咖啡馆时可能会跳过某些数字。
Let $ n \in \mathbb{N} $, $ n \geq 1 $, and let $ a = (a_1, a_2, \dots, a_n) $ be a sequence of integers such that $ 0 \leq a_i \leq 2 \cdot 10^5 $ for all $ i \in \{1, 2, \dots, n\} $. For each distinct cafe index $ c \in \{a_1, a_2, \dots, a_n\} $, define $ \ell(c) $ as the **last position** in the sequence where $ c $ appears, i.e., $$ \ell(c) = \max \{ i \in \{1, 2, \dots, n\} \mid a_i = c \}. $$ Let $ C = \{ a_1, a_2, \dots, a_n \} $ be the set of distinct cafe indices appearing in the sequence. We seek the cafe index $ c^* \in C $ such that: $$ \ell(c^*) = \min_{c \in C} \ell(c). $$ If multiple such $ c $ exist, output any one (the problem implies uniqueness in context, but formally, we take the minimum index among those achieving the minimum last occurrence). **Output:** $ c^* $, the cafe index with the earliest last occurrence.
Samples
Input #1
5
1 3 2 1 2
Output #1
3
Input #2
6
2 1 2 2 4 1
Output #2
2
API Response (JSON)
{
  "problem": {
    "name": "B. Vlad and Cafes",
    "description": {
      "content": "Vlad likes to eat in cafes very much. During his life, he has visited cafes _n_ times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF890B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Vlad likes to eat in cafes very much. During his life, he has visited cafes _n_ times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Vlad 非常喜欢去咖啡馆吃饭。在他的一生中,他一共访问了 #cf_span[n] 次咖啡馆。不幸的是,Vlad 开始觉得最近的几次访问彼此之间没有任何区别。为了解决这个问题,Vlad 做了一项小研究。\n\n首先,Vlad 给所有咖啡馆分配了独立的编号。然后,他按访问顺序记录了他访问过的咖啡馆编号。现在,Vlad 希望找到这样一个咖啡馆:他最后一次访问该咖啡馆的时间,早于他最后一次访问其他所有咖啡馆...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n \\in \\mathbb{N} $, $ n \\geq 1 $, and let $ a = (a_1, a_2, \\dots, a_n) $ be a sequence of integers such that $ 0 \\leq a_i \\leq 2 \\cdot 10^5 $ for all $ i \\in \\{1, 2, \\dots, n\\} $.\n\nFor each dist...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments