• 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;
    }
    
    
  • 相关阅读:
    一个完善的ActiveX Web控件教程
    用ATL开发和部署ActiveX网页控件
    非IE内核浏览器支持activex插件
    OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)
    CImageList使用简要说明
    VC,一条会被鼠标移动的直线
    Java NIO学习笔记之基本概念
    netty源码分析
    如何绕过验证码方式总结
    解决Eclipse 启动后总是Building WorkSpace(sleeping)
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5690900.html
Copyright © 2020-2023  润新知