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

Sample Input:

5 3

1 3

2 5

4 5

Sample Output:

2

1 0 2 1 0

这题我觉得是思维的题,如果直接告诉这么实现的话,几乎都会,但根据题意能想到这一层次的人却不是很多,所以应更注重思维的锻炼。

【题目链接】C. Alyona and mex

【题目类型】构造

&题意:

要构造出一个数组,使他的mex值最大,mex值定义:一个区间[li,ri]内,最小的不重复的非负整数。

&题解:

仔细观察发现,最大的就是区间长度最小的长度值,之后根据这个区间,向两边扩展,按照0~dis-1的顺序,一直扩展就好了。

&代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
#define PI(A) cout<<(A)<<endl;
const int MAXN = 100000 + 5 ;
int n, m, x, y, dis;
int a[MAXN];
int main() {
while (cin >> n >> m) {
dis = INF;
rez(i, 1, m) {
int u, v;
cin >> u >> v;
if (dis > v - u + 1) {
dis = v - u + 1;
x = u, y = v;
}
}
int p = 0;
PI(dis)
rez(i, x, y) {
a[i] = p++;
}
p = 0;
rez(i, y + 1, n) {
a[i] = p % dis;
p++;
}
p = (int)2e9 - (int)2e9 % dis - 1;
red(i, x - 1, 0) {
a[i] = p % dis;
p--;
}
rez(i, 1, n)
printf("%d ", a[i]);
}
return 0;
}
上一篇:H265 Rtp封包


下一篇:【转】对H264进行RTP封包原理