• Codeforces Round #381 (Div. 2)C Alyona and mex


    Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.

    Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].

    Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.

    You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.

    The mex of a set S is a minimum possible non-negative integer that is not in S.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 105).

    The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers liand ri (1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].

    Output

    In the first line print single integer — the maximum possible minimum mex.

    In the second line print n integers — the array a. All the elements in a should be between 0 and 109.

    It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.

    If there are multiple solutions, print any of them.

    Examples
    input
    5 3
    1 3
    2 5
    4 5
    output
    2
    1 0 2 1 0
    input
    4 2
    1 4
    2 4
    output
    3
    5 2 0 1
    Note

    The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.

     解题思路:

    之前没看懂题。。后面看了下题解,就是求最小区间的mex,mex就是没在区间出现过的最小正整数,所以只要选择最小的区间就可以,他的mex最小,至于其他的区间直接按照0.。ans-1,0.。ans-1,这样取就可以了。

    实现代码:

    #include<iostream>
    using namespace std;
    
    int main()
    {
        int m,n,i,ans,a,b;
        cin>>m>>n;
        for(i=0;i<n;i++){
            cin>>a>>b;
            ans = min(ans,b-a);
        }
        ans++;
        cout<<ans<<endl;
        cout<<1%ans;
        for(i=2;i<=m;i++){
            cout<<" "<<i%ans;
        }
        return 0;
    }
  • 相关阅读:
    批量导入
    循环语句
    判断循环常见
    常见C运算符
    oc将字符串中单词按照出现次数(次数都不一样)降序排序,排序之后单词只出现一次,源字符串中单词用下划线连接,生成字符串也用下滑线连接
    把字符串中的字母大小写反转OC
    查找子串出现次数OC
    现有一个数组NSMutableArray, 数组有若干个NSString的元素,进行选择法排序
    终端的一些命令
    编程语言名字来历
  • 原文地址:https://www.cnblogs.com/kls123/p/7116155.html
Copyright © 2020-2023  润新知