• CodeForces 682B Alyona and Mex (排序+离散化)


    Alyona and Mex

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/121333#problem/B

    Description

    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.

    Sample Input

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

    题意:

    可以任意减小数组元素的大小,使得最小不出现的自然数最大;

    题解:

    排序后离散化即可;当出现不能离散化时即为最大的自然数.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #define LL long long
    #define eps 1e-8
    #define maxn 110000
    #define inf 0x3f3f3f3f
    #define IN freopen("in.txt","r",stdin);
    using namespace std;
    
    int n;
    int a[maxn];
    
    int main(int argc, char const *argv[])
    {
        //IN;
    
        while(scanf("%d",&n) != EOF)
        {
            for(int i=1; i<=n; i++)
                scanf("%d", &a[i]);
            sort(a+1,a+1+n);
    
            int ans = 0;
            for(int i=1; i<=n; i++)
                if(a[i] > ans) ans++;
    
            printf("%d
    ", ans+1);
        }
    
        return 0;
    }
    
    
  • 相关阅读:
    JavaScript DOM 选择器 querySelector
    JavaScript call()函数的应用
    flex布局中 align-items 和 align-content的区别
    移动端WEB
    触发器与存储过程
    游标
    触发器---存储过程---存储函数
    mysql中check无效
    mysql唯一性约束和索引
    分页查询
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5690900.html
Copyright © 2020-2023  润新知