您的位置:首页 > 其它

Codeforces#381(Div. 2) C. Alyona and mex【思维】好题~

2016-11-24 13:18 369 查看
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(l,r)值为区间【l,r】内没有出现过的第一个非负整数(0,1,2,3,4..............).

我们需要构造出一个序列,使其所有区间的mex值中最小的值尽可能的大。

输出这个最小值。

以及输出任意一个可行解。

思路:

1、首先我们通过观察和分析不难得出这样一个结论:

对应我们这m个区间里边,长度最短的区间,其长度就是这个要输出的值,记做ans。

2、那么接下来还是一个脑筋急转弯的题:我们要如何构造出来一个合法的序列呢?

其实对应第i个位子的值定义为i%ans即可。

这样做一定能够保证在长度为ans的区间内,保证有从0到ans-1的所有数值。

那么其他区间因为一定长度大于等于这个ans,那么肯定一点:这样的其他的序列,长度范围内,肯定也包含了从0到ans-1的所有数值。

那么此时满足,所有区间的mex值都和最短长度的mex值相等。满足需求序列。

Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int minn=0x3f3f3f3f;
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
minn=min(minn,y-x+1);
}
printf("%d\n",minn);
for(int i=1;i<=n;i++)
{
printf("%d ",i%minn);
}
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces#381Div. 2