[題解]Mail.Ru Cup 2018 Round 1 - B. Appending Mex

【題目】ios

B. Appending Mex數組

【描述】ide

Ildar定義了一種方法,能夠由一個數組產生一個數。具體地,從這個數組中任選一個子集,不在這個子集中的最小的非負整數稱爲mex,就是由這個數組獲得的數。初始時刻Ildar的數組是一個空數組,經過上述方法獲得某個mex,加入到數組的尾端,不斷重複以上操做。如今給你一個n長的數組a,問Ildar可否獲得這個數組,若是能則輸出-1,不然輸出最小的整數t,表示數組的前t個數中至少有一個數不能獲得。spa

數據範圍:1<=n<=100000,0<=a[i]<=10^9code

【思路】blog

要想獲得數字0,能夠選擇空集做爲子集;要想獲得數字k,選擇的子集必須包含{0,1,...,k-1}。因而,從前日後掃給的數組a,當前的數字最多能比以前出現過的數字大1,不然這個數字是不能被獲得的,當前位置就是t。get

【個人實現】string

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 #define MaxN 100020
 9 int a[MaxN];
10 
11 int main()
12 {
13     int n, x;
14     int cur = -1;
15     scanf("%d", &n);
16     for(int i = 1; i <= n; i++)
17     {
18         scanf("%d", &x);
19         if(x > cur + 1)
20         {
21             printf("%d", i);
22             return 0;
23         }
24         cur = max(cur, x);
25     }
26     printf("-1");
27     return 0;
28 }
View Code

【評測結果】it

相關文章
相關標籤/搜索