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代码:

 #include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,l,r,mex=1e5+;
cin>>n>>m;
while(m--){
cin>>l>>r;
mex=min(mex,r-l+);
}
cout<<mex<<endl;
for(int i=;i<=n;++i)
cout<<i%mex<<(i==n?"\n":" ");
return ;
}

B - Alyona and mex(构造)的更多相关文章

  1. Codeforces Round #381 (Div. 1) A. Alyona and mex 构造

    A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...

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

    C. Alyona and mex Problem Description: Alyona's mother wants to present an array of n non-negative i ...

  3. Codeforces 740C. Alyona and mex 思路模拟

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

  4. Codeforces Round #358 (Div. 2)B. Alyona and Mex

    B. Alyona and Mex time limit per test 1 second memory limit per test 256 megabytes input standard in ...

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

    Alyona and Mex 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/B Description Someone gave ...

  6. Alyona and mex

    Alyona and mex time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #358 (Div. 2) B. Alyona and Mex 水题

    B. Alyona and Mex 题目连接: http://www.codeforces.com/contest/682/problem/B Description Someone gave Aly ...

  8. #381 Div2 Problem C Alyona and mex (思维 && 构造)

    题意 : 题目的要求是构造出一个长度为 n 的数列, 构造条件是在接下来给出的 m 个子区间中, 要求每一个子区间的mex值最大, 然后在这 m 个子区间产生的mex值中取最小的输出, 并且输出构造出 ...

  9. CF | Alyona and Mex

    Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyon ...

随机推荐

  1. 前端领域的BEM到底是什么

    前端领域的BEM到底是什么 BEM - Block Element Modfier(块元素编辑器) BEM方法确保每一个参加了同一网站开发项目的人,基于一套代码规范去开发,这样非常有利于团队成员理解彼 ...

  2. The C++ Programming Language - Bjarne Stroustrup

    Preface Part 1: Introduction 1.1 The Structure of This Book 1.1.1 Introduction 1.1.2 Basic Facilitie ...

  3. 我理解的数据结构(一)—— 数组(Array)

    我理解的数据结构(一)-- 数组(Array) 首先,我是一个phper,但是毕竟php是一个脚本语言,如果使用脚本语言去理解数据结构具有一定的局限性.因为脚本语言是不需要编译的,如果你的语法写的不错 ...

  4. Git 基础教程 之 别名

     配置别名, 例如:       git config --global alias.st status                                      git config ...

  5. Maven学习总结(3)——使用Maven构建项目

    Maven学习总结(三)--使用Maven构建项目 maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项目的过程. 一.构建Jave项目 1.1.创建J ...

  6. [bzoj3669][Noi2014]魔法森林_LCT_并查集

    魔法森林 bzoj-3669 Noi-2014 题目大意:说不明白题意系列++……题目链接 注释:略. 想法:如果只有1个参量的话spfa.dij什么的都上来了. 两个参量的话我们考虑,想将所有的边按 ...

  7. 循环A表,根据查询结果,更新A表字段

    create or replace procedure prc_user_xtzx_match(p_flag out varchar2) IS xingming_match_loginname ); ...

  8. oracle级联操作

    在加入foreing key约束时,还能够指定级联操作的类型,主要用于确定当删除(on delete) 附表中的一条记录时,怎样处理子表中的外键字段,有例如以下三种引用类型. cascade 此key ...

  9. quick-cocos2d-x教程7:程序框架内framework文件夹分析

    framework是整个框架执行时的实现代码.所以一定要细致看这个文件夹的文件.我们先从framework文件夹中的init.lua分析起走. init.lua文件里,框架初始化时,会自己主动加载下面 ...

  10. iOS开发使用Unwind Segue进行返回

    我们在之前的一篇博客中谈到怎样使用dismissViewControllerAnimation()的方法在iOS中返回,如今我们有一个更为方便的方法来实现界面跳转之后的返回操作.使用的是Unwind ...