zoukankan      html  css  js  c++  java
  • Codeforces 740C. Alyona and mex 思路模拟

    C. Alyona and mex
    time limit per test:
    2 seconds
    memory limit per test:
    256 megabytes
    input:
    standard input
    output:
    standard output

    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 li and 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.

    题目链接:http://codeforces.com/contest/740/problem/C


    题意:定义mex为一个集合里面没有出现的最小的非负整数,有一个长度为n的集合,m个区间,现在要使得m各区间的最小的mex最大。

    思路:最小的mex就是最小的区间长度。因为每个区间0,1,2,3...,才能使得最小的mex最大。当时最小的区间限制了长度,所以最小的mex最大为最小的区间长度x。长度为n的数组只要0,1,2,3,...x-2,x-1循环至n个数。因为每个区间的都是大于等于x的,所以无论怎么取区间都会有0~x-1。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 using namespace std;
     7 int main()
     8 {
     9     int n,m;
    10     scanf("%d%d",&n,&m);
    11     int Max=1e6+100;
    12     for(int i=0; i<m; i++)
    13     {
    14         int l,r;
    15         scanf("%d%d",&l,&r);
    16         Max=min(Max,r-l+1);
    17     }
    18     cout<<Max<<endl;
    19     int sign=0;
    20     for(int i=0; i<n; i++)
    21     {
    22         cout<<sign<<" ";
    23         sign++;
    24         sign=sign%Max;
    25     }
    26     cout<<endl;
    27     return 0;
    28 }
    View Code
    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    vue-cli构建的项目手动添加eslint配置
    给通过canvas生成的二维码添加logo
    webpack打包时候去掉console.log配置
    gist.github.com 被墙无法访问解决办法
    axios.js 在测试机ios7.1的iphone4中不能发送http请求解决方案
    linux 系统的7个运行级别
    今天遇到了不能创建mysql函数
    今天测试大商创,遇到了 upstream sent too big header while reading response header from upstream
    mysql5.7 datetime 默认值为‘0000-00-00 00:00:00'值无法创建问题解决
    IE浏览器中判断IE版本
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/6099215.html
Copyright © 2011-2022 走看看