B - 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 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(构造)的更多相关文章
- 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 ...
- 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 ...
- Codeforces 740C. Alyona and mex 思路模拟
C. Alyona and mex time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- 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 ...
- CodeForces 682B Alyona and Mex (排序+离散化)
Alyona and Mex 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/B Description Someone gave ...
- Alyona and mex
Alyona and mex time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 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 ...
- #381 Div2 Problem C Alyona and mex (思维 && 构造)
题意 : 题目的要求是构造出一个长度为 n 的数列, 构造条件是在接下来给出的 m 个子区间中, 要求每一个子区间的mex值最大, 然后在这 m 个子区间产生的mex值中取最小的输出, 并且输出构造出 ...
- CF | Alyona and Mex
Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyon ...
随机推荐
- uva253 Cube painting(UVA - 253)
题目大意 输入有三种颜色判断两个骰子是否相同 思路(借鉴) ①先用string输入那12个字符,然后for出两个骰子各自的字符串 ②这里用的算法是先找出第一个的三个面与第二个的六个面去比较,如果找到相 ...
- Java 初学者
在有C++和C#基础之下开始学习Java,主要记录了一些和C++C#不同的或不知到的点 栈对象必须初始化,否则会报错.(其他的则有默认值) byte占用8位,char占用16位 接口默认为public ...
- Linux之网络文件共享服务(FTP)
一.FTP概念 •File Transfer Protocol 早期的三个应用级协议之一 •基于C/S结构 •双通道协议:数据和命令连接 •数据传输格式:二进制(默认)和文本 •两种模式:服务器角度 ...
- 46.颜色+品牌下钻分析时按最深层metric进行排序
主要知识点: 在做下钻分析时的排序 需求,以颜色进行bucket,这里bucket里面的doc以其各品牌的平均价格排序, GET /tvs/sales/_search { " ...
- Python 字符串和数字
Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...
- BZOJ 2434 Luogu P2414 [NOI2011]阿狸的打字机 (AC自动机、树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2434 题解: 我写的是离线做法,不知道有没有在线做法. 转化一波题意,\(x\)在AC ...
- Lucas小记
组合数学全忘了 记笔记记笔记 做个简单题 代码 from bzoj4403 #include <stdio.h> #define p 1000003 typedef long long l ...
- 树状数组&线段树
先是树状数组. 令这棵树的结点编号为C1,C2...Cn.令每个结点的值为这棵树的值的总和,那么容易发现: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 ...
- Oracle数据库点滴
分页查询: SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM <= 40)W ...
- ArcGIS api for javascript——加载查询结果,悬停显示信息窗口
转自原文 ArcGIS api for javascript——加载查询结果,悬停显示信息窗口 描述 本例在开始和地图交互前执行一个查询任务并加在查询结果.这允许用户鼠标悬停在任意郡县时立即见到Inf ...