您的位置:首页 > 其它

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

2016-11-28 21:43 495 查看
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 andri (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.

题意:有一串数字,给你m个区间求每一个区间内不含有的最小的数,输出全部中最小的那个尽量使得这个最小值最大,然后把符合条件的数字串输出,输出任意一种即可

由于题目要求的mex要最大(mex指着个区间中的数在0~n中缺少的最小的数字例如mex(1,2)=0,mex(0,2)=1,mex(0,1)=2)

所以尽量要从零开始递增,那么最小的mex值肯定是区间最小的那个,然后剩下的我们只要满足最小区间是从0开始的单调递增序列就可以了

只有是单调的增序列mex才使最下的mex最大,

#include<stdio.h>

#include<string.h>

#include<math.h>

#include<algorithm>

using namespace std;

int main()

{   int n,m;

    int l,r;

    int minn=0x3f3f3f3f;

    scanf("%d %d",&n,&m);

    for(int i=1;i<=m;i++)

    {  scanf("%d %d",&l,&r);

       minn=min(minn,r-l);
}
minn++; 
printf("%d\n",minn);
int tem=0;
for(int i=1;i<=n;i++)
{   if(tem==minn)
   {   tem=0;
}
printf("%d",tem);
tem++;
if(i!=n)
printf(" ");
else
printf("\n");
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: