您的位置:首页 > 其它

Codeforces Round #381 (Div. 2)C(构造,思维)

2016-11-25 21:16 393 查看
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.

题意:给你一个长度为n的非负数区间和m个子区间。定义mex为子区间未出现的最小正整数,问 最大的最小正整数是谁和构造出任意满足题意区间。

题解:

这个最大的最小正整数一定是最小区间长度+1,如何构造这个区间才是问题。想一下剩下子区间的长度都大于等于ans。我们只要将这些子区间从0—ans-1赋值即可。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[1000000];
int main()
{
int n,m,x,y,l,r;
cin>>n>>m;
int ans=1e120;
memset(a,0,sizeof(a));
while(m--)
{
cin>>x>>y;
if(y-x<ans)
{
ans=y-x;
l=x;
r=y;
}
}
ans++;
int tmp=0;
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
a[i]=tmp++;
if(tmp==ans)
tmp=0;
}
for(int i=1;i<=n;i++)
{
printf("%I64d ",a[i]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces