A12129 | Appending Mex
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Initially Ildar has an empty array. He performs $n$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array.
The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $[0, 2, 3]$ is $1$ , while the mex of the multiset $[1, 2, 1]$ is $0$ .
More formally, on the step $m$ , when Ildar already has an array $a_1, a_2, \ldots, a_{m-1}$ , he chooses some subset of indices $1 \leq i_1 < i_2 < \ldots < i_k < m$ (possibly, empty), where $0 \leq k < m$ , and appends the $mex(a_{i_1}, a_{i_2}, \ldots a_{i_k})$ to the end of the array.
After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $a_1, a_2, \ldots, a_n$ the minimum step $t$ such that he has definitely made a mistake on at least one of the steps $1, 2, \ldots, t$ , or determine that he could have obtained this array without mistakes.
The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $[0, 2, 3]$ is $1$ , while the mex of the multiset $[1, 2, 1]$ is $0$ .
More formally, on the step $m$ , when Ildar already has an array $a_1, a_2, \ldots, a_{m-1}$ , he chooses some subset of indices $1 \leq i_1 < i_2 < \ldots < i_k < m$ (possibly, empty), where $0 \leq k < m$ , and appends the $mex(a_{i_1}, a_{i_2}, \ldots a_{i_k})$ to the end of the array.
After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $a_1, a_2, \ldots, a_n$ the minimum step $t$ such that he has definitely made a mistake on at least one of the steps $1, 2, \ldots, t$ , or determine that he could have obtained this array without mistakes.
输入格式
The first line contains a single integer $n$ ( $1 \leq n \leq 100\,000$ ) — the number of steps Ildar made.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \leq a_i \leq 10^9$ ) — the array Ildar obtained.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $0 \leq a_i \leq 10^9$ ) — the array Ildar obtained.
输出格式
If Ildar could have chosen the subsets on each step in such a way that the resulting array is $a_1, a_2, \ldots, a_n$ , print $-1$ .
Otherwise print a single integer $t$ — the smallest index of a step such that a mistake was made on at least one step among steps $1, 2, \ldots, t$ .
Otherwise print a single integer $t$ — the smallest index of a step such that a mistake was made on at least one step among steps $1, 2, \ldots, t$ .
输入输出样例
输入 #1
4 0 1 2 1
输出 #1
-1
输入 #2
3 1 0 1
输出 #2
1
输入 #3
4 0 1 2 239
输出 #3
4
In the first example it is possible that Ildar made no mistakes. Here is the process he could have followed.
- $1$ -st step. The initial array is empty. He can choose an empty subset and obtain $0$ , because the mex of an empty set is $0$ . Appending this value to the end he gets the array $[0]$ .
- $2$ -nd step. The current array is $[0]$ . He can choose a subset $[0]$ and obtain an integer $1$ , because $mex(0) = 1$ . Appending this value to the end he gets the array $[0,1]$ .
- $3$ -rd step. The current array is $[0,1]$ . He can choose a subset $[0,1]$ and obtain an integer $2$ , because $mex(0,1) = 2$ . Appending this value to the end he gets the array $[0,1,2]$ .
- $4$ -th step. The current array is $[0,1,2]$ . He can choose a subset $[0]$ and obtain an integer $1$ , because $mex(0) = 1$ . Appending this value to the end he gets the array $[0,1,2,1]$ .
Thus, he can get the array without mistakes, so the answer is $-1$ .
In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $0$ .
In the third example he could have obtained $[0, 1, 2]$ without mistakes, but $239$ is definitely wrong.
- $1$ -st step. The initial array is empty. He can choose an empty subset and obtain $0$ , because the mex of an empty set is $0$ . Appending this value to the end he gets the array $[0]$ .
- $2$ -nd step. The current array is $[0]$ . He can choose a subset $[0]$ and obtain an integer $1$ , because $mex(0) = 1$ . Appending this value to the end he gets the array $[0,1]$ .
- $3$ -rd step. The current array is $[0,1]$ . He can choose a subset $[0,1]$ and obtain an integer $2$ , because $mex(0,1) = 2$ . Appending this value to the end he gets the array $[0,1,2]$ .
- $4$ -th step. The current array is $[0,1,2]$ . He can choose a subset $[0]$ and obtain an integer $1$ , because $mex(0) = 1$ . Appending this value to the end he gets the array $[0,1,2,1]$ .
Thus, he can get the array without mistakes, so the answer is $-1$ .
In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $0$ .
In the third example he could have obtained $[0, 1, 2]$ without mistakes, but $239$ is definitely wrong.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted