zoukankan      html  css  js  c++  java
  • B

    Problem description

    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 mexesthe 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.

    解题思路:最小的mex其实就是查看m个区间中哪个区间含有的元素个数最少。这道题最明显的就是这一点,然后接下来的一行输出(即a数组中的每个元素)让我一头雾水,完全找不到思路QAQ,完全不懂它究竟是怎么输出的=_=||,将题目精读了两个小时后,终于有了眉目。原来是要我们构造一个(新的数组元素)序列,结合红色语句可知,这个序列即数组a中的所有元素都可以是不超过最小mex这个最大值,即a[i](1<=i<=n)=i%mex(0~mex-1);这样才保证mex大于集合S中m个区间各自的mex个元素值,即验证了题目中的这句话:集合S的mex是不在S中的最小可能的非负整数。还有一点,题目已经说明了如果有多种情况,打印其中任何一个值,因此这种构造序列的方法应该是正确的。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     int n,m,l,r,mex=1e5+1;
     5     cin>>n>>m;
     6     while(m--){
     7         cin>>l>>r;
     8         mex=min(mex,r-l+1);
     9     }
    10     cout<<mex<<endl;
    11     for(int i=1;i<=n;++i)
    12         cout<<i%mex<<(i==n?"
    ":" ");
    13     return 0;
    14 }
  • 相关阅读:
    平台建设-用户反馈跟踪模块(简单爬虫)
    Android Send event 汇总 (点击,滑动,两指操作 源码)
    uiautomator 获取控件,点击的原理
    自动化进程调用CommomService获取androidapi功能
    大快搜索“大数据与智慧城市”南京技术交流会成功举办
    系统架构都经历了怎样的演变?
    云计算的云数据安全与加密技术
    从服务端视角看高并发难题
    动态分配多任务资源的移动端深度学习框架
    详谈分布式系统缓存的设计细节
  • 原文地址:https://www.cnblogs.com/acgoto/p/9118599.html
Copyright © 2011-2022 走看看