zoukankan      html  css  js  c++  java
  • CF | Alyona and Mex

    Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.

    Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 ≤ bi ≤ ai for every 1 ≤ i ≤ n. Your task is to determine the maximum possible value of mex of this array.

    Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of elements in the Alyona's array.

    The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

    Output

    Print one positive integer — the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.

    Example

    Input
    5
    1 3 3 3 6
    Output
    5
    Input
    2
    2 1
    Output
    3

    Note

    In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 4 will be equal to 5.

    To reach the answer to the second sample case one must not decrease any of the array elements.

    题意:给定n个元素的数组,让你输出最大的Mex (是不出现在这个数组中的最小正整数),然而,数组是能够进行调整的,也就是元素的数值可以从大变小。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int num[1000000];
     4 bool cmp(int a,int b)  
     5 {
     6     return a < b;
     7 }
     8 int main()
     9 {
    10     int n,ans;
    11     while(cin>>n) {
    12         for(int i = 0;i < n; i++)
    13             scanf("%d",&num[i]);
    14         sort(num,num+n,cmp); //预处理,先把数组num从小到大排序  
    15         ans = 2;  //预设数组没有出现的最小整数是2 
    16         for(int i = 1; i < n; i++)
    17         {
    18             if(ans <= num[i]) // 当目前的预设ans能够被覆盖时,更新ans 
    19                 ans++;        
    20         } 
    21         cout<<ans<<endl;     
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    pam_smb
    什么是PAM认证
    如何使windows7的默认共享可以被访问[转载]
    remote mounting from windows to linux
    Kernel boot options
    Linux kernel启动选项(参数)
    tftp client命令示例
    在不同的Linux发行版上安装TFTP Server
    SpringBoot2 整合Nacos组件,环境搭建和入门案例详解
    我是如何做到springboot自动配置原理解析
  • 原文地址:https://www.cnblogs.com/jj81/p/7853476.html
Copyright © 2011-2022 走看看