B. Appending Mex
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Initially Ildar has an empty array. He performs nn 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][0,2,3] is 11, while the mex of the multiset [1,2,1][1,2,1] is 00.
More formally, on the step mm, when Ildar already has an array a1,a2,…,am−1a1,a2,…,am−1, he chooses some subset of indices 1≤i1<i2<…<ik<m1≤i1<i2<…<ik<m (possibly, empty), where 0≤k<m0≤k<m, and appends the mex(ai1,ai2,…aik)mex(ai1,ai2,…aik) 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 a1,a2,…,ana1,a2,…,an the minimum step tt such that he has definitely made a mistake on at least one of the steps 1,2,…,t1,2,…,t, or determine that he could have obtained this array without mistakes.
Input
The first line contains a single integer nn (1≤n≤1000001≤n≤100000) — the number of steps Ildar made.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) — the array Ildar obtained.
Output
If Ildar could have chosen the subsets on each step in such a way that the resulting array is a1,a2,…,ana1,a2,…,an, print −1−1.
Otherwise print a single integer tt — the smallest index of a step such that a mistake was made on at least one step among steps 1,2,…,t1,2,…,t.
Examples
input
Copy
4 0 1 2 1
output
Copy
-1
input
Copy
3 1 0 1
output
Copy
1
input
Copy
4 0 1 2 239
output
Copy
4
Note
In the first example it is possible that Ildar made no mistakes. Here is the process he could have followed.
- 11-st step. The initial array is empty. He can choose an empty subset and obtain 00, because the mex of an empty set is 00. Appending this value to the end he gets the array [0][0].
- 22-nd step. The current array is [0][0]. He can choose a subset [0][0] and obtain an integer 11, because mex(0)=1mex(0)=1. Appending this value to the end he gets the array [0,1][0,1].
- 33-rd step. The current array is [0,1][0,1]. He can choose a subset [0,1][0,1] and obtain an integer 22, because mex(0,1)=2mex(0,1)=2. Appending this value to the end he gets the array [0,1,2][0,1,2].
- 44-th step. The current array is [0,1,2][0,1,2]. He can choose a subset [0][0] and obtain an integer 11, because mex(0)=1mex(0)=1. Appending this value to the end he gets the array [0,1,2,1][0,1,2,1].
Thus, he can get the array without mistakes, so the answer is −1−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 00.
In the third example he could have obtained [0,1,2][0,1,2] without mistakes, but 239239 is definitely wrong.
#include<bits/stdc++.h>
using namespace std;
int maxn,n;
int main()
{
cin>>n;
for(int i=1; i<=n; i++)
{
int x;
cin>>x;
if(x>maxn)
{
cout<<i<<endl;
return 0;
}
if(x>=maxn) maxn=x+1;
}
cout<<"-1"<<endl;
return 0;
}
探讨了如何确定给定数组是否能通过特定步骤无误构建,以及如何找到首次构建错误的步骤。该问题源于一道编程竞赛题,涉及算法、数据结构和逻辑判断。
2927

被折叠的 条评论
为什么被折叠?



